Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support shell out/err splitting and exit codes #15

Open
zach-klippenstein opened this issue May 19, 2016 · 1 comment
Open

Support shell out/err splitting and exit codes #15

zach-klippenstein opened this issue May 19, 2016 · 1 comment

Comments

@zach-klippenstein
Copy link
Owner

zach-klippenstein commented May 19, 2016

adb is getting support for differentiating stdout and stderr streams, and process exit codes.

Implementation:
https://github.com/android/platform_system_core/blob/ac50d0cc5789d8462fcd2d9f6cb225e0edf9a12f/adb/shell_service.h
https://github.com/android/platform_system_core/blob/ac50d0cc5789d8462fcd2d9f6cb225e0edf9a12f/adb/shell_service.cpp

Usage: https://github.com/android/platform_system_core/blob/ac50d0cc5789d8462fcd2d9f6cb225e0edf9a12f/adb/commandline.cpp#L291

RunCommand should still be supported for combined stdin/stdout streams, but there should be an API like exec.Cmd. The old mode is called "PTY" mode, and RunCommand should probably use that. The new mode is called "protocol" mode, and multiplexes out/err streams over a single connection with a simple protocol that sends packets with stream ID headers.

The exit code is sent as a single raw byte on the kIdExit stream.

@codeskyblue
Copy link
Contributor

For now, I use this way

func (c *Device) RunCommand(cmd string, args ...string) (string, error) {
    exArgs := append(args, ";", "echo", ":$?")
    outStr, err := c.commandOutput(cmd, exArgs...)
    if err != nil {
        return outStr, err
    }
    idx := strings.LastIndexByte(outStr, ':')
    if idx == -1 {
        return outStr, fmt.Errorf("adb shell error, parse exit code failed")
    }
    exitCode, _ := strconv.Atoi(strings.TrimSpace(outStr[idx+1:]))
    if exitCode != 0 {
        err = ShellExitError{strings.Join(args, " "), exitCode}
    }
    outStr = strings.Replace(outStr[0:idx], "\r\n", "\n", -1)
    return outStr, err
}

even through a little wired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants