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

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)