Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions ansible/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions internal/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down
12 changes: 7 additions & 5 deletions plugin/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -34,20 +34,22 @@ 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
}

defer os.Remove(p.Settings.Ansible.PrivateKeyFile)
}

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
}

Expand Down
12 changes: 6 additions & 6 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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"
)

//go:generate go run ../internal/docs/main.go -output=../docs/data/data-raw.yaml

// Plugin implements provide the plugin.
type Plugin struct {
*wp.Plugin
*plugin_base.Plugin
Settings *Settings
}

Expand All @@ -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,
}
Expand All @@ -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
}
Expand Down
15 changes: 9 additions & 6 deletions plugin/util.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
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",
"--requirement",
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
}