etcdctl: lock return exit code of exec-command

Sometimes we expect to get the exit code of the command being
executed.
This commit is contained in:
garenchan
2021-04-05 11:55:55 +08:00
parent dfb03aba0b
commit c047ed593c
3 changed files with 46 additions and 1 deletions

View File

@ -48,10 +48,25 @@ func lockCommandFunc(cmd *cobra.Command, args []string) {
}
c := mustClientFromCmd(cmd)
if err := lockUntilSignal(c, args[0], args[1:]); err != nil {
ExitWithError(ExitError, err)
code := getExitCodeFromError(err)
ExitWithError(code, err)
}
}
func getExitCodeFromError(err error) int {
if err == nil {
return ExitSuccess
}
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus()
}
}
return ExitError
}
func lockUntilSignal(c *clientv3.Client, lockname string, cmdArgs []string) error {
s, err := concurrency.NewSession(c, concurrency.WithTTL(lockTTL))
if err != nil {