Repository navigation
feat(cmd): report a failed command's exit status - #518
Merged
Merged
Conversation
Add ProcessError.ExitCode and cmd.ExitCodeOf, read from the error each protocol's Wait returns: x/crypto/ssh's ExitError, os/exec's ExitError, and a new winrm error type carrying the code. -1 means no status was reported. Since commands run under /bin/sh, a shell's wording for a missing command depends on which shell that is: dash prints "k0s: not found" where bash prints "command not found". The status (127) is the same on both, so it is what callers should test instead of matching stderr. Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Exported API changed by this pull requestCompared against This only adds exported API. |
There was a problem hiding this comment.
🟡 Changes recommended
SSH signal-only termination incorrectly returns a synthesized exit code such as 143 instead of the documented -1, requiring a signal check and regression test.
1 open issue
What changed in this PR
Adds exit-status reporting to command errors so callers can detect failures such as missing commands without relying on shell-specific error messages.
Changes:
- Adds
ProcessError.ExitCode()andcmd.ExitCodeOf(err). - Preserves WinRM exit codes in a typed error while retaining sentinel matching.
- Adds status-reporting tests for command errors, localhost, SSH, and WinRM.
| File | Description |
|---|---|
protocol/winrm/connection.go |
Exposes nonzero exit codes through a typed error. |
protocol/winrm/connection_test.go |
Checks that command errors carry the exit code. |
protocol/ssh/session_test.go |
Tests propagation of SSH exit status 127. |
cmd/processerror.go |
Adds public exit-code accessors and protocol error extraction. |
cmd/processerror_test.go |
Covers status extraction, unavailable statuses, and localhost failures. |
Review effort: Balanced
x/crypto/ssh makes up a status of 128 plus the signal number when the server sends exit-signal without exit-status, so check Signal before trusting ExitStatus, matching os/exec's -1 for a signalled process. Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Adds
ProcessError.ExitCode()andcmd.ExitCodeOf(err), returning -1 when no status was reported.Since #433, commands run under
/bin/sh, so a missing command readssh: 1: k0s: not foundon dash where bash sayscommand not found. k0smotron's RemoteMachine cleanup matched the bash wording and stopped working on Debian and Ubuntu hosts. The exit status (127) is the same under both shells, so it is the reliable thing to check.x/crypto/ssh.ExitError.ExitStatus()exec.ExitError.ExitCode()(over openssh, 255 can also mean the ssh client failed)ExitCode();errors.Is(err, errExitCode)still holdsAdditive API, so this would go out as v2.4.0.