diff --git a/README.md b/README.md index 6d7f698..31b7755 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,13 @@ $ gidevice syslog $ gidevice crashreport /path/.../local/dir/ -e -k ``` +#### Screenshot + +```shell +$ gidevice screenshot +$ gidevice screenshot -u=39xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7 -o=/path/..../screenshot.png +``` + ## Thanks | |About| diff --git a/cmd/screenshot.go b/cmd/screenshot.go new file mode 100644 index 0000000..8279659 --- /dev/null +++ b/cmd/screenshot.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "fmt" + "image" + "image/jpeg" + "image/png" + "log" + "os" + "time" + + "github.com/electricbubble/gidevice-cli/internal" + "github.com/spf13/cobra" +) + +// screenshotCmd represents the screenshot command +var screenshotCmd = &cobra.Command{ + Use: "screenshot", + Short: "Device screenshot", + Run: func(cmd *cobra.Command, args []string) { + udid, _ := cmd.Flags().GetString("udid") + outputFile, _ := cmd.Flags().GetString("outputFile") + + d, err := internal.GetDeviceFromCommand(udid) + internal.ErrorExit(err) + + raw, err := d.Screenshot() + if err != nil { + log.Fatalln(err) + } + + img, format, err := image.Decode(raw) + if err != nil { + log.Fatalln(err) + } + if len(outputFile) == 0 { + now := time.Now() + outputFile = fmt.Sprintf("%d%d%d%d%d%d", now.Year(), int(now.Month()), + now.Day(), now.Hour(), now.Minute(), now.Second()) + "." + format + } + file, err := os.Create(outputFile) + if err != nil { + log.Fatalln(err) + } + defer func() { _ = file.Close() }() + switch format { + case "png": + err = png.Encode(file, img) + case "jpeg": + err = jpeg.Encode(file, img, nil) + } + if err != nil { + log.Fatalln(err) + } + log.Println(file.Name()) + }, +} + +func init() { + rootCmd.AddCommand(screenshotCmd) + screenshotCmd.Flags().StringP("udid", "u", "", "Device uuid") + screenshotCmd.Flags().StringP("outputFile", "o", "", "Output file") +} diff --git a/go.sum b/go.sum index 3a7982a..b28d099 100644 --- a/go.sum +++ b/go.sum @@ -37,8 +37,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/electricbubble/gidevice v0.3.0 h1:7BviJ3frVmV1iIBRl1max2oDczTQ6H7XwsqSHgGiwjI= -github.com/electricbubble/gidevice v0.3.0/go.mod h1:hWRHIPf4uyiEB56hnVHVvu6MoVg7RlJY8ZV2FVgLKZA= github.com/electricbubble/gidevice v0.3.2 h1:s//oKXaC8hihiu3EYoaPwnyKfrmaEPxMVXjoXxRXb78= github.com/electricbubble/gidevice v0.3.2/go.mod h1:hWRHIPf4uyiEB56hnVHVvu6MoVg7RlJY8ZV2FVgLKZA= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=