Merge pull request #203 from lyyyuna/v2

增加 install 的 help
This commit is contained in:
Li Yiyang 2021-07-21 17:11:59 +08:00 committed by GitHub
commit 2c2a70f7f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 208 additions and 22 deletions

View File

@ -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 ```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. 日志优化 #### 3. 日志优化
@ -55,7 +62,7 @@ goc build -o /home/app . --debug # 非法
#### 5. watch 模式 #### 5. watch 模式
当使用 `goc build --mode watch .` 编译后,被测服务任何覆盖率变化都将实时推送到 goc server。 当使用 `goc build --gocmode watch .` 编译后,被测服务任何覆盖率变化都将实时推送到 goc server。
用户可以使用该 websocket 连接 `ws://[goc_server_host]/cover/ws/watch` 观察到被测服务的新触发代码块,推送信息格式如下: 用户可以使用该 websocket 连接 `ws://[goc_server_host]/cover/ws/watch` 观察到被测服务的新触发代码块,推送信息格式如下:

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"github.com/qiniu/goc/v2/pkg/build" "github.com/qiniu/goc/v2/pkg/build"
"github.com/qiniu/goc/v2/pkg/config" "github.com/qiniu/goc/v2/pkg/config"
"github.com/qiniu/goc/v2/pkg/flag"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -14,12 +15,14 @@ var buildCmd = &cobra.Command{
} }
func init() { func init() {
buildCmd.Flags().StringVarP(&config.GocConfig.Mode, "mode", "", "count", "coverage mode: set, count, atomic, watch") buildCmd.Flags().StringVarP(&config.GocConfig.Mode, "gocmode", "", "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.Host, "gochost", "", "127.0.0.1:7777", "specify the host of the goc sever")
rootCmd.AddCommand(buildCmd) rootCmd.AddCommand(buildCmd)
} }
func buildAction(cmd *cobra.Command, args []string) { 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() b.Build()
} }

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"github.com/qiniu/goc/v2/pkg/build" "github.com/qiniu/goc/v2/pkg/build"
"github.com/qiniu/goc/v2/pkg/config" "github.com/qiniu/goc/v2/pkg/config"
"github.com/qiniu/goc/v2/pkg/flag"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -14,12 +15,14 @@ var installCmd = &cobra.Command{
} }
func init() { func init() {
installCmd.Flags().StringVarP(&config.GocConfig.Mode, "mode", "", "count", "coverage mode: set, count, atomic, watch") installCmd.Flags().StringVarP(&config.GocConfig.Mode, "gocmode", "", "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.Host, "gochost", "", "127.0.0.1:7777", "specify the host of the goc sever")
rootCmd.AddCommand(installCmd) rootCmd.AddCommand(installCmd)
} }
func installAction(cmd *cobra.Command, args []string) { 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() b.Install()
} }

View File

@ -39,7 +39,7 @@ Find more information at:
} }
func init() { 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 // Execute the goc tool

49
doc/goc_diff.drawio Normal file
View File

@ -0,0 +1,49 @@
<mxfile>
<diagram id="k9NB9guo-TzxMCc_p6Qm" name="diff design">
<mxGraphModel dx="888" dy="574" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" value="goc diff" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="160" y="440" width="90" height="40" as="geometry"/>
</mxCell>
<mxCell id="3" value="local store" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="160" y="320" width="90" height="40" as="geometry"/>
</mxCell>
<mxCell id="4" value="s3 store" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="324" y="440" width="90" height="40" as="geometry"/>
</mxCell>
<mxCell id="5" value="" style="endArrow=classic;startArrow=classic;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="1" source="2" target="3">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="390" y="370" as="sourcePoint"/>
<mxPoint x="440" y="320" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="6" value="" style="endArrow=classic;startArrow=classic;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="2" target="4">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="390" y="370" as="sourcePoint"/>
<mxPoint x="440" y="320" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="8" value="" style="endArrow=none;dashed=1;html=1;dashPattern=1 3;strokeWidth=12;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="390" y="360" as="sourcePoint"/>
<mxPoint x="390" y="130" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="9" value="" style="endArrow=classic;html=1;strokeWidth=2;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="530" y="300" as="sourcePoint"/>
<mxPoint x="400" y="300" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="10" value="base" style="edgeLabel;resizable=0;html=1;align=center;verticalAlign=middle;" connectable="0" vertex="1" parent="9">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="12" value="store 存储 base 分支 commit 的profile" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;" vertex="1" parent="1">
<mxGeometry x="440" y="310" width="220" height="20" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

106
doc/protocol.drawio Normal file
View File

@ -0,0 +1,106 @@
<mxfile>
<diagram id="D3htiJm5t3r9G7ztMOgi" name="protocol design">
<mxGraphModel dx="969" dy="574" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" value="goc server v1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="140" y="170" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="3" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="520" y="170" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="4" value="http agent 1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="525" y="190" width="100" height="20" as="geometry"/>
</mxCell>
<mxCell id="5" value="" style="endArrow=classic;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="2">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="420" y="310" as="sourcePoint"/>
<mxPoint x="510" y="200" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="6" value="" style="endArrow=none;html=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="420" y="310" as="sourcePoint"/>
<mxPoint x="420" y="90" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="7" value="NAT" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="440" y="80" width="40" height="20" as="geometry"/>
</mxCell>
<mxCell id="8" value="goc server v2" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="140" y="450" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="9" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="520" y="450" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="10" value="agent 1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="500" y="470" width="100" height="20" as="geometry"/>
</mxCell>
<mxCell id="12" value="" style="endArrow=none;html=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="420" y="590" as="sourcePoint"/>
<mxPoint x="420" y="370" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="13" value="NAT" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="440" y="360" width="40" height="20" as="geometry"/>
</mxCell>
<mxCell id="14" value="" style="endArrow=classic;startArrow=classic;html=1;" edge="1" parent="1" target="10">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="270" y="480" as="sourcePoint"/>
<mxPoint x="470" y="430" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="15" value="rpc client" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="155" y="530" width="90" height="20" as="geometry"/>
</mxCell>
<mxCell id="16" value="rpc server" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="535" y="530" width="90" height="20" as="geometry"/>
</mxCell>
<mxCell id="17" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="520" y="570" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="18" value="agent 2" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="500" y="590" width="100" height="20" as="geometry"/>
</mxCell>
<mxCell id="19" value="rpc server" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="535" y="650" width="90" height="20" as="geometry"/>
</mxCell>
<mxCell id="20" value="websocket server" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="140" y="410" width="120" height="20" as="geometry"/>
</mxCell>
<mxCell id="21" value="websocket client" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="520" y="410" width="120" height="20" as="geometry"/>
</mxCell>
<mxCell id="22" value="http 1.1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="310" y="180" width="60" height="20" as="geometry"/>
</mxCell>
<mxCell id="23" value="websocket&amp;nbsp;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="310" y="460" width="60" height="20" as="geometry"/>
</mxCell>
<mxCell id="24" value="1. 需要 http agent 可访问&lt;br&gt;2. 需要 goc server 维护一个地址列表&lt;br&gt;3. 重启 client/server&amp;nbsp;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry y="160" width="130" height="150" as="geometry"/>
</mxCell>
<mxCell id="25" value="1. 不需要 http agent 暴露&lt;br&gt;2. 需要 goc server 维护一个 connection 字典&lt;br&gt;3. 重启 client/server&amp;nbsp;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry y="430" width="130" height="150" as="geometry"/>
</mxCell>
<mxCell id="26" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="520" y="250" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="27" value="http agent 2" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="525" y="270" width="100" height="20" as="geometry"/>
</mxCell>
<mxCell id="28" value="" style="endArrow=classic;html=1;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="340" y="260" as="sourcePoint"/>
<mxPoint x="420" y="200" as="targetPoint"/>
</mxGeometry>
</mxCell>
<mxCell id="29" value="端口映射" style="edgeLabel;resizable=0;html=1;align=center;verticalAlign=middle;" connectable="0" vertex="1" parent="28">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

View File

@ -8,7 +8,6 @@ import (
"github.com/qiniu/goc/v2/pkg/cover" "github.com/qiniu/goc/v2/pkg/cover"
"github.com/qiniu/goc/v2/pkg/flag" "github.com/qiniu/goc/v2/pkg/flag"
"github.com/qiniu/goc/v2/pkg/log" "github.com/qiniu/goc/v2/pkg/log"
"github.com/spf13/cobra"
) )
// Build struct a build // Build struct a build
@ -18,13 +17,11 @@ type Build struct {
// NewBuild creates a Build struct // NewBuild creates a Build struct
// //
// consumes args, get package dirs, read project meta info. func NewBuild(args []string) *Build {
func NewBuild(cmd *cobra.Command, args []string) *Build {
b := &Build{} b := &Build{}
// 1. 解析 goc 命令行和 go 命令行
remainedArgs := flag.BuildCmdArgsParse(cmd, args)
// 2. 解析 go 包位置 // 2. 解析 go 包位置
flag.GetPackagesDir(remainedArgs) flag.GetPackagesDir(args)
// 3. 读取工程元信息go.mod, pkgs list ... // 3. 读取工程元信息go.mod, pkgs list ...
b.readProjectMetaInfo() b.readProjectMetaInfo()
// 4. 展示元信息 // 4. 展示元信息

View File

@ -7,11 +7,10 @@ import (
"github.com/qiniu/goc/v2/pkg/config" "github.com/qiniu/goc/v2/pkg/config"
"github.com/qiniu/goc/v2/pkg/cover" "github.com/qiniu/goc/v2/pkg/cover"
"github.com/qiniu/goc/v2/pkg/log" "github.com/qiniu/goc/v2/pkg/log"
"github.com/spf13/cobra"
) )
func NewInstall(cmd *cobra.Command, args []string) *Build { func NewInstall(args []string) *Build {
return NewBuild(cmd, args) return NewBuild(args)
} }
// Install starts go install // Install starts go install

View File

@ -14,15 +14,31 @@ import (
var buildUsage string = `Usage: var buildUsage string = `Usage:
goc build [-o output] [build flags] [packages] [goc flags] 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. The [goc flags] can be placed in anywhere in the command line.
However, other flags' order are same with the go official command. 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 // BuildCmdArgsParse parse both go flags and goc flags, it rewrite go flags if
// necessary, and returns all non-flag arguments. // necessary, and returns all non-flag arguments.
// //
// 吞下 [packages] 之前所有的 flags. // 吞下 [packages] 之前所有的 flags.
func BuildCmdArgsParse(cmd *cobra.Command, args []string) []string { func BuildCmdArgsParse(cmd *cobra.Command, args []string, cmdType int) []string {
// 首先解析 cobra 定义的 flag // 首先解析 cobra 定义的 flag
allFlagSets := cmd.Flags() allFlagSets := cmd.Flags()
// 因为 args 里面含有 go 的 flag所以需要忽略解析 go flag 的错误 // 因为 args 里面含有 go 的 flag所以需要忽略解析 go flag 的错误
@ -33,7 +49,13 @@ func BuildCmdArgsParse(cmd *cobra.Command, args []string) []string {
helpFlag := allFlagSets.Lookup("help") helpFlag := allFlagSets.Lookup("help")
if helpFlag.Changed { if helpFlag.Changed {
if cmdType == GO_BUILD {
printHelp(buildUsage, cmd) printHelp(buildUsage, cmd)
} else if cmdType == GO_INSTALL {
printHelp(installUsage, cmd)
}
os.Exit(0)
} }
// 删除 help flag // 删除 help flag
args = findAndDelHelpFlag(args) args = findAndDelHelpFlag(args)