From 81abc49af21750c5c82422570849e15ba7fd7ba1 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Wed, 21 Jul 2021 17:06:58 +0800 Subject: [PATCH] add install cmd help --- cmd/build.go | 9 ++++++--- cmd/install.go | 9 ++++++--- cmd/root.go | 2 +- pkg/build/build.go | 9 +++------ pkg/build/install.go | 5 ++--- pkg/flag/build_flags.go | 26 ++++++++++++++++++++++++-- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 1aab87c..0965da1 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/qiniu/goc/v2/pkg/build" "github.com/qiniu/goc/v2/pkg/config" + "github.com/qiniu/goc/v2/pkg/flag" "github.com/spf13/cobra" ) @@ -14,12 +15,14 @@ var buildCmd = &cobra.Command{ } func init() { - buildCmd.Flags().StringVarP(&config.GocConfig.Mode, "mode", "", "count", "coverage mode: set, count, atomic, watch") - buildCmd.Flags().StringVarP(&config.GocConfig.Host, "host", "", "127.0.0.1:7777", "specify the host of the goc sever") + buildCmd.Flags().StringVarP(&config.GocConfig.Mode, "gocmode", "", "count", "coverage mode: set, count, atomic, watch") + buildCmd.Flags().StringVarP(&config.GocConfig.Host, "gochost", "", "127.0.0.1:7777", "specify the host of the goc sever") rootCmd.AddCommand(buildCmd) } func buildAction(cmd *cobra.Command, args []string) { - b := build.NewBuild(cmd, args) + // 1. 解析 goc 命令行和 go 命令行 + remainedArgs := flag.BuildCmdArgsParse(cmd, args, flag.GO_BUILD) + b := build.NewBuild(remainedArgs) b.Build() } diff --git a/cmd/install.go b/cmd/install.go index d027474..7396080 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/qiniu/goc/v2/pkg/build" "github.com/qiniu/goc/v2/pkg/config" + "github.com/qiniu/goc/v2/pkg/flag" "github.com/spf13/cobra" ) @@ -14,12 +15,14 @@ var installCmd = &cobra.Command{ } func init() { - installCmd.Flags().StringVarP(&config.GocConfig.Mode, "mode", "", "count", "coverage mode: set, count, atomic, watch") - installCmd.Flags().StringVarP(&config.GocConfig.Host, "host", "", "127.0.0.1:7777", "specify the host of the goc sever") + installCmd.Flags().StringVarP(&config.GocConfig.Mode, "gocmode", "", "count", "coverage mode: set, count, atomic, watch") + installCmd.Flags().StringVarP(&config.GocConfig.Host, "gochost", "", "127.0.0.1:7777", "specify the host of the goc sever") rootCmd.AddCommand(installCmd) } func installAction(cmd *cobra.Command, args []string) { - b := build.NewInstall(cmd, args) + // 1. 解析 goc 命令行和 go 命令行 + remainedArgs := flag.BuildCmdArgsParse(cmd, args, flag.GO_INSTALL) + b := build.NewInstall(remainedArgs) b.Install() } diff --git a/cmd/root.go b/cmd/root.go index 36955b3..6e01b28 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,7 +39,7 @@ Find more information at: } func init() { - rootCmd.PersistentFlags().BoolVar(&config.GocConfig.Debug, "debug", false, "run goc in debug mode") + rootCmd.PersistentFlags().BoolVar(&config.GocConfig.Debug, "gocdebug", false, "run goc in debug mode") } // Execute the goc tool diff --git a/pkg/build/build.go b/pkg/build/build.go index f421978..e1202e7 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -8,7 +8,6 @@ import ( "github.com/qiniu/goc/v2/pkg/cover" "github.com/qiniu/goc/v2/pkg/flag" "github.com/qiniu/goc/v2/pkg/log" - "github.com/spf13/cobra" ) // Build struct a build @@ -18,13 +17,11 @@ type Build struct { // NewBuild creates a Build struct // -// consumes args, get package dirs, read project meta info. -func NewBuild(cmd *cobra.Command, args []string) *Build { +func NewBuild(args []string) *Build { b := &Build{} - // 1. 解析 goc 命令行和 go 命令行 - remainedArgs := flag.BuildCmdArgsParse(cmd, args) + // 2. 解析 go 包位置 - flag.GetPackagesDir(remainedArgs) + flag.GetPackagesDir(args) // 3. 读取工程元信息:go.mod, pkgs list ... b.readProjectMetaInfo() // 4. 展示元信息 diff --git a/pkg/build/install.go b/pkg/build/install.go index 96bc3a7..a2484f2 100644 --- a/pkg/build/install.go +++ b/pkg/build/install.go @@ -7,11 +7,10 @@ import ( "github.com/qiniu/goc/v2/pkg/config" "github.com/qiniu/goc/v2/pkg/cover" "github.com/qiniu/goc/v2/pkg/log" - "github.com/spf13/cobra" ) -func NewInstall(cmd *cobra.Command, args []string) *Build { - return NewBuild(cmd, args) +func NewInstall(args []string) *Build { + return NewBuild(args) } // Install starts go install diff --git a/pkg/flag/build_flags.go b/pkg/flag/build_flags.go index ee88900..5f5ea0f 100644 --- a/pkg/flag/build_flags.go +++ b/pkg/flag/build_flags.go @@ -14,15 +14,31 @@ import ( var buildUsage string = `Usage: goc build [-o output] [build flags] [packages] [goc flags] +[build flags] are same with go official command, you can copy them here directly. + The [goc flags] can be placed in anywhere in the command line. However, other flags' order are same with the go official command. ` +var installUsage string = `Usage: +goc install [-o output] [build flags] [packages] [goc flags] + +[build flags] are same with go official command, you can copy them here directly. + +The [goc flags] can be placed in anywhere in the command line. +However, other flags' order are same with the go official command. +` + +const ( + GO_BUILD = iota + GO_INSTALL +) + // BuildCmdArgsParse parse both go flags and goc flags, it rewrite go flags if // necessary, and returns all non-flag arguments. // // 吞下 [packages] 之前所有的 flags. -func BuildCmdArgsParse(cmd *cobra.Command, args []string) []string { +func BuildCmdArgsParse(cmd *cobra.Command, args []string, cmdType int) []string { // 首先解析 cobra 定义的 flag allFlagSets := cmd.Flags() // 因为 args 里面含有 go 的 flag,所以需要忽略解析 go flag 的错误 @@ -33,7 +49,13 @@ func BuildCmdArgsParse(cmd *cobra.Command, args []string) []string { helpFlag := allFlagSets.Lookup("help") if helpFlag.Changed { - printHelp(buildUsage, cmd) + if cmdType == GO_BUILD { + printHelp(buildUsage, cmd) + } else if cmdType == GO_INSTALL { + printHelp(installUsage, cmd) + } + + os.Exit(0) } // 删除 help flag args = findAndDelHelpFlag(args)