From 6eb13df2e2c1366e925ad27c1e57708f52e50345 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Tue, 7 Mar 2023 18:06:57 +0800 Subject: [PATCH] test cobra --- cmd/api/main.go | 2 +- pkg/common/cmd/root.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/api/main.go b/cmd/api/main.go index 197936fc4..57f404aa4 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -30,7 +30,7 @@ func main() { startCmd.Flags().IntP(constant.FlagPort, "p", 0, "Port to listen on") startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") rootCmd := cmd.NewRootCmd() - cobra.Command(rootCmd).AddCommand(startCmd) + rootCmd.Command.AddCommand(startCmd) if err := startCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index d7b3960d5..f9af56a49 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -6,7 +6,9 @@ import ( "github.com/spf13/cobra" ) -type RootCmd cobra.Command +type RootCmd struct { + Command cobra.Command +} func NewRootCmd() RootCmd { c := cobra.Command{ @@ -18,11 +20,13 @@ func NewRootCmd() RootCmd { }, } c.Flags() - return RootCmd(c) + rootCmd := RootCmd{Command: c} + rootCmd.init() + return rootCmd } -func (r RootCmd) Init() { - cobra.Command(r).Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") +func (r RootCmd) init() { + r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") } func getConfFromCmdAndInit(cmdLines *cobra.Command) error {