Skip to content

Commit

Permalink
fix: fix the flag parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
luhaoling committed Nov 7, 2023
1 parent af516d5 commit c8e3c50
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 101 deletions.
22 changes: 2 additions & 20 deletions cmd/api/admin-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"flag"
"fmt"
"github.com/OpenIMSDK/chat/pkg/discovery_register"
"math/rand"
Expand All @@ -41,28 +40,11 @@ import (

func main() {
rand.Seed(time.Now().UnixNano())

configFile, err := config.FindConfigPath()
configFile, ginPort, hide, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
}

//var configFile string
//flag.StringVar(&configFile, "config_folder_path", "../../config/config.yaml", "Config full path")

var ginPort int

flag.IntVar(&ginPort, "port", 10009, "get ginServerPort from cmd")

var hide bool
flag.BoolVar(&hide, "hide", false, "hide the ComponentCheck result")

// Version flag
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "show version and exit")

flag.Parse()

// Check if the version flag was set
if showVersion {
ver := version.Get()
Expand All @@ -74,7 +56,7 @@ func main() {
fmt.Println("Platform:", ver.Platform)
return
}
flag.Parse()

err = component.ComponentCheck(configFile, hide)
if err != nil {
return
Expand Down
21 changes: 5 additions & 16 deletions cmd/api/chat-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,11 @@ func init() {
}

func main() {
var configFile string
//flag.StringVar(&configFile, "config_folder_path", "../../../../../config/config.yaml", "Config full path:")
flag.StringVar(&configFile, "config_folder_path", "config/config.yaml", "Config full path")

// defaultPorts := config.Config.ChatApi.GinPort
var ginPort int
flag.IntVar(&ginPort, "port", 10008, "get ginServerPort from cmd")

var hide bool
flag.BoolVar(&hide, "hide", true, "hide the ComponentCheck result")

// Version flag
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "show version and exit")

flag.Parse()
configFile, ginPort, hide, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
}

// Check if the version flag was set
if showVersion {
Expand All @@ -78,7 +67,7 @@ func main() {

flag.Parse()

err := component.ComponentCheck(configFile, hide)
err = component.ComponentCheck(configFile, hide)
if err != nil {
return
}
Expand Down
15 changes: 1 addition & 14 deletions cmd/rpc/admin-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,11 @@ import (
func main() {
rand.Seed(time.Now().UnixNano())

configFile, err := config.FindConfigPath()
configFile, rpcPort, hide, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
}

var rpcPort int

flag.IntVar(&rpcPort, "port", 30200, "get rpc ServerPort from cmd")

var hide bool
flag.BoolVar(&hide, "hide", true, "hide the ComponentCheck result")

// Version flag
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "show version and exit")

flag.Parse()

// Check if the version flag was set
if showVersion {
ver := version.Get()
Expand Down
23 changes: 5 additions & 18 deletions cmd/rpc/chat-rpc/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"flag"
"fmt"
"math/rand"
"time"
Expand All @@ -17,22 +16,10 @@ import (

func main() {
rand.Seed(time.Now().UnixNano())

var configFile string
//flag.StringVar(&configFile, "config_folder_path", "../../../../../config/config.yaml", "Config full path")
flag.StringVar(&configFile, "config_folder_path", "config/config.yaml", "Config full path")

var rpcPort int
flag.IntVar(&rpcPort, "port", 30300, "get rpc ServerPort from cmd")

var hide bool
flag.BoolVar(&hide, "hide", true, "hide the ComponentCheck result")

// Version flag
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "show version and exit")

flag.Parse()
configFile, rpcPort, hide, showVersion, err := config.FlagParse()
if err != nil {
panic(err)
}

// Check if the version flag was set
if showVersion {
Expand All @@ -46,7 +33,7 @@ func main() {
return
}

err := component.ComponentCheck(configFile, hide)
err = component.ComponentCheck(configFile, hide)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mysql:
# maxLifeTime: 60 #连接可以重复使用的最长时间(秒)
# logLevel: 4 #日志级别 1=slient 2=error 3=warn 4=info
# slowThreshold: 500 #慢语句阈值 (毫秒)
database: openim_enterprise
# database: openim_enterprise

# 没有配置表示和OpenIM一致
log:
Expand Down
30 changes: 25 additions & 5 deletions pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,9 @@ func CreateCatalogPath() []string {
return []string{Constant.ConfigPath1, Constant.ConfigPath2}
}

func FindConfigPath() (string, error) {
var configFile string
func findConfigPath(configFile string) (string, error) {
path := make([]string, 10)
flag.StringVar(&configFile, "config_folder_path", "", "Config full path")
flag.Parse()

// First, check the configFile argument
if configFile != "" {
if _, err := findConfigFile([]string{configFile}); err != nil {
Expand All @@ -216,7 +214,6 @@ func FindConfigPath() (string, error) {

// Second, check for OPENIMCONFIG environment variable
envConfigPath := os.Getenv(Constant.OpenIMConfig)
fmt.Println("Env")
if envConfigPath != "" {
if _, err := findConfigFile([]string{envConfigPath}); err != nil {
return "", errors.New("the environment path config path is error")
Expand All @@ -234,3 +231,26 @@ func FindConfigPath() (string, error) {
// Forth, use the Default path.
return Constant.Default, nil
}

func FlagParse() (string, int, bool, bool, error) {
var configFile string
flag.StringVar(&configFile, "config_folder_path", "", "Config full path")

var ginPort int
flag.IntVar(&ginPort, "port", 10009, "get ginServerPort from cmd")

var hide bool
flag.BoolVar(&hide, "hide", false, "hide the ComponentCheck result")

// Version flag
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "show version and exit")

flag.Parse()

configFile, err := findConfigPath(configFile)
if err != nil {
return "", 0, false, false, err
}
return configFile, ginPort, hide, showVersion, nil
}
54 changes: 27 additions & 27 deletions pkg/common/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const (
ConfigPath1 = "../config/config.yaml"
ConfigPath2 = "../../../../../config/config.yaml"

OpenIMConfig = "OpenIMConfig" // 环境变量
OpenIMConfig = "OpenIMConfig" // environment variables
Default = "../../../config/config.yaml"
)

const (
// verificationCode used for.
VerificationCodeForRegister = 1 // 注册
VerificationCodeForResetPassword = 2 // 重置密码
VerificationCodeForLogin = 3 // 登录
VerificationCodeForRegister = 1 // Register
VerificationCodeForResetPassword = 2 // Reset password
VerificationCodeForLogin = 3 // Login

VerificationCodeForRegisterSuffix = "_forRegister"
VerificationCodeForResetSuffix = "_forReset"
Expand Down Expand Up @@ -65,8 +65,8 @@ const (

// AddFriendCtrl.
const (
OrdinaryUserAddFriendEnable = 1 // 允许普通用户添加好友
OrdinaryUserAddFriendDisable = -1 // 不允许普通用户添加好友
OrdinaryUserAddFriendEnable = 1 // Allow ordinary users to add friends
OrdinaryUserAddFriendDisable = -1 // Do not allow ordinary users to add friends
)

// minioUpload.
Expand All @@ -83,15 +83,15 @@ const (
)

const (
ScreenInvitationRegisterAll = 0 // 全部
ScreenInvitationRegisterUsed = 1 // 已使用
ScreenInvitationRegisterNotUsed = 2 // 未使用
ScreenInvitationRegisterAll = 0 // All
ScreenInvitationRegisterUsed = 1 // Used
ScreenInvitationRegisterNotUsed = 2 // Unused
)

// 1 block; 2 unblock.
const (
UserBlock = 1 // 封号
UserUnblock = 2 // 解封
UserBlock = 1 // Account ban
UserUnblock = 2 // Unban
)

const (
Expand Down Expand Up @@ -125,14 +125,14 @@ const (
)

const (
NotNeedInvitationCodeRegister = 0 // 不需要邀请码
NeedInvitationCodeRegister = 1 // 需要邀请码
NotNeedInvitationCodeRegister = 0 // No invitation code required
NeedInvitationCodeRegister = 1 // Invitation code required
)

// 小程序.
// mini-app
const (
StatusOnShelf = 1 // 上架
StatusUnShelf = 2 // 下架
StatusOnShelf = 1 // OnShelf
StatusUnShelf = 2 // UnShelf
)

const (
Expand All @@ -141,22 +141,22 @@ const (
)

const (
LimitNil = 0 //
LimitEmpty = 1 // 都不限制
LimitOnlyLoginIP = 2 // 仅限制登录
LimitOnlyRegisterIP = 3 // 仅限制注册
LimitLoginIP = 4 // 限制登录
LimitRegisterIP = 5 // 限制注册
LimitLoginRegisterIP = 6 // 限制登录注册
LimitNil = 0 // None
LimitEmpty = 1 // Neither are restricted
LimitOnlyLoginIP = 2 // Only login is restricted
LimitOnlyRegisterIP = 3 // Only registration is restricted
LimitLoginIP = 4 // Restrict login
LimitRegisterIP = 5 // Restrict registration
LimitLoginRegisterIP = 6 // Restrict both login and registration
)

const (
InvitationCodeAll = 0 // 全部
InvitationCodeUsed = 1 // 已使用
InvitationCodeUnused = 2 // 未使用
InvitationCodeAll = 0 // All
InvitationCodeUsed = 1 // Used
InvitationCodeUnused = 2 // Unused
)

// 默认发现页面.
// Default discovery page
const DefaultDiscoverPageURL = "https://doc.rentsoft.cn/#/"

// const OperationID = "operationID"
Expand Down

0 comments on commit c8e3c50

Please sign in to comment.