diff --git a/cmd/info.go b/cmd/info.go new file mode 100644 index 0000000..e168f68 --- /dev/null +++ b/cmd/info.go @@ -0,0 +1,60 @@ +/* + * Copyright (C) [SonicCloudOrg] Sonic Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package cmd + +import ( + "encoding/json" + "fmt" + "github.com/SonicCloudOrg/sonic-ios-bridge/src/util" + "os" + + "github.com/spf13/cobra" +) + +var infoCmd = &cobra.Command{ + Use: "info", + Short: "Show domain info in your device", + Long: "Show domain info in your device", + RunE: func(cmd *cobra.Command, args []string) error { + device := util.GetDeviceByUdId(udid) + if device == nil { + os.Exit(0) + } + interResult, err := device.GetValue(domain, key) + if err != nil { + return util.NewErrorPrint(util.ErrSendCommand, "get value", err) + } + if isFormat { + result, _ := json.MarshalIndent(interResult, "", "\t") + fmt.Println(string(result)) + } else { + result, _ := json.Marshal(interResult) + fmt.Println(string(result)) + } + return nil + }, +} + +var domain, key string + +func init() { + rootCmd.AddCommand(infoCmd) + infoCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )") + infoCmd.Flags().StringVarP(&domain, "domain", "d", "", "Domain") + infoCmd.Flags().StringVarP(&key, "key", "k", "", "Key") + infoCmd.Flags().BoolVarP(&isFormat, "format", "f", false, "convert to JSON string and format") +} diff --git a/cmd/kill.go b/cmd/kill.go index 2e5b695..23b12e8 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -18,10 +18,8 @@ package cmd import ( "fmt" - "github.com/SonicCloudOrg/sonic-ios-bridge/src/entity" "github.com/SonicCloudOrg/sonic-ios-bridge/src/util" giDevice "github.com/electricbubble/gidevice" - "github.com/mitchellh/mapstructure" "os" "github.com/spf13/cobra" @@ -36,24 +34,34 @@ var killCmd = &cobra.Command{ if device == nil { os.Exit(0) } - result, errList := device.InstallationProxyBrowse( - giDevice.WithBundleIDs(bundleId), - giDevice.WithReturnAttributes("CFBundleVersion", "CFBundleDisplayName", "CFBundleIdentifier")) - if errList != nil { - return util.NewErrorPrint(util.ErrSendCommand, "appList", errList) + lookup, err := device.InstallationProxyLookup(giDevice.WithBundleIDs(bundleId)) + if err != nil { + return util.NewErrorPrint(util.ErrSendCommand, "look up", err) } - if result == nil { + lookupResult := lookup.(map[string]interface{}) + if lookupResult[bundleId] == nil { fmt.Printf("%s is not in your device!", bundleId) os.Exit(0) } + lookupResult = lookupResult[bundleId].(map[string]interface{}) + execName := lookupResult["CFBundleExecutable"] + processList, errProcess := device.AppRunningProcesses() if errProcess != nil { - return util.NewErrorPrint(util.ErrSendCommand, "processList", errList) + return util.NewErrorPrint(util.ErrSendCommand, "processList", errProcess) } - a := entity.Application{} - mapstructure.Decode(result, &a) - for _, process := range processList { + var hit bool + for _, process := range processList { + if process.Name == execName { + hit = true + device.AppKill(process.Pid) + fmt.Println("kill process successful!") + break + } + } + if !hit { + fmt.Printf("%s process not found\n", bundleId) } return nil }, diff --git a/cmd/ps.go b/cmd/ps.go new file mode 100644 index 0000000..cbf4e8d --- /dev/null +++ b/cmd/ps.go @@ -0,0 +1,61 @@ +/* + * Copyright (C) [SonicCloudOrg] Sonic Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/SonicCloudOrg/sonic-ios-bridge/src/util" + "github.com/spf13/cobra" +) + +var psCmd = &cobra.Command{ + Use: "ps", + Short: "Show process in your device", + Long: "Show process in your device", + RunE: func(cmd *cobra.Command, args []string) error { + device := util.GetDeviceByUdId(udid) + if device == nil { + os.Exit(0) + } + processList, errProcess := device.AppRunningProcesses() + if errProcess != nil { + return util.NewErrorPrint(util.ErrSendCommand, "processList", errProcess) + } + if isFormat { + result, _ := json.MarshalIndent(processList, "", "\t") + fmt.Println(string(result)) + } else if isJson { + result, _ := json.Marshal(processList) + fmt.Println(string(result)) + } else { + for _, process := range processList { + fmt.Printf("%s %d %s %s\n", process.Name, process.Pid, process.RealAppName, process.StartDate) + } + } + return nil + }, +} + +func init() { + rootCmd.AddCommand(psCmd) + psCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber") + psCmd.Flags().BoolVarP(&isJson, "json", "j", false, "convert to JSON string") + psCmd.Flags().BoolVarP(&isFormat, "format", "f", false, "convert to JSON string and format") +} diff --git a/cmd/version.go b/cmd/version.go index 4e996b7..5f59319 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -27,7 +27,7 @@ var versionCmd = &cobra.Command{ Short: "Version code of sib", Long: "Version code of sib", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("1.1.1") + fmt.Println("1.1.2") }, }