Skip to content

Commit

Permalink
add global option -d, -s, add healthcheck command
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 10, 2018
1 parent 15b137b commit 5d0e98c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 24 deletions.
53 changes: 31 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
- [x] install support http url
- [x] support launch after install apk
- [ ] install apk and auto click confirm
- [.] check device health status
- [ ] show current app
- [ ] unlock device
- [ ] reset device state, clean up installed packages
- [ ] support `fa devices --json`
- [ ] show wlan (ip,mac,signal), enable and disable it
- [ ] share device to public web
- [ ] check device health status
- [ ] install ipa support

## Install
Expand All @@ -30,36 +30,54 @@ brew install codeskyblue/tap/fa
download binary from [**releases**](https://github.com/codeskyblue/fa/releases)

## Usage
Show version
### Show version

```bash
$ fa version
fa version v0.0.5 # just example
```

Screenshot (only png support for now)
### Run adb command with device select
if multi device connected, `fa` will give you list of devices to choose.

```bash
fa screenshot -o screenshot.png
$ fa adb shell
@ select device
> 3aff8912 Smartion
vv12afvv Google Nexus 5
{selected 3aff8912}
shell $
```
Install APK
`-s` option and `$ANDROID_SERIAL` is also supported, but if you known serial, maybe use `adb` directly is better.
```
$ fa install ApiDemos-debug.apk
```bash
$ fa -s 3578298f adb shell pwd
/
$ ANDROID_SERIAL=3578298 fa adb shell pwd
/
```
Install APK then start app
### Screenshot
only `png` format
```
$ fa install --launch ApiDemos-debug.apk
```bash
fa screenshot -o screenshot.png
```
Install APK from URL with _uninstall first and launch after installed_

### Install APK
```bash
fa install ApiDemos-debug.apk # from local file
fa install http://example.org/demo.apk # from URL
fa install -l ApiDemos-debug.apk # launch after install
fa install -f ApiDemos-debug.apk # uninstall before install
```
$ fa install --force --launch https://github.com/appium/java-client/raw/master/src/test/java/io/appium/java_client/ApiDemos-debug.apk
Show debug info when install
```bash
$ fa -d install --force --launch https://github.com/appium/java-client/raw/master/src/test/java/io/appium/java_client/ApiDemos-debug.apk
Downloading ApiDemos-debug.apk...
2.94 MiB / 2.94 MiB [================================] 100.00% 282.47 KiB/s 10s
Download saved to ApiDemos-debug.apk
Expand All @@ -72,16 +90,7 @@ Launch io.appium.android.apis ...
+ adb -s 0123456789ABCDEF shell am start -n io.appium.android.apis/.ApiDemos
```
Run adb command, if multi device connected, `fa` will give you choice to select one.
```
$ fa adb pwd
@ select device
> 3aff8912 Smartion
vv12afvv Google Nexus 5
{selected 3aff8912}
/
```
## Reference
Articles
Expand Down
34 changes: 33 additions & 1 deletion adb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
)

const (
Expand All @@ -17,12 +19,42 @@ const (
)

func adbCommand(serial string, args ...string) *exec.Cmd {
fmt.Println("+ adb", "-s", serial, strings.Join(args, " "))
if debug {
fmt.Println("+ adb", "-s", serial, strings.Join(args, " "))
}
c := exec.Command(adbPath(), args...)
c.Env = append(os.Environ(), "ANDROID_SERIAL="+serial)
return c
}

func runCommand(name string, args ...string) (err error) {
if filepath.Base(name) == name {
name, err = exec.LookPath(name)
if err != nil {
return err
}
}
procAttr := new(os.ProcAttr)
procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
proc, err := os.StartProcess(name, append([]string{name}, args...), procAttr)
if err != nil {
return err
}
procState, err := proc.Wait()
if err != nil {
return err
}
ws, ok := procState.Sys().(syscall.WaitStatus)
if !ok {
return errors.New("exit code unknown")
}
exitCode := ws.ExitStatus()
if exitCode == 0 {
return nil
}
return errors.New("exit code " + strconv.Itoa(exitCode))
}

func panicError(e error) {
if e != nil {
panic(e)
Expand Down
33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
)

var (
version = "develop"
version = "develop"
debug = false
defaultSerial string
)

type Device struct {
Expand Down Expand Up @@ -62,6 +64,9 @@ func listDevices() (ds []Device, err error) {
}

func choose(devices []Device) Device {
if defaultSerial != "" {
return Device{Serial: defaultSerial}
}
if len(devices) == 1 {
return devices[0]
}
Expand Down Expand Up @@ -139,6 +144,19 @@ func main() {
Email: "codeskyblue@gmail.com",
},
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "show debug info",
Destination: &debug,
},
cli.StringFlag{
Name: "serial, s",
Usage: "use device with given serial",
EnvVar: "ANDROID_SERIAL",
Destination: &defaultSerial,
},
}
app.Commands = []cli.Command{
{
Name: "version",
Expand Down Expand Up @@ -202,6 +220,19 @@ func main() {
},
Action: actInstall,
},
{
Name: "healthcheck",
Usage: "check device health status",
Action: func(ctx *cli.Context) error {
log.Println("check install")
err := runCommand(os.Args[0], "install", "-f", "https://github.com/appium/java-client/raw/master/src/test/java/io/appium/java_client/ApiDemos-debug.apk")
if err != nil {
return err
}
log.Println("OKAY")
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
Expand Down

0 comments on commit 5d0e98c

Please sign in to comment.