mirror of https://github.com/bjdgyc/anylink.git
修复配置文件默认值
This commit is contained in:
parent
88a3d35784
commit
35c6d80c8d
|
@ -31,6 +31,7 @@ var (
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
// LinkAddr string `json:"link_addr"`
|
// LinkAddr string `json:"link_addr"`
|
||||||
|
ConfFile string `json:"conf_file"`
|
||||||
ServerAddr string `json:"server_addr"`
|
ServerAddr string `json:"server_addr"`
|
||||||
ServerDTLSAddr string `json:"server_dtls_addr"`
|
ServerDTLSAddr string `json:"server_dtls_addr"`
|
||||||
ServerDTLS bool `json:"server_dtls"`
|
ServerDTLS bool `json:"server_dtls"`
|
||||||
|
@ -65,7 +66,7 @@ type ServerConfig struct {
|
||||||
MobileDpd int `json:"mobile_dpd"`
|
MobileDpd int `json:"mobile_dpd"`
|
||||||
|
|
||||||
SessionTimeout int `json:"session_timeout"` // in seconds
|
SessionTimeout int `json:"session_timeout"` // in seconds
|
||||||
AuthTimeout int `json:"auth_timeout"` // in seconds
|
// AuthTimeout int `json:"auth_timeout"` // in seconds
|
||||||
}
|
}
|
||||||
|
|
||||||
func initServerCfg() {
|
func initServerCfg() {
|
||||||
|
@ -127,6 +128,7 @@ func initCfg() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Cfg.ConfFile = cfgFile
|
||||||
initServerCfg()
|
initServerCfg()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,9 +152,9 @@ func ServerCfg2Slice() []SCfg {
|
||||||
value := s.Field(i)
|
value := s.Field(i)
|
||||||
tag := field.Tag.Get("json")
|
tag := field.Tag.Get("json")
|
||||||
usage, env := getUsageEnv(tag)
|
usage, env := getUsageEnv(tag)
|
||||||
if usage == "" {
|
// if usage == "" {
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
|
|
||||||
datas = append(datas, SCfg{Name: tag, Env: env, Info: usage, Data: value.Interface()})
|
datas = append(datas, SCfg{Name: tag, Env: env, Info: usage, Data: value.Interface()})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package base
|
package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -22,8 +23,8 @@ var (
|
||||||
secret bool
|
secret bool
|
||||||
// 显示版本信息
|
// 显示版本信息
|
||||||
rev bool
|
rev bool
|
||||||
// 获取env名称
|
// 输出debug信息
|
||||||
env bool
|
debug bool
|
||||||
|
|
||||||
// Used for flags.
|
// Used for flags.
|
||||||
runSrv bool
|
runSrv bool
|
||||||
|
@ -57,35 +58,21 @@ func init() {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cobra.OnInitialize(func() {
|
|
||||||
viper.SetConfigFile(cfgFile)
|
|
||||||
viper.AutomaticEnv()
|
|
||||||
|
|
||||||
if cfgFile == "" {
|
|
||||||
// 没有配置文件,不做处理
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := viper.ReadInConfig()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Using config file:", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
viper.SetEnvPrefix("link")
|
viper.SetEnvPrefix("link")
|
||||||
|
|
||||||
// 基础配置
|
// 基础配置
|
||||||
rootCmd.Flags().StringVarP(&cfgFile, "conf", "c", "", "config file")
|
rootCmd.Flags().StringVarP(&cfgFile, "conf", "c", "./conf/server.toml", "config file")
|
||||||
|
_ = viper.BindEnv("conf")
|
||||||
|
|
||||||
for _, v := range configs {
|
for _, v := range configs {
|
||||||
if v.Typ == cfgStr {
|
if v.Typ == cfgStr {
|
||||||
rootCmd.Flags().String(v.Name, v.ValStr, v.Usage)
|
rootCmd.Flags().StringP(v.Name, v.Short, v.ValStr, v.Usage)
|
||||||
}
|
}
|
||||||
if v.Typ == cfgInt {
|
if v.Typ == cfgInt {
|
||||||
rootCmd.Flags().Int(v.Name, v.ValInt, v.Usage)
|
rootCmd.Flags().IntP(v.Name, v.Short, v.ValInt, v.Usage)
|
||||||
}
|
}
|
||||||
if v.Typ == cfgBool {
|
if v.Typ == cfgBool {
|
||||||
rootCmd.Flags().Bool(v.Name, v.ValBool, v.Usage)
|
rootCmd.Flags().BoolP(v.Name, v.Short, v.ValBool, v.Usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = viper.BindPFlag(v.Name, rootCmd.Flags().Lookup(v.Name))
|
_ = viper.BindPFlag(v.Name, rootCmd.Flags().Lookup(v.Name))
|
||||||
|
@ -94,6 +81,22 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
rootCmd.AddCommand(initToolCmd())
|
rootCmd.AddCommand(initToolCmd())
|
||||||
|
|
||||||
|
cobra.OnInitialize(func() {
|
||||||
|
viper.SetConfigFile(cfgFile)
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
|
||||||
|
_, err := os.Stat(cfgFile)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
// 没有配置文件,不做处理
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = viper.ReadInConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Using config file:", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func initToolCmd() *cobra.Command {
|
func initToolCmd() *cobra.Command {
|
||||||
|
@ -106,7 +109,7 @@ func initToolCmd() *cobra.Command {
|
||||||
toolCmd.Flags().BoolVarP(&rev, "version", "v", false, "display version info")
|
toolCmd.Flags().BoolVarP(&rev, "version", "v", false, "display version info")
|
||||||
toolCmd.Flags().BoolVarP(&secret, "secret", "s", false, "generate a random jwt secret")
|
toolCmd.Flags().BoolVarP(&secret, "secret", "s", false, "generate a random jwt secret")
|
||||||
toolCmd.Flags().StringVarP(&passwd, "passwd", "p", "", "convert the password plaintext")
|
toolCmd.Flags().StringVarP(&passwd, "passwd", "p", "", "convert the password plaintext")
|
||||||
toolCmd.Flags().BoolVarP(&env, "env", "e", false, "list the config name and env key")
|
toolCmd.Flags().BoolVarP(&debug, "debug", "d", false, "list the config viper.Debug() info")
|
||||||
|
|
||||||
toolCmd.Run = func(cmd *cobra.Command, args []string) {
|
toolCmd.Run = func(cmd *cobra.Command, args []string) {
|
||||||
switch {
|
switch {
|
||||||
|
@ -120,10 +123,8 @@ func initToolCmd() *cobra.Command {
|
||||||
case passwd != "":
|
case passwd != "":
|
||||||
pass, _ := utils.PasswordHash(passwd)
|
pass, _ := utils.PasswordHash(passwd)
|
||||||
fmt.Printf("Passwd:%s\n", pass)
|
fmt.Printf("Passwd:%s\n", pass)
|
||||||
case env:
|
case debug:
|
||||||
for k, v := range envs {
|
viper.Debug()
|
||||||
fmt.Printf("%s => %s\n", k, v)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
fmt.Println("Using [anylink tool -h] for help")
|
fmt.Println("Using [anylink tool -h] for help")
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ const (
|
||||||
type config struct {
|
type config struct {
|
||||||
Typ int
|
Typ int
|
||||||
Name string
|
Name string
|
||||||
|
Short string
|
||||||
Usage string
|
Usage string
|
||||||
ValStr string
|
ValStr string
|
||||||
ValInt int
|
ValInt int
|
||||||
|
|
Loading…
Reference in New Issue