/
kill.go
57 lines (45 loc) · 1.31 KB
/
kill.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cmd
import (
"errors"
"fmt"
giDevice "github.com/electricbubble/gidevice"
"github.com/electricbubble/gidevice-cli/internal"
"github.com/spf13/cobra"
)
// killCmd represents the kill command
var killCmd = &cobra.Command{
Use: "kill",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
internal.ErrorExit(errors.New("required parameter missing 'bundleID'"))
}
bundleID := args[0]
udid, _ := cmd.Flags().GetString("udid")
d, err := internal.GetDeviceFromCommand(udid)
internal.ErrorExit(err)
lookup, err := d.InstallationProxyLookup(giDevice.WithBundleIDs(bundleID))
internal.ErrorExit(err)
lookupResult := lookup.(map[string]interface{})
lookupResult = lookupResult[bundleID].(map[string]interface{})
execName := lookupResult["CFBundleExecutable"]
runningProcesses, err := d.AppRunningProcesses()
internal.ErrorExit(err)
pid := 0
for _, p := range runningProcesses {
if p.Name == execName {
pid = p.Pid
}
}
if pid == 0 {
internal.ErrorExit(fmt.Errorf("didn't running: %s", bundleID))
}
err = d.AppKill(pid)
internal.ErrorExit(err)
fmt.Printf("pid: %d\tbundleID: %s\n", pid, bundleID)
},
}
func init() {
rootCmd.AddCommand(killCmd)
killCmd.Flags().StringP("udid", "u", "", "Device uuid")
}