From b3d0a8de1b9336ffe5455760fe66593d979d1058 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Wed, 21 Jul 2021 16:35:06 +0800 Subject: [PATCH 1/3] add design --- doc/goc_diff.drawio | 49 ++++++++++++++++++++ doc/protocol.drawio | 106 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 doc/goc_diff.drawio create mode 100644 doc/protocol.drawio diff --git a/doc/goc_diff.drawio b/doc/goc_diff.drawio new file mode 100644 index 0000000..0161f48 --- /dev/null +++ b/doc/goc_diff.drawio @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/doc/protocol.drawio b/doc/protocol.drawio new file mode 100644 index 0000000..4a1f898 --- /dev/null +++ b/doc/protocol.drawio @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From fa844a5f8de23c91c51a4a52e716ec728c7027e0 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Wed, 21 Jul 2021 17:06:31 +0800 Subject: [PATCH 2/3] update readme --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ecec669..6e0edcf 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,19 @@ goc install ./app/... # ``` -由于 go 命令对 flags 和 args 的相对位置有着严格要求:`go build [-o output] [build flags] [packages]`,所以在指定 goc 自己的 flags (所有 goc flags 都是 `--` 开头)必须和 `build flags` 位置保持相同,即: +由于 go 命令对 flags 和 args 的相对位置有着严格要求:`go build [-o output] [build flags] [packages]`,所以 goc 命令中 `[build flags]` 的位置也类似。而 goc 自己的 flags 则相对灵活: ```bash -goc build --debug -o /home/app . # 合法 +goc build -o -ldflags 'foo=bar' ./app --gocdebug --gochost "127.0.0.1:7779" -goc build -o /home/app . --debug # 非法 +goc build --gocdebug -o -ldflags 'foo=bar' ./app --gochost "127.0.0.1:7779" +``` + +两者都对。如果不想最小化的改动 go 编译命令,那么 `--gochost` 可以不指定,在服务启动时指定坏境变量就能动态改变注册中心地址: + +``` +export GOC_CUSTOM_HOST="127.0.0.1:7779" +./agent ``` #### 3. 日志优化 @@ -55,7 +62,7 @@ goc build -o /home/app . --debug # 非法 #### 5. watch 模式 -当使用 `goc build --mode watch .` 编译后,被测服务任何覆盖率变化都将实时推送到 goc server。 +当使用 `goc build --gocmode watch .` 编译后,被测服务任何覆盖率变化都将实时推送到 goc server。 用户可以使用该 websocket 连接 `ws://[goc_server_host]/cover/ws/watch` 观察到被测服务的新触发代码块,推送信息格式如下: From 81abc49af21750c5c82422570849e15ba7fd7ba1 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Wed, 21 Jul 2021 17:06:58 +0800 Subject: [PATCH 3/3] 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)