From f819abd64b9a72d163f8dcb23eabe994dddd991b Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 7 Feb 2024 13:17:49 +0100 Subject: [PATCH] chore: improve no_log handling for commands (#7) --- plugin/impl.go | 31 ++++++++------- plugin/tofu.go | 102 ++++++++++++++++++++---------------------------- plugin/utils.go | 2 +- 3 files changed, 59 insertions(+), 76 deletions(-) diff --git a/plugin/impl.go b/plugin/impl.go index 5b40ecd..c08531d 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -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) } diff --git a/plugin/tofu.go b/plugin/tofu.go index 33fff8f..05a4151 100644 --- a/plugin/tofu.go +++ b/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", - } - - return execabs.Command( - tofuBin, - args..., - ) +type pluginCommand struct { + cmd *execabs.Cmd + private bool } -func (p *Plugin) initCommand() *execabs.Cmd { +func (p *Plugin) versionCommand() *pluginCommand { + return &pluginCommand{ + execabs.Command(tofuBin, "version"), + false, + } +} + +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 } diff --git a/plugin/utils.go b/plugin/utils.go index 7252562..4083234 100644 --- a/plugin/utils.go +++ b/plugin/utils.go @@ -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 }