Skip to content

Commit

Permalink
use exit code returned by the subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed May 18, 2024
1 parent 837c5d7 commit fab94db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 12 additions & 2 deletions pkg/cmd/remoteaccess/connect/run/run.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
15 changes: 14 additions & 1 deletion pkg/cmd/remoteaccess/connect/ssh/ssh.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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()
Expand All @@ -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
})
}

0 comments on commit fab94db

Please sign in to comment.