From ed42a97d1c0124a44a418cb2c87e1fe9c2bfe7f8 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 17 May 2024 21:49:52 +0200 Subject: [PATCH] chore: migrate to wp-plugin-go v3 (#63) --- ansible/ansible.go | 51 +++++++++++++++++++++++++------------------ go.mod | 4 ++-- go.sum | 4 ++-- internal/docs/main.go | 6 ++--- plugin/impl.go | 12 +++++----- plugin/plugin.go | 12 +++++----- plugin/util.go | 15 ++++++++----- 7 files changed, 59 insertions(+), 45 deletions(-) diff --git a/ansible/ansible.go b/ansible/ansible.go index f7d411e..a909370 100644 --- a/ansible/ansible.go +++ b/ansible/ansible.go @@ -3,14 +3,14 @@ package ansible import ( "errors" "fmt" + "os" "path/filepath" "strconv" "strings" "github.com/rs/zerolog/log" - "github.com/thegeeklab/wp-plugin-go/v2/types" + plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec" "github.com/urfave/cli/v2" - "golang.org/x/sys/execabs" ) const ( @@ -59,14 +59,16 @@ type Ansible struct { } // Version runs the Ansible binary with the --version flag to retrieve the current version. -func (a *Ansible) Version() *types.Cmd { +func (a *Ansible) Version() *plugin_exec.Cmd { args := []string{ "--version", } - return &types.Cmd{ - Cmd: execabs.Command(ansibleBin, args...), - } + cmd := plugin_exec.Command(ansibleBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } // GetPlaybooks retrieves the list of Ansible playbook files based on the configured playbook patterns. @@ -96,7 +98,7 @@ func (a *Ansible) GetPlaybooks() error { } // GalaxyInstall runs the ansible-galaxy install command with the configured options. -func (a *Ansible) GalaxyInstall() *types.Cmd { +func (a *Ansible) GalaxyInstall() *plugin_exec.Cmd { args := []string{ "install", "--force", @@ -108,15 +110,17 @@ func (a *Ansible) GalaxyInstall() *types.Cmd { args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", a.Verbose))) } - return &types.Cmd{ - Cmd: execabs.Command(ansibleGalaxyBin, args...), - } + cmd := plugin_exec.Command(ansibleGalaxyBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } // Play runs the Ansible playbook with the configured options. // //nolint:gocyclo -func (a *Ansible) Play() *types.Cmd { +func (a *Ansible) Play() *plugin_exec.Cmd { args := make([]string, 0) for _, inventory := range a.Inventories.Value() { @@ -143,18 +147,22 @@ func (a *Ansible) Play() *types.Cmd { args = append(args, "--list-hosts") args = append(args, a.Playbooks.Value()...) - return &types.Cmd{ - Cmd: execabs.Command(ansiblePlaybookBin, args...), - } + cmd := plugin_exec.Command(ansiblePlaybookBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } if a.SyntaxCheck { args = append(args, "--syntax-check") args = append(args, a.Playbooks.Value()...) - return &types.Cmd{ - Cmd: execabs.Command(ansiblePlaybookBin, args...), - } + cmd := plugin_exec.Command(ansiblePlaybookBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } if a.Check { @@ -251,8 +259,9 @@ func (a *Ansible) Play() *types.Cmd { args = append(args, a.Playbooks.Value()...) - return &types.Cmd{ - Cmd: execabs.Command(ansiblePlaybookBin, args...), - Private: false, - } + cmd := plugin_exec.Command(ansiblePlaybookBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd } diff --git a/go.mod b/go.mod index 8808cb2..f8f34bd 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,8 @@ go 1.22 require ( github.com/rs/zerolog v1.32.0 github.com/stretchr/testify v1.9.0 - github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 + github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 github.com/urfave/cli/v2 v2.27.2 - golang.org/x/sys v0.20.0 ) require ( @@ -31,5 +30,6 @@ require ( github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect golang.org/x/crypto v0.23.0 // indirect golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 41f134d..b286ef2 100644 --- a/go.sum +++ b/go.sum @@ -47,8 +47,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 h1:ARwYgTPZ5iPsmOenmqcCf8TjiEe8wBOHKO7H/Xshe48= -github.com/thegeeklab/wp-plugin-go/v2 v2.3.1/go.mod h1:0t8M8txtEFiaB6RqLX8vLrxkqAo5FT5Hx7dztN592D4= +github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 h1:Mv5i8S1WY+BUNjTjX6lOnB3p8S9mvM+XwfY4R98gx0g= +github.com/thegeeklab/wp-plugin-go/v3 v3.0.2/go.mod h1:ij1iJcAVgzerBTqXnmq0bu1VA+hhVVwzXKqiqfoGjjg= github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw= diff --git a/internal/docs/main.go b/internal/docs/main.go index 481bd87..2063928 100644 --- a/internal/docs/main.go +++ b/internal/docs/main.go @@ -11,8 +11,8 @@ import ( "time" "github.com/thegeeklab/wp-ansible/plugin" - "github.com/thegeeklab/wp-plugin-go/v2/docs" - "github.com/thegeeklab/wp-plugin-go/v2/template" + plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs" + plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template" ) func main() { @@ -23,7 +23,7 @@ func main() { p := plugin.New(nil) - out, err := template.Render(context.Background(), client, tmpl, docs.GetTemplateData(p.App)) + out, err := plugin_template.Render(context.Background(), client, tmpl, plugin_docs.GetTemplateData(p.App)) if err != nil { panic(err) } diff --git a/plugin/impl.go b/plugin/impl.go index d1eff8a..394040f 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -5,8 +5,8 @@ import ( "fmt" "os" - "github.com/thegeeklab/wp-plugin-go/v2/file" - "github.com/thegeeklab/wp-plugin-go/v2/types" + plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec" + plugin_file "github.com/thegeeklab/wp-plugin-go/v3/file" ) func (p *Plugin) run(_ context.Context) error { @@ -34,12 +34,13 @@ func (p *Plugin) Validate() error { func (p *Plugin) Execute() error { var err error - batchCmd := make([]*types.Cmd, 0) + batchCmd := make([]*plugin_exec.Cmd, 0) batchCmd = append(batchCmd, p.Settings.Ansible.Version()) if p.Settings.PrivateKey != "" { - if p.Settings.Ansible.PrivateKeyFile, err = file.WriteTmpFile("privateKey", p.Settings.PrivateKey); err != nil { + p.Settings.Ansible.PrivateKeyFile, err = plugin_file.WriteTmpFile("privateKey", p.Settings.PrivateKey) + if err != nil { return err } @@ -47,7 +48,8 @@ func (p *Plugin) Execute() error { } if p.Settings.VaultPassword != "" { - if p.Settings.Ansible.VaultPasswordFile, err = file.WriteTmpFile("vaultPass", p.Settings.VaultPassword); err != nil { + p.Settings.Ansible.VaultPasswordFile, err = plugin_file.WriteTmpFile("vaultPass", p.Settings.VaultPassword) + if err != nil { return err } diff --git a/plugin/plugin.go b/plugin/plugin.go index 001ad6d..f8f2ee9 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/thegeeklab/wp-ansible/ansible" - wp "github.com/thegeeklab/wp-plugin-go/v2/plugin" + plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin" "github.com/urfave/cli/v2" ) @@ -12,7 +12,7 @@ import ( // Plugin implements provide the plugin. type Plugin struct { - *wp.Plugin + *plugin_base.Plugin Settings *Settings } @@ -24,15 +24,15 @@ type Settings struct { Ansible ansible.Ansible } -func New(e wp.ExecuteFunc, build ...string) *Plugin { +func New(e plugin_base.ExecuteFunc, build ...string) *Plugin { p := &Plugin{ Settings: &Settings{}, } - options := wp.Options{ + options := plugin_base.Options{ Name: "wp-ansible", Description: "Manage infrastructure with Ansible", - Flags: Flags(p.Settings, wp.FlagsPluginCategory), + Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory), Execute: p.run, HideWoodpeckerFlags: true, } @@ -49,7 +49,7 @@ func New(e wp.ExecuteFunc, build ...string) *Plugin { options.Execute = e } - p.Plugin = wp.New(options) + p.Plugin = plugin_base.New(options) return p } diff --git a/plugin/util.go b/plugin/util.go index 510c032..a38ba5e 100644 --- a/plugin/util.go +++ b/plugin/util.go @@ -1,15 +1,16 @@ package plugin import ( - "github.com/thegeeklab/wp-plugin-go/v2/types" - "golang.org/x/sys/execabs" + "os" + + plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec" ) const pipBin = "/usr/local/bin/pip" // PipInstall returns a command to install Python packages from a requirements file. // The command will upgrade any existing packages and install the packages specified in the given requirements file. -func PipInstall(req string) *types.Cmd { +func PipInstall(req string) *plugin_exec.Cmd { args := []string{ "install", "--upgrade", @@ -17,7 +18,9 @@ func PipInstall(req string) *types.Cmd { req, } - return &types.Cmd{ - Cmd: execabs.Command(pipBin, args...), - } + cmd := plugin_exec.Command(pipBin, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd }