diff --git a/cmd/AFC.go b/cmd/AFC.go deleted file mode 100644 index b8eb9ad..0000000 --- a/cmd/AFC.go +++ /dev/null @@ -1,30 +0,0 @@ -package cmd - -import ( - "github.com/SonicCloudOrg/sonic-ios-bridge/cmd/afcUtil" - "github.com/spf13/cobra" - "strings" -) - -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 { - return nil - }, -} - -var bundleID string - -func init() { - rootCmd.AddCommand(afcCmd) - afcCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber ( default first device )") - afcCmd.Flags().StringVarP(&bundleID, "bundleId", "b", "", "app bundleId") - afcCmd.SetUsageTemplate(strings.Replace( - afcCmd.UsageTemplate(), - "{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}\n {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}", - "sib afc [-h] [-b BUNDLE_ID] {ls,rm,cat,pull,push,stat,tree,rmtree,mkdir} arguments [arguments ...]", - 1)) - afcUtil.InitAfc(afcCmd,udid,bundleID) -} diff --git a/cmd/afc.go b/cmd/afc.go new file mode 100644 index 0000000..3b42c6f --- /dev/null +++ b/cmd/afc.go @@ -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) +} diff --git a/cmd/afcUtil/InitAFC.go b/cmd/afcUtil/afcInit.go similarity index 85% rename from cmd/afcUtil/InitAFC.go rename to cmd/afcUtil/afcInit.go index c499132..c6cf421 100644 --- a/cmd/afcUtil/InitAFC.go +++ b/cmd/afcUtil/afcInit.go @@ -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() @@ -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) @@ -53,4 +53,5 @@ func getAFCServer()() { fmt.Println(err) os.Exit(0) } + return afcServer } \ No newline at end of file diff --git a/cmd/afcUtil/cat.go b/cmd/afcUtil/cat.go index 15a4d9d..62d972a 100644 --- a/cmd/afcUtil/cat.go +++ b/cmd/afcUtil/cat.go @@ -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) { diff --git a/cmd/afcUtil/ls.go b/cmd/afcUtil/ls.go index bfffc8c..788ae58 100644 --- a/cmd/afcUtil/ls.go +++ b/cmd/afcUtil/ls.go @@ -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) { diff --git a/cmd/afcUtil/mkdir.go b/cmd/afcUtil/mkdir.go index 1979c19..9bf008e 100644 --- a/cmd/afcUtil/mkdir.go +++ b/cmd/afcUtil/mkdir.go @@ -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) @@ -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") } diff --git a/cmd/afcUtil/pull.go b/cmd/afcUtil/pull.go index c3fbf6b..42cd684 100644 --- a/cmd/afcUtil/pull.go +++ b/cmd/afcUtil/pull.go @@ -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) { diff --git a/cmd/afcUtil/push.go b/cmd/afcUtil/push.go index a0325a3..4aeb640 100644 --- a/cmd/afcUtil/push.go +++ b/cmd/afcUtil/push.go @@ -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) { diff --git a/cmd/afcUtil/rm.go b/cmd/afcUtil/rm.go index 5a29137..a3fa1d0 100644 --- a/cmd/afcUtil/rm.go +++ b/cmd/afcUtil/rm.go @@ -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) @@ -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") } diff --git a/cmd/afcUtil/rmtree.go b/cmd/afcUtil/rmtree.go index 4a1e45c..595ccb8 100644 --- a/cmd/afcUtil/rmtree.go +++ b/cmd/afcUtil/rmtree.go @@ -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) { diff --git a/cmd/afcUtil/stat.go b/cmd/afcUtil/stat.go index 5811c76..de5304d 100644 --- a/cmd/afcUtil/stat.go +++ b/cmd/afcUtil/stat.go @@ -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) } @@ -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") } diff --git a/cmd/afcUtil/tree.go b/cmd/afcUtil/tree.go index 240d095..38a2bfc 100644 --- a/cmd/afcUtil/tree.go +++ b/cmd/afcUtil/tree.go @@ -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, @@ -101,6 +109,3 @@ func buildPrefix(level int) string { return result } -func initTree() { - afcRootCMD.AddCommand(afcTreeCmd) -}