test cobra

This commit is contained in:
wangchuxiao 2023-03-06 18:47:48 +08:00
parent 757fd4bf0b
commit 373b317b1a
2 changed files with 11 additions and 5 deletions

View File

@ -17,9 +17,9 @@ var startCmd = &cobra.Command{
Use: "start", Use: "start",
Short: "Start the server", Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetInt("port") port, _ := cmd.Flags().GetInt(constant.FlagPort)
configFolderPath, _ := cmd.Flags().GetString("config_folder_path") configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
fmt.Printf("Starting server on port %s with config file at %s\n", port, configFolderPath) fmt.Printf("Starting server on port %d with config file at %s\n", port, configFolderPath)
if err := run(configFolderPath, port); err != nil { if err := run(configFolderPath, port); err != nil {
panic(err.Error()) panic(err.Error())
} }
@ -27,8 +27,8 @@ var startCmd = &cobra.Command{
} }
func init() { func init() {
startCmd.Flags().IntP("port", "p", 10002, "Port to listen on") startCmd.Flags().IntP(constant.FlagPort, "p", 10002, "Port to listen on")
startCmd.Flags().StringP("config_path", "c", "", "Path to config file folder") startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
} }
func run(configFolderPath string, port int) error { func run(configFolderPath string, port int) error {

View File

@ -327,3 +327,9 @@ const LogFileName = "OpenIM.log"
const CurrentVersion = "v2.3.4-rc0" const CurrentVersion = "v2.3.4-rc0"
const LocalHost = "0.0.0.0" const LocalHost = "0.0.0.0"
// flag parse
const (
FlagPort = "port"
FlagConf = "config_folder_path"
)