Skip to content

Commit

Permalink
优化命令执行方式,类似如下:
Browse files Browse the repository at this point in the history
1.查看afc命令使用帮助时:sib afc ls -h (其他命令可类比)
2.afc查看使用ls命令时:sib afc ls -f [path] (参考1中的使用说明)
  • Loading branch information
aoliaoaoaojiao committed Jul 23, 2022
1 parent 8c3eaf2 commit 427d618
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 84 deletions.
30 changes: 0 additions & 30 deletions cmd/AFC.go

This file was deleted.

21 changes: 21 additions & 0 deletions cmd/afc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"github.com/SonicCloudOrg/sonic-ios-bridge/cmd/afcUtil"
"github.com/spf13/cobra"
)

var afcCmd = &cobra.Command{
Use: "afc",
Short: "manipulate device files through afc commands",
Long: "manipulate device files through afc commands",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
return nil
},
}

func init() {
rootCmd.AddCommand(afcCmd)
afcUtil.InitAfc(afcCmd)
}
11 changes: 6 additions & 5 deletions cmd/afcUtil/InitAFC.go → cmd/afcUtil/afcInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"os"
)

var afcServer giDevice.Afc

var afcRootCMD *cobra.Command

var udid string
var bundleID string

// InitAfc 用于初始化,在上层中调用这个方法,否则不会正常进行初始化
func InitAfc(afcCMD *cobra.Command,pUdid string,pBundleID string){
udid = pUdid
bundleID = pBundleID
func InitAfc(afcCMD *cobra.Command){
afcRootCMD = afcCMD

initMkDir()
Expand All @@ -32,7 +32,7 @@ func InitAfc(afcCMD *cobra.Command,pUdid string,pBundleID string){
initRMTree()
}

func getAFCServer()() {
func getAFCServer()(afcServer giDevice.Afc) {
device := util.GetDeviceByUdId(udid)
if device == nil {
os.Exit(0)
Expand All @@ -53,4 +53,5 @@ func getAFCServer()() {
fmt.Println(err)
os.Exit(0)
}
return afcServer
}
12 changes: 8 additions & 4 deletions cmd/afcUtil/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ var afcCatCmd = &cobra.Command{
Short: "cat to view files",
Long: "cat to view files",
RunE: func(cmd *cobra.Command, args []string) error {
if afcServer==nil {
getAFCServer()
}
catFile(afcServer, args[0])
afcServer:=getAFCServer()
catFile(afcServer, catFilePath)
return nil
},
}

var catFilePath string

func initCat() {
afcRootCMD.AddCommand(afcCatCmd)
afcCatCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcCatCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcCatCmd.Flags().StringVarP(&catFilePath, "file", "f","", "cat file path")
afcCatCmd.MarkFlagRequired("file")
}

func catFile(afc giDevice.Afc, filePath string) {
Expand Down
12 changes: 8 additions & 4 deletions cmd/afcUtil/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ var afcLsCmd = &cobra.Command{
Short: "ls to view the directory",
Long: "ls to view the directory",
RunE: func(cmd *cobra.Command, args []string) error {
if afcServer==nil {
getAFCServer()
}
lsShow(afcServer, args[0])
afcServer:=getAFCServer()
lsShow(afcServer, lsDirPath)
return nil
},
}

var lsDirPath string

func initLs() {
afcRootCMD.AddCommand(afcLsCmd)
afcLsCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcLsCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcLsCmd.Flags().StringVarP(&lsDirPath,"folder", "f","", "ls folder path")
afcLsCmd.MarkFlagRequired("folder")
}

func lsShow(afc giDevice.Afc, filePath string) {
Expand Down
12 changes: 8 additions & 4 deletions cmd/afcUtil/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ var afcMkDirCmd = &cobra.Command{
Short: "create a directory",
Long: "create a directory",
RunE: func(cmd *cobra.Command, args []string) error {
if afcServer==nil {
getAFCServer()
}
err := (afcServer).Mkdir(args[0])
afcServer:=getAFCServer()
err := (afcServer).Mkdir(mkDir)
if err != nil {
fmt.Println(err)
os.Exit(0)
Expand All @@ -24,6 +22,12 @@ var afcMkDirCmd = &cobra.Command{
},
}

var mkDir string

func initMkDir() {
afcRootCMD.AddCommand(afcMkDirCmd)
afcMkDirCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcMkDirCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcMkDirCmd.Flags().StringVarP(&mkDir,"folder", "f","", "mkdir directory path")
afcMkDirCmd.MarkFlagRequired("folder")
}
20 changes: 11 additions & 9 deletions cmd/afcUtil/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ var afcPullCmd = &cobra.Command{
Short: "pull file or directory from device",
Long: "pull file or directory from device",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
fmt.Println("arguments error")
os.Exit(0)
}
if afcServer==nil {
getAFCServer()
}
pullOperate(afcServer, args[0], args[1])
fmt.Println(fmt.Sprintf("success,pull %s --> %s", args[0], args[1]))
afcServer:=getAFCServer()
pullOperate(afcServer, pullDevicePath, pullSaveLocalPath)
fmt.Println(fmt.Sprintf("success,pull %s --> %s", pullDevicePath, pullSaveLocalPath))
return nil
},
}

var pullDevicePath string
var pullSaveLocalPath string
func initPullCmd() {
afcRootCMD.AddCommand(afcPullCmd)
afcPullCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcPullCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcPullCmd.Flags().StringVarP(&pullDevicePath,"devicePath", "d","", "pull file or directory device path")
afcPullCmd.Flags().StringVarP(&pullSaveLocalPath,"localPath", "l","", "pull save file or directory to local path")
afcPullCmd.MarkFlagRequired("devicePath")
afcPullCmd.MarkFlagRequired("localPath")
}

func pullOperate(afc giDevice.Afc, devicePath string, localPath string) {
Expand Down
21 changes: 12 additions & 9 deletions cmd/afcUtil/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ var afcPushCmd = &cobra.Command{
Short: "push a file or directory to the device",
Long: "push a file or directory to the device",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
fmt.Println("parameter error")
os.Exit(0)
}
if afcServer==nil {
getAFCServer()
}
pushOperate(afcServer, args[0], args[1])
fmt.Println(fmt.Sprintf("success,push %s --> %s", args[0], args[1]))
afcServer:=getAFCServer()
pushOperate(afcServer, pushLocalPath, pushSaveDevicePath)
fmt.Println(fmt.Sprintf("success,push %s --> %s", pushLocalPath, pushSaveDevicePath))

return nil
},
}

var pushLocalPath string
var pushSaveDevicePath string

func initPush() {
afcRootCMD.AddCommand(afcPushCmd)
afcPushCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcPushCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcPushCmd.Flags().StringVarP(&pushLocalPath,"localPath", "l","", "push file or directory local path")
afcPushCmd.Flags().StringVarP(&pushSaveDevicePath,"devicePath", "d","", "push save file or directory to device path")
afcPushCmd.MarkFlagRequired("localPath")
afcPushCmd.MarkFlagRequired("devicePath")
}

func pushFile(afc giDevice.Afc, localPath string, devicePath string) {
Expand Down
12 changes: 8 additions & 4 deletions cmd/afcUtil/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ var afcRMCmd = &cobra.Command{
Short: "delete file",
Long: "delete file",
RunE: func(cmd *cobra.Command, args []string) error {
if afcServer==nil {
getAFCServer()
}
err := (afcServer).Remove(args[0])
afcServer:=getAFCServer()
err := (afcServer).Remove(rmFilePath)
if err != nil {
fmt.Println(err)
os.Exit(0)
Expand All @@ -24,6 +22,12 @@ var afcRMCmd = &cobra.Command{
},
}

var rmFilePath string

func initRM() {
afcRootCMD.AddCommand(afcRMCmd)
afcRMCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcRMCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcRMCmd.Flags().StringVarP(&rmFilePath,"file","f","","the address of the file to be deleted")
afcRMCmd.MarkFlagRequired("file")
}
13 changes: 9 additions & 4 deletions cmd/afcUtil/rmtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ var afcRMTreeCmd = &cobra.Command{
Short: "recursively delete all files in a directory",
Long: "recursively delete all files in a directory",
RunE: func(cmd *cobra.Command, args []string) error {
if afcServer==nil {
getAFCServer()
}
removeTree(afcServer,args[0])
afcServer:=getAFCServer()
removeTree(afcServer,rmDir)
fmt.Println("success")
return nil
},
}

var rmDir string

func initRMTree() {
afcRootCMD.AddCommand(afcRMTreeCmd)
afcRMTreeCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcRMTreeCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcRMTreeCmd.Flags().StringVarP(&rmDir,"folder","f","","folder address to delete")
afcRMTreeCmd.MarkFlagRequired("folder")
}

func removeTree(afc giDevice.Afc, devicePath string) {
Expand Down
12 changes: 8 additions & 4 deletions cmd/afcUtil/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ var afcStatCmd = &cobra.Command{
Short: "view file details",
Long: "view file details",
RunE: func(cmd *cobra.Command, args []string) error {
if afcServer==nil {
getAFCServer()
}
info, err := (afcServer).Stat(args[0])
afcServer:=getAFCServer()
info, err := (afcServer).Stat(statPath)
if err != nil {
os.Exit(0)
}
Expand All @@ -30,6 +28,12 @@ var afcStatCmd = &cobra.Command{
},
}

var statPath string

func initStat() {
afcRootCMD.AddCommand(afcStatCmd)
afcStatCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcStatCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcStatCmd.Flags().StringVarP(&statPath,"path","p","","files or folders for which details need to be viewed")
afcStatCmd.MarkFlagRequired("path")
}
19 changes: 12 additions & 7 deletions cmd/afcUtil/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ var afcTreeCmd = &cobra.Command{
Short: "tree structure view directory",
Long: "tree structure view directory",
RunE: func(cmd *cobra.Command, args []string) error {
if afcServer==nil {
getAFCServer()
}
showTree(afcServer, args[0],100)
afcServer:=getAFCServer()
showTree(afcServer, treeDir,100)
return nil
},
}

var treeDir string

func initTree() {
afcRootCMD.AddCommand(afcTreeCmd)
afcTreeCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )")
afcTreeCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId")
afcTreeCmd.Flags().StringVarP(&treeDir,"folder","f","","folder path to tree view")
afcTreeCmd.MarkFlagRequired("folder")
}

var (
levelFlag []bool // 路径级别标志
fileCount,
Expand Down Expand Up @@ -101,6 +109,3 @@ func buildPrefix(level int) string {
return result
}

func initTree() {
afcRootCMD.AddCommand(afcTreeCmd)
}

0 comments on commit 427d618

Please sign in to comment.