test cobra

This commit is contained in:
wangchuxiao 2023-03-07 20:31:40 +08:00
parent c1746fb32e
commit 2c91185102
3 changed files with 54 additions and 7 deletions

View File

@ -93,15 +93,16 @@ var clearCmd = &cobra.Command{
} }
func main() { func main() {
cmd.RootCmd.PersistentFlags().StringP("userID", "u", "", "openIM userID") rootCmd := cmd.NewMsgUtilsCmd()
cmd.RootCmd.PersistentFlags().StringP("groupID", "u", "", "openIM superGroupID") rootCmd.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
rootCmd.Command.PersistentFlags().StringP("groupID", "u", "", "openIM superGroupID")
seqCmd.Flags().BoolP("fixAll", "c", false, "openIM fix all seqs") seqCmd.Flags().BoolP("fixAll", "c", false, "openIM fix all seqs")
msgCmd.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs") msgCmd.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs")
cmd.RootCmd.AddCommand(getCmd, fixCmd, clearCmd)
getCmd.AddCommand(seqCmd, msgCmd) getCmd.AddCommand(seqCmd, msgCmd)
fixCmd.AddCommand(seqCmd) fixCmd.AddCommand(seqCmd)
clearCmd.AddCommand(msgCmd) clearCmd.AddCommand(msgCmd)
if err := cmd.RootCmd.Execute(); err != nil { rootCmd.AddCommand(getCmd, fixCmd, clearCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }

42
pkg/common/cmd/msg.go Normal file
View File

@ -0,0 +1,42 @@
package cmd
type MsgUtilsCmd struct {
RootCmd
userID string
userIDFlag bool
superGroupID string
superGroupIDFlag bool
clearAll bool
clearAllFlag bool
fixAll bool
fixAllFlag bool
}
func NewMsgUtilsCmd() MsgUtilsCmd {
return MsgUtilsCmd{RootCmd: NewRootCmd()}
}
func (m *MsgUtilsCmd) AddUserIDFlag() {
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
m.userIDFlag = true
}
func (m *MsgUtilsCmd) getUserIDFlag() {
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
}
func (m *MsgUtilsCmd) AddGroupIDFlag() {
m.Command.PersistentFlags().StringP("super-groupID", "u", "", "openIM superGroupID")
}
func (m *MsgUtilsCmd) AddClearAllFlag() {
m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs")
}
func (m *MsgUtilsCmd) AddFixAllFlag() {
m.Command.PersistentFlags().BoolP("fixAll", "c", false, "openIM fix all seqs")
}

View File

@ -27,7 +27,7 @@ func NewRootCmd() RootCmd {
rootCmd.port = rootCmd.getPortFlag(cmd) rootCmd.port = rootCmd.getPortFlag(cmd)
} }
if rootCmd.prometheusPortFlag { if rootCmd.prometheusPortFlag {
rootCmd.prometheusPort = rootCmd.GetPrometheusPortFlag(cmd) rootCmd.prometheusPort = rootCmd.getPrometheusPortFlag(cmd)
} }
return rootCmd.getConfFromCmdAndInit(cmd) return rootCmd.getConfFromCmdAndInit(cmd)
} }
@ -47,7 +47,7 @@ func (r *RootCmd) init() {
} }
func (r *RootCmd) AddPortFlag() { func (r *RootCmd) AddPortFlag() {
r.Command.Flags().StringP(constant.FlagPort, "p", "", "server listen port") r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
r.portFlag = true r.portFlag = true
} }
@ -65,7 +65,7 @@ func (r *RootCmd) AddPrometheusPortFlag() {
r.prometheusPortFlag = true r.prometheusPortFlag = true
} }
func (r *RootCmd) GetPrometheusPortFlag(cmd *cobra.Command) int { func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
port, _ := cmd.Flags().GetInt(constant.PrometheusPort) port, _ := cmd.Flags().GetInt(constant.PrometheusPort)
return port return port
} }
@ -78,3 +78,7 @@ func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
func (r *RootCmd) Execute() error { func (r *RootCmd) Execute() error {
return r.Command.Execute() return r.Command.Execute()
} }
func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
r.Command.AddCommand(cmds...)
}