0
0
mirror of https://github.com/thegeeklab/wp-ansible.git synced 2024-06-22 16:10:51 +02:00

chore: migrate to wp-plugin-go v3 (#63)

This commit is contained in:
Robert Kaussow 2024-05-17 21:49:52 +02:00 committed by GitHub
parent 7b51a968a2
commit ed42a97d1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 59 additions and 45 deletions

View File

@ -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
}

4
go.mod
View File

@ -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
)

4
go.sum
View File

@ -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=

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}