0
0
mirror of https://github.com/thegeeklab/wp-ansible.git synced 2024-06-02 08:19:40 +02:00

cleanup cmd struct

This commit is contained in:
Robert Kaussow 2024-05-04 15:43:51 +02:00
parent e1b6948534
commit 156982c2f0
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
3 changed files with 42 additions and 44 deletions

View File

@ -121,21 +121,20 @@ func (p *Plugin) getPlaybooks() error {
return nil
}
func (p *Plugin) versionCommand() *execabs.Cmd {
func (p *Plugin) versionCommand() *Cmd {
args := []string{
"--version",
}
return execabs.Command(
ansibleBin,
args...,
)
return &Cmd{
Cmd: execabs.Command(ansibleBin, args...),
}
}
// pythonRequirementsCommand returns an execabs.Cmd that runs the pip install
// command with the specified Python requirements file and upgrades any existing
// packages.
func (p *Plugin) pythonRequirementsCommand() *execabs.Cmd {
func (p *Plugin) pythonRequirementsCommand() *Cmd {
args := []string{
"install",
"--upgrade",
@ -143,15 +142,14 @@ func (p *Plugin) pythonRequirementsCommand() *execabs.Cmd {
p.Settings.PythonRequirements,
}
return execabs.Command(
pipBin,
args...,
)
return &Cmd{
Cmd: execabs.Command(pipBin, args...),
}
}
// galaxyRequirementsCommand returns an execabs.Cmd that runs the Ansible Galaxy
// install command with the specified role file and verbosity level.
func (p *Plugin) galaxyRequirementsCommand() *execabs.Cmd {
func (p *Plugin) galaxyRequirementsCommand() *Cmd {
args := []string{
"install",
"--force",
@ -163,15 +161,14 @@ func (p *Plugin) galaxyRequirementsCommand() *execabs.Cmd {
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.Settings.Verbose)))
}
return execabs.Command(
ansibleGalaxyBin,
args...,
)
return &Cmd{
Cmd: execabs.Command(ansibleGalaxyBin, args...),
}
}
// ansibleCommand returns an execabs.Cmd that runs the Ansible playbook with the
// specified inventory file and various configuration options set on the Plugin struct.
func (p *Plugin) ansibleCommand(inventory string) *execabs.Cmd {
func (p *Plugin) ansibleCommand(inventory string) *Cmd {
args := []string{
"--inventory",
inventory,
@ -197,20 +194,18 @@ func (p *Plugin) ansibleCommand(inventory string) *execabs.Cmd {
args = append(args, "--list-hosts")
args = append(args, p.Settings.Playbooks.Value()...)
return execabs.Command(
ansiblePlaybookBin,
args...,
)
return &Cmd{
Cmd: execabs.Command(ansiblePlaybookBin, args...),
}
}
if p.Settings.SyntaxCheck {
args = append(args, "--syntax-check")
args = append(args, p.Settings.Playbooks.Value()...)
return execabs.Command(
ansiblePlaybookBin,
args...,
)
return &Cmd{
Cmd: execabs.Command(ansiblePlaybookBin, args...),
}
}
if p.Settings.Check {
@ -307,8 +302,8 @@ func (p *Plugin) ansibleCommand(inventory string) *execabs.Cmd {
args = append(args, p.Settings.Playbooks.Value()...)
return execabs.Command(
ansiblePlaybookBin,
args...,
)
return &Cmd{
Cmd: execabs.Command(ansiblePlaybookBin, args...),
Private: false,
}
}

View File

@ -6,7 +6,6 @@ import (
"os"
"github.com/thegeeklab/wp-plugin-go/v2/trace"
"golang.org/x/sys/execabs"
)
func (p *Plugin) run(_ context.Context) error {
@ -28,6 +27,9 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
batchCmd := make([]*Cmd, 0)
batchCmd = append(batchCmd, p.versionCommand())
if err := p.getPlaybooks(); err != nil {
return err
}
@ -52,32 +54,27 @@ func (p *Plugin) Execute() error {
defer os.Remove(p.Settings.VaultPasswordFile)
}
commands := []*execabs.Cmd{
p.versionCommand(),
}
if p.Settings.PythonRequirements != "" {
commands = append(commands, p.pythonRequirementsCommand())
batchCmd = append(batchCmd, p.pythonRequirementsCommand())
}
if p.Settings.GalaxyRequirements != "" {
commands = append(commands, p.galaxyRequirementsCommand())
batchCmd = append(batchCmd, p.galaxyRequirementsCommand())
}
for _, inventory := range p.Settings.Inventories.Value() {
commands = append(commands, p.ansibleCommand(inventory))
batchCmd = append(batchCmd, p.ansibleCommand(inventory))
}
for _, cmd := range commands {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
for _, bc := range batchCmd {
bc.Stdout = os.Stdout
bc.Stderr = os.Stderr
trace.Cmd(bc.Cmd)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=1")
bc.Env = os.Environ()
bc.Env = append(bc.Env, "ANSIBLE_FORCE_COLOR=1")
trace.Cmd(cmd)
if err := cmd.Run(); err != nil {
if err := bc.Run(); err != nil {
return err
}
}

View File

@ -5,6 +5,7 @@ import (
wp "github.com/thegeeklab/wp-plugin-go/v2/plugin"
"github.com/urfave/cli/v2"
"golang.org/x/sys/execabs"
)
//go:generate go run ../internal/docs/main.go -output=../docs/data/data-raw.yaml
@ -54,6 +55,11 @@ type Settings struct {
BecomeUser string
}
type Cmd struct {
*execabs.Cmd
Private bool
}
func New(e wp.ExecuteFunc, build ...string) *Plugin {
p := &Plugin{
Settings: &Settings{},