mirror of
https://github.com/thegeeklab/wp-opentofu.git
synced 2024-11-21 14:20:40 +00:00
chore: improve no_log handling for commands (#7)
This commit is contained in:
parent
4ec61ecbff
commit
f819abd64b
@ -6,7 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/execabs"
|
||||
"github.com/thegeeklab/wp-plugin-go/trace"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -46,6 +46,11 @@ func (p *Plugin) Validate() error {
|
||||
p.Settings.OutFile = fmt.Sprintf("%s.plan.tfout", p.Settings.DataDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute provides the implementation of the plugin.
|
||||
func (p *Plugin) Execute() error {
|
||||
if p.Settings.TofuVersion != "" {
|
||||
err := installPackage(p.Plugin.Network.Context, p.Plugin.Network.Client, p.Settings.TofuVersion, maxDecompressionSize)
|
||||
if err != nil {
|
||||
@ -53,12 +58,7 @@ func (p *Plugin) Validate() error {
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute provides the implementation of the plugin.
|
||||
func (p *Plugin) Execute() error {
|
||||
commands := []*execabs.Cmd{
|
||||
commands := []*pluginCommand{
|
||||
p.versionCommand(),
|
||||
}
|
||||
|
||||
@ -84,24 +84,25 @@ func (p *Plugin) Execute() error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := deleteCache(p.Settings.DataDir); err != nil {
|
||||
if err := deleteDir(p.Settings.DataDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cmd := range commands {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
for _, command := range commands {
|
||||
command.cmd.Stdout = os.Stdout
|
||||
command.cmd.Stderr = os.Stderr
|
||||
command.cmd.Env = os.Environ()
|
||||
|
||||
if p.Settings.RootDir != "" {
|
||||
cmd.Dir = p.Settings.RootDir
|
||||
command.cmd.Dir = p.Settings.RootDir
|
||||
}
|
||||
|
||||
cmd.Env = os.Environ()
|
||||
trace.Cmd(command.cmd)
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
if err := command.cmd.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return deleteCache(p.Settings.DataDir)
|
||||
return deleteDir(p.Settings.DataDir)
|
||||
}
|
||||
|
102
plugin/tofu.go
102
plugin/tofu.go
@ -3,7 +3,6 @@ package plugin
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/wp-plugin-go/trace"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
@ -11,18 +10,19 @@ const (
|
||||
tofuBin = "/usr/local/bin/tofu"
|
||||
)
|
||||
|
||||
func (p *Plugin) versionCommand() *execabs.Cmd {
|
||||
args := []string{
|
||||
"version",
|
||||
type pluginCommand struct {
|
||||
cmd *execabs.Cmd
|
||||
private bool
|
||||
}
|
||||
|
||||
return execabs.Command(
|
||||
tofuBin,
|
||||
args...,
|
||||
)
|
||||
func (p *Plugin) versionCommand() *pluginCommand {
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, "version"),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) initCommand() *execabs.Cmd {
|
||||
func (p *Plugin) initCommand() *pluginCommand {
|
||||
args := []string{
|
||||
"init",
|
||||
}
|
||||
@ -34,27 +34,27 @@ func (p *Plugin) initCommand() *execabs.Cmd {
|
||||
// Fail tofu execution on prompt
|
||||
args = append(args, "-input=false")
|
||||
|
||||
return execabs.Command(
|
||||
tofuBin,
|
||||
args...,
|
||||
)
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, args...),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) getModulesCommand() *execabs.Cmd {
|
||||
return execabs.Command(
|
||||
tofuBin,
|
||||
"get",
|
||||
)
|
||||
func (p *Plugin) getModulesCommand() *pluginCommand {
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, "get"),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) validateCommand() *execabs.Cmd {
|
||||
return execabs.Command(
|
||||
tofuBin,
|
||||
"validate",
|
||||
)
|
||||
func (p *Plugin) validateCommand() *pluginCommand {
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, "validate"),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) fmtCommand() *execabs.Cmd {
|
||||
func (p *Plugin) fmtCommand() *pluginCommand {
|
||||
args := []string{
|
||||
"fmt",
|
||||
}
|
||||
@ -75,13 +75,13 @@ func (p *Plugin) fmtCommand() *execabs.Cmd {
|
||||
args = append(args, fmt.Sprintf("-check=%t", *p.Settings.FmtOptions.Check))
|
||||
}
|
||||
|
||||
return execabs.Command(
|
||||
tofuBin,
|
||||
args...,
|
||||
)
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, args...),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) planCommand(destroy bool) *execabs.Cmd {
|
||||
func (p *Plugin) planCommand(destroy bool) *pluginCommand {
|
||||
args := []string{
|
||||
"plan",
|
||||
}
|
||||
@ -112,19 +112,13 @@ func (p *Plugin) planCommand(destroy bool) *execabs.Cmd {
|
||||
args = append(args, "-refresh=false")
|
||||
}
|
||||
|
||||
cmd := execabs.Command(
|
||||
tofuBin,
|
||||
args...,
|
||||
)
|
||||
|
||||
if !p.Settings.NoLog {
|
||||
trace.Cmd(cmd)
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, args...),
|
||||
p.Settings.NoLog,
|
||||
}
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (p *Plugin) applyCommand() *execabs.Cmd {
|
||||
func (p *Plugin) applyCommand() *pluginCommand {
|
||||
args := []string{
|
||||
"apply",
|
||||
}
|
||||
@ -151,19 +145,13 @@ func (p *Plugin) applyCommand() *execabs.Cmd {
|
||||
|
||||
args = append(args, p.Settings.OutFile)
|
||||
|
||||
cmd := execabs.Command(
|
||||
tofuBin,
|
||||
args...,
|
||||
)
|
||||
|
||||
if !p.Settings.NoLog {
|
||||
trace.Cmd(cmd)
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, args...),
|
||||
p.Settings.NoLog,
|
||||
}
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (p *Plugin) destroyCommand() *execabs.Cmd {
|
||||
func (p *Plugin) destroyCommand() *pluginCommand {
|
||||
args := []string{
|
||||
"destroy",
|
||||
}
|
||||
@ -186,14 +174,8 @@ func (p *Plugin) destroyCommand() *execabs.Cmd {
|
||||
|
||||
args = append(args, "-auto-approve")
|
||||
|
||||
cmd := execabs.Command(
|
||||
tofuBin,
|
||||
args...,
|
||||
)
|
||||
|
||||
if !p.Settings.NoLog {
|
||||
trace.Cmd(cmd)
|
||||
return &pluginCommand{
|
||||
execabs.Command(tofuBin, args...),
|
||||
p.Settings.NoLog,
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ func sanitizeArchivePath(d, t string) (string, error) {
|
||||
return "", fmt.Errorf("%w: %v", ErrTaintedPath, t)
|
||||
}
|
||||
|
||||
func deleteCache(path string) error {
|
||||
func deleteDir(path string) error {
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user