From 8c633fd4b23a293f508a6dbe1dafe03fbcd10e74 Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Wed, 15 Jan 2025 23:33:55 -0600 Subject: [PATCH] [WIP] v0.7.0 getCommandType first --- examples/backy.yaml | 2 +- pkg/backy/backy.go | 10 ++++++---- pkg/backy/ssh.go | 5 +++-- pkg/backy/utils.go | 2 -- pkg/pkgman/apt/apt.go | 8 ++------ 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/examples/backy.yaml b/examples/backy.yaml index 5a7f0fe..8ebfdab 100644 --- a/examples/backy.yaml +++ b/examples/backy.yaml @@ -33,7 +33,7 @@ commands: Args: - docker-ce-cli packageManager: apt - packageOperation: install + packageOperation: upgrade cmd-lists: cmds-to-run: # this can be any name you want diff --git a/pkg/backy/backy.go b/pkg/backy/backy.go index e1603df..d274dce 100644 --- a/pkg/backy/backy.go +++ b/pkg/backy/backy.go @@ -45,12 +45,14 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([ } ) + // Get the command type + // This must be done before concatenating the arguments + command = getCommandType(command) + for _, v := range command.Args { ArgsStr += fmt.Sprintf(" %s", v) } - command = getCommandType(command) - if command.Type == "user" { if command.UserOperation == "password" { cmdCtxLogger.Info().Str("password", command.UserPassword).Msg("user password to be updated") @@ -60,7 +62,6 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([ var errSSH error // is host defined if command.Host != nil { - print("host is defined") outputArr, errSSH = command.RunCmdSSH(cmdCtxLogger, opts) if errSSH != nil { return outputArr, errSSH @@ -78,7 +79,7 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([ cmd.Stderr = cmdOutWriters if err := cmd.Run(); err != nil { - return nil, fmt.Errorf("error running command %s policy: %w", ArgsStr, err) + return nil, fmt.Errorf("error running command %s: %w", ArgsStr, err) } return parsePackageVersion(cmdOutBuf.String(), cmdCtxLogger, command, cmdOutBuf) @@ -98,6 +99,7 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([ cmdCtxLogger.Info().Str("Command", fmt.Sprintf("Running command %s on local machine", command.Name)).Send() localCMD = exec.Command(command.Cmd, command.Args...) + } if command.Dir != nil { diff --git a/pkg/backy/ssh.go b/pkg/backy/ssh.go index ffea6a0..8cdbbc7 100644 --- a/pkg/backy/ssh.go +++ b/pkg/backy/ssh.go @@ -492,7 +492,8 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) env: command.Environment, } ) - + // Get the command type + // This must be done before concatenating the arguments command.Type = strings.TrimSpace(command.Type) command = getCommandType(command) @@ -506,7 +507,7 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) Str("Host", *command.Host). Msgf("Running %s on host %s", getCommandTypeLabel(command.Type), *command.Host) - cmdCtxLogger.Debug().Str("cmd", command.Cmd).Strs("args", command.Args).Send() + // cmdCtxLogger.Debug().Str("cmd", command.Cmd).Strs("args", command.Args).Send() // Ensure SSH client is connected if command.RemoteHost.SshClient == nil { diff --git a/pkg/backy/utils.go b/pkg/backy/utils.go index 936450b..7629325 100644 --- a/pkg/backy/utils.go +++ b/pkg/backy/utils.go @@ -259,8 +259,6 @@ func getCommandType(command *Command) *Command { case "checkVersion": command.Cmd, command.Args = command.pkgMan.CheckVersion(command.PackageName, command.PackageVersion) } - } else if command.Type != "package" { - command.packageCmdSet = false } if command.Type == "user" && !command.userCmdSet { diff --git a/pkg/pkgman/apt/apt.go b/pkg/pkgman/apt/apt.go index d156c7c..0042508 100644 --- a/pkg/pkgman/apt/apt.go +++ b/pkg/pkgman/apt/apt.go @@ -40,6 +40,7 @@ func (a *AptManager) Install(pkg, version string, args []string) (string, []stri if args != nil { baseArgs = append(baseArgs, args...) } + fmt.Printf("baseArgs: %v\n", baseArgs) return baseCmd, baseArgs } @@ -56,7 +57,7 @@ func (a *AptManager) Remove(pkg string, args []string) (string, []string) { // Upgrade returns the command and arguments for upgrading a specific package. func (a *AptManager) Upgrade(pkg, version string) (string, []string) { baseCmd := a.prependAuthCommand(DefaultPackageCommand) - baseArgs := []string{"update", "&&", baseCmd, "install", "--only-upgrade", "-y "} + baseArgs := []string{"update", "&&", baseCmd, "install", "--only-upgrade", "-y"} if version != "" { baseArgs = append(baseArgs, fmt.Sprintf("%s=%s", pkg, version)) } else { @@ -105,11 +106,6 @@ func (a *AptManager) SetAuthCommand(authCommand string) { a.authCommand = authCommand } -// SetPackageParser assigns a PackageParser. -func (a *AptManager) SetPackageParser(parser pkgcommon.PackageParser) { - a.Parser = parser -} - // Parse parses the apt-cache policy output to extract Installed and Candidate versions. func (a *AptManager) Parse(output string) (*pkgcommon.PackageVersion, error) { // Check for error message in the output