From 35c6d80c8d392d74babb36ec73c2bbeb495ea4ee Mon Sep 17 00:00:00 2001
From: bjdgyc <bjdgyc@163.com>
Date: Wed, 21 Jul 2021 16:46:34 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=85=8D=E7=BD=AE=E6=96=87?=
 =?UTF-8?q?=E4=BB=B6=E9=BB=98=E8=AE=A4=E5=80=BC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 server/base/cfg.go    | 10 ++++----
 server/base/cmd.go    | 53 ++++++++++++++++++++++---------------------
 server/base/config.go |  1 +
 3 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/server/base/cfg.go b/server/base/cfg.go
index 7f2e8dd..a853f1d 100644
--- a/server/base/cfg.go
+++ b/server/base/cfg.go
@@ -31,6 +31,7 @@ var (
 
 type ServerConfig struct {
 	// LinkAddr      string `json:"link_addr"`
+	ConfFile       string `json:"conf_file"`
 	ServerAddr     string `json:"server_addr"`
 	ServerDTLSAddr string `json:"server_dtls_addr"`
 	ServerDTLS     bool   `json:"server_dtls"`
@@ -65,7 +66,7 @@ type ServerConfig struct {
 	MobileDpd       int    `json:"mobile_dpd"`
 
 	SessionTimeout int `json:"session_timeout"` // in seconds
-	AuthTimeout    int `json:"auth_timeout"`    // in seconds
+	// AuthTimeout    int `json:"auth_timeout"`    // in seconds
 }
 
 func initServerCfg() {
@@ -127,6 +128,7 @@ func initCfg() {
 		}
 	}
 
+	Cfg.ConfFile = cfgFile
 	initServerCfg()
 }
 
@@ -150,9 +152,9 @@ func ServerCfg2Slice() []SCfg {
 		value := s.Field(i)
 		tag := field.Tag.Get("json")
 		usage, env := getUsageEnv(tag)
-		if usage == "" {
-			continue
-		}
+		// if usage == "" {
+		// continue
+		// }
 
 		datas = append(datas, SCfg{Name: tag, Env: env, Info: usage, Data: value.Interface()})
 	}
diff --git a/server/base/cmd.go b/server/base/cmd.go
index baa3afb..2e34649 100644
--- a/server/base/cmd.go
+++ b/server/base/cmd.go
@@ -1,6 +1,7 @@
 package base
 
 import (
+	"errors"
 	"fmt"
 	"os"
 	"runtime"
@@ -22,8 +23,8 @@ var (
 	secret bool
 	// 显示版本信息
 	rev bool
-	// 获取env名称
-	env bool
+	// 输出debug信息
+	debug bool
 
 	// Used for flags.
 	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")
 
 	// 基础配置
-	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 {
 		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 {
-			rootCmd.Flags().Int(v.Name, v.ValInt, v.Usage)
+			rootCmd.Flags().IntP(v.Name, v.Short, v.ValInt, v.Usage)
 		}
 		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))
@@ -94,6 +81,22 @@ func init() {
 	}
 
 	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 {
@@ -106,7 +109,7 @@ func initToolCmd() *cobra.Command {
 	toolCmd.Flags().BoolVarP(&rev, "version", "v", false, "display version info")
 	toolCmd.Flags().BoolVarP(&secret, "secret", "s", false, "generate a random jwt secret")
 	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) {
 		switch {
@@ -120,10 +123,8 @@ func initToolCmd() *cobra.Command {
 		case passwd != "":
 			pass, _ := utils.PasswordHash(passwd)
 			fmt.Printf("Passwd:%s\n", pass)
-		case env:
-			for k, v := range envs {
-				fmt.Printf("%s => %s\n", k, v)
-			}
+		case debug:
+			viper.Debug()
 		default:
 			fmt.Println("Using [anylink tool -h] for help")
 		}
diff --git a/server/base/config.go b/server/base/config.go
index ff02370..24e72f5 100644
--- a/server/base/config.go
+++ b/server/base/config.go
@@ -11,6 +11,7 @@ const (
 type config struct {
 	Typ     int
 	Name    string
+	Short   string
 	Usage   string
 	ValStr  string
 	ValInt  int