Skip to content

Commit

Permalink
新增kill、设备info与process进程获取
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Jul 21, 2022
1 parent 0b6450f commit 29d52ae
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 13 deletions.
60 changes: 60 additions & 0 deletions cmd/info.go
Original file line number Diff line number Diff line change
@@ -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")
}
32 changes: 20 additions & 12 deletions cmd/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
},
Expand Down
61 changes: 61 additions & 0 deletions cmd/ps.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
}

Expand Down

0 comments on commit 29d52ae

Please sign in to comment.