add install cmd help

This commit is contained in:
lyyyuna 2021-07-21 17:06:58 +08:00
parent fa844a5f8d
commit 81abc49af2
6 changed files with 42 additions and 18 deletions

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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

View File

@ -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. 展示元信息

View File

@ -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

View File

@ -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 {
if cmdType == GO_BUILD {
printHelp(buildUsage, cmd)
} else if cmdType == GO_INSTALL {
printHelp(installUsage, cmd)
}
os.Exit(0)
}
// 删除 help flag
args = findAndDelHelpFlag(args)