test cobra

This commit is contained in:
wangchuxiao
2023-03-07 17:59:34 +08:00
parent d6704aed80
commit ea614167a6
6 changed files with 125 additions and 92 deletions
+9 -16
View File
@@ -19,22 +19,24 @@ var startCmd = &cobra.Command{
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetInt(constant.FlagPort)
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
if err := run(configFolderPath, port); err != nil {
if err := run(port); err != nil {
panic(err.Error())
}
},
}
func init() {
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)
if err := startCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run(configFolderPath string, port int) error {
if err := config.InitConfig(configFolderPath); err != nil {
return err
}
func run(port int) error {
if port == 0 {
port = config.Config.Api.GinPort[0]
}
@@ -52,12 +54,3 @@ func run(configFolderPath string, port int) error {
}
return nil
}
func main() {
rootCmd := cmd.NewRootCmd()
rootCmd.AddCommand(startCmd)
if err := startCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}