From b10e6629222b7ee5aa1a6696ce7f24f064056442 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Wed, 19 Dec 2018 08:30:31 +0800 Subject: [PATCH] add fa shell command --- adb.go | 23 ++++++++++++++--------- go.mod | 1 + main.go | 37 +++++++++++++++++++++++-------------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/adb.go b/adb.go index 22971a8..966e91c 100644 --- a/adb.go +++ b/adb.go @@ -175,7 +175,7 @@ func (c *AdbClient) DeviceWithSerial(serial string) *AdbDevice { } } -func (c *AdbDevice) openCommand(cmd string) (reader io.ReadCloser, err error) { +func (c *AdbDevice) OpenShell(cmd string) (rw io.ReadWriteCloser, err error) { conn, err := c.newConnection() if err != nil { return @@ -191,13 +191,18 @@ func (c *AdbDevice) openCommand(cmd string) (reader io.ReadCloser, err error) { return conn, nil } -func (c *AdbDevice) OpenShell(args ...string) (reader io.ReadCloser, err error) { - return c.openCommand(shellquote.Join(args...)) +// OpenCommand accept list of args return combined output reader +func (c *AdbDevice) OpenCommand(args ...string) (reader io.ReadWriteCloser, err error) { + return c.OpenShell(shellquote.Join(args...)) } -// func (c *AdbDevice) RunShell(args ...string) (exitCode int, err error) { -// reader, err := c.OpenShell(args...) -// if err != nil { -// return -// } -// } +func (c *AdbDevice) RunCommand(args ...string) (exitCode int, err error) { + // TODO + reader, err := c.OpenCommand(args...) + if err != nil { + return + } + defer reader.Close() + _, err = io.Copy(os.Stdout, reader) + return +} diff --git a/go.mod b/go.mod index ef63e27..6981164 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ module github.com/codeskyblue/fa require ( github.com/cavaliercoder/grab v2.0.0+incompatible + github.com/fatih/color v1.7.0 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/kr/pty v1.1.3 // indirect github.com/manifoldco/promptui v0.3.2 diff --git a/main.go b/main.go index 47c4c3f..341e31a 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "log" "os" "os/exec" @@ -279,20 +280,28 @@ func main() { }, Action: actInstall, }, - // { - // Name: "shell", - // Usage: "run shell command", - // SkipFlagParsing: true, - // Action: func(ctx *cli.Context) error { - // output, exitCode, err := DefaultAdbClient.Shell(ctx.Args()...) - // if err != nil { - // return err - // } - // fmt.Print(output) - // os.Exit(exitCode) - // return nil - // }, - // }, + { + Name: "shell", + Usage: "run shell command", + SkipFlagParsing: true, + Action: func(ctx *cli.Context) error { + serial, err := chooseOne() + if err != nil { + return err + } + device := DefaultAdbClient.DeviceWithSerial(serial) + rwc, err := device.OpenCommand(ctx.Args()...) + if err != nil { + return err + } + defer rwc.Close() + go io.Copy(rwc, os.Stdin) + io.Copy(os.Stdout, rwc) + // fmt.Print(output) + // os.Exit(exitCode) + return nil + }, + }, { Name: "healthcheck", Usage: "check device health status",