0
0
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:
Robert Kaussow 2024-02-07 13:17:49 +01:00 committed by GitHub
parent 4ec61ecbff
commit f819abd64b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 76 deletions

View File

@ -6,7 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
"golang.org/x/sys/execabs" "github.com/thegeeklab/wp-plugin-go/trace"
) )
var ( var (
@ -46,6 +46,11 @@ func (p *Plugin) Validate() error {
p.Settings.OutFile = fmt.Sprintf("%s.plan.tfout", p.Settings.DataDir) 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 != "" { if p.Settings.TofuVersion != "" {
err := installPackage(p.Plugin.Network.Context, p.Plugin.Network.Client, p.Settings.TofuVersion, maxDecompressionSize) err := installPackage(p.Plugin.Network.Context, p.Plugin.Network.Client, p.Settings.TofuVersion, maxDecompressionSize)
if err != nil { if err != nil {
@ -53,12 +58,7 @@ func (p *Plugin) Validate() error {
} }
} }
return nil commands := []*pluginCommand{
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
commands := []*execabs.Cmd{
p.versionCommand(), 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 return err
} }
for _, cmd := range commands { for _, command := range commands {
cmd.Stdout = os.Stdout command.cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr command.cmd.Stderr = os.Stderr
command.cmd.Env = os.Environ()
if p.Settings.RootDir != "" { 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 err
} }
} }
return deleteCache(p.Settings.DataDir) return deleteDir(p.Settings.DataDir)
} }

View File

@ -3,7 +3,6 @@ package plugin
import ( import (
"fmt" "fmt"
"github.com/thegeeklab/wp-plugin-go/trace"
"golang.org/x/sys/execabs" "golang.org/x/sys/execabs"
) )
@ -11,18 +10,19 @@ const (
tofuBin = "/usr/local/bin/tofu" tofuBin = "/usr/local/bin/tofu"
) )
func (p *Plugin) versionCommand() *execabs.Cmd { type pluginCommand struct {
args := []string{ cmd *execabs.Cmd
"version", private bool
}
return execabs.Command(
tofuBin,
args...,
)
} }
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{ args := []string{
"init", "init",
} }
@ -34,27 +34,27 @@ func (p *Plugin) initCommand() *execabs.Cmd {
// Fail tofu execution on prompt // Fail tofu execution on prompt
args = append(args, "-input=false") args = append(args, "-input=false")
return execabs.Command( return &pluginCommand{
tofuBin, execabs.Command(tofuBin, args...),
args..., false,
) }
} }
func (p *Plugin) getModulesCommand() *execabs.Cmd { func (p *Plugin) getModulesCommand() *pluginCommand {
return execabs.Command( return &pluginCommand{
tofuBin, execabs.Command(tofuBin, "get"),
"get", false,
) }
} }
func (p *Plugin) validateCommand() *execabs.Cmd { func (p *Plugin) validateCommand() *pluginCommand {
return execabs.Command( return &pluginCommand{
tofuBin, execabs.Command(tofuBin, "validate"),
"validate", false,
) }
} }
func (p *Plugin) fmtCommand() *execabs.Cmd { func (p *Plugin) fmtCommand() *pluginCommand {
args := []string{ args := []string{
"fmt", "fmt",
} }
@ -75,13 +75,13 @@ func (p *Plugin) fmtCommand() *execabs.Cmd {
args = append(args, fmt.Sprintf("-check=%t", *p.Settings.FmtOptions.Check)) args = append(args, fmt.Sprintf("-check=%t", *p.Settings.FmtOptions.Check))
} }
return execabs.Command( return &pluginCommand{
tofuBin, execabs.Command(tofuBin, args...),
args..., false,
) }
} }
func (p *Plugin) planCommand(destroy bool) *execabs.Cmd { func (p *Plugin) planCommand(destroy bool) *pluginCommand {
args := []string{ args := []string{
"plan", "plan",
} }
@ -112,19 +112,13 @@ func (p *Plugin) planCommand(destroy bool) *execabs.Cmd {
args = append(args, "-refresh=false") args = append(args, "-refresh=false")
} }
cmd := execabs.Command( return &pluginCommand{
tofuBin, execabs.Command(tofuBin, args...),
args..., p.Settings.NoLog,
)
if !p.Settings.NoLog {
trace.Cmd(cmd)
} }
return cmd
} }
func (p *Plugin) applyCommand() *execabs.Cmd { func (p *Plugin) applyCommand() *pluginCommand {
args := []string{ args := []string{
"apply", "apply",
} }
@ -151,19 +145,13 @@ func (p *Plugin) applyCommand() *execabs.Cmd {
args = append(args, p.Settings.OutFile) args = append(args, p.Settings.OutFile)
cmd := execabs.Command( return &pluginCommand{
tofuBin, execabs.Command(tofuBin, args...),
args..., p.Settings.NoLog,
)
if !p.Settings.NoLog {
trace.Cmd(cmd)
} }
return cmd
} }
func (p *Plugin) destroyCommand() *execabs.Cmd { func (p *Plugin) destroyCommand() *pluginCommand {
args := []string{ args := []string{
"destroy", "destroy",
} }
@ -186,14 +174,8 @@ func (p *Plugin) destroyCommand() *execabs.Cmd {
args = append(args, "-auto-approve") args = append(args, "-auto-approve")
cmd := execabs.Command( return &pluginCommand{
tofuBin, execabs.Command(tofuBin, args...),
args..., p.Settings.NoLog,
)
if !p.Settings.NoLog {
trace.Cmd(cmd)
} }
return cmd
} }

View File

@ -142,7 +142,7 @@ func sanitizeArchivePath(d, t string) (string, error) {
return "", fmt.Errorf("%w: %v", ErrTaintedPath, t) 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) { if _, err := os.Stat(path); os.IsNotExist(err) {
return nil return nil
} }