From fab94db29b46fa8bf1f98c1664112b05fad58010 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Sat, 18 May 2024 13:46:23 +0200 Subject: [PATCH] use exit code returned by the subcommand --- pkg/cmd/remoteaccess/connect/run/run.manual.go | 14 ++++++++++++-- pkg/cmd/remoteaccess/connect/ssh/ssh.manual.go | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/remoteaccess/connect/run/run.manual.go b/pkg/cmd/remoteaccess/connect/run/run.manual.go index 4f1a31cc1..25d3fcf0d 100644 --- a/pkg/cmd/remoteaccess/connect/run/run.manual.go +++ b/pkg/cmd/remoteaccess/connect/run/run.manual.go @@ -209,10 +209,20 @@ func (n *CmdRun) RunE(cmd *cobra.Command, args []string) error { fmt.Fprintln(n.factory.IOStreams.ErrOut, cs.Green(fmt.Sprintf("Starting external command on %s (%s)\n", device, strings.TrimRight(client.BaseURL.String(), "/")))) start := time.Now() - sshErr := runCmd.Run() + runErr := runCmd.Run() duration := time.Since(start).Truncate(time.Millisecond) fmt.Fprintf(n.factory.IOStreams.ErrOut, "Duration: %s\n", duration) - return nil, sshErr + // Use exit code from the command + if runCmd.ProcessState != nil { + if runCmd.ProcessState.ExitCode() != 0 { + return nil, cmderrors.NewErrorWithExitCode( + cmderrors.ExitCode(runCmd.ProcessState.ExitCode()), + runErr, + ) + } + } + + return nil, runErr }) } diff --git a/pkg/cmd/remoteaccess/connect/ssh/ssh.manual.go b/pkg/cmd/remoteaccess/connect/ssh/ssh.manual.go index 5b05277e6..a57b4151e 100644 --- a/pkg/cmd/remoteaccess/connect/ssh/ssh.manual.go +++ b/pkg/cmd/remoteaccess/connect/ssh/ssh.manual.go @@ -10,6 +10,7 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8yfetcher" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand" + "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmderrors" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil" "github.com/reubenmiller/go-c8y-cli/v2/pkg/completion" "github.com/reubenmiller/go-c8y-cli/v2/pkg/flags" @@ -63,6 +64,9 @@ func NewCmdSSH(f *cmdutil.Factory) *CmdSSH { $ c8y remoteaccess connect ssh --device 12345 --user admin -- systemctl status Use a non-interactive session to execute a single command and print the result + + $ c8y remoteaccess connect ssh --device 12345 --user admin -- "sh -c 'cat /etc/os-release'" + use a non-interactive session to execute a custom shell command (notice the surrounding double quotes on the command!) `), RunE: ccmd.RunE, } @@ -191,7 +195,6 @@ func (n *CmdSSH) RunE(cmd *cobra.Command, args []string) error { sshCmd.Stdout = n.factory.IOStreams.Out sshCmd.Stdin = n.factory.IOStreams.In sshCmd.Stderr = n.factory.IOStreams.ErrOut - log.Infof("Executing command: ssh %s\n", strings.Join(sshArgs, " ")) cs := n.factory.IOStreams.ColorScheme() @@ -202,6 +205,16 @@ func (n *CmdSSH) RunE(cmd *cobra.Command, args []string) error { duration := time.Since(start).Truncate(time.Millisecond) fmt.Fprintf(n.factory.IOStreams.ErrOut, "Duration: %s\n", duration) + // Use exit code from the command + if sshCmd.ProcessState != nil { + if sshCmd.ProcessState.ExitCode() != 0 { + return nil, cmderrors.NewErrorWithExitCode( + cmderrors.ExitCode(sshCmd.ProcessState.ExitCode()), + sshErr, + ) + } + } + return nil, sshErr }) }