dockerfiles/anylink/server/base/cmd.go

158 lines
3.2 KiB
Go
Raw Normal View History

2021-06-08 20:45:26 +08:00
package base
import (
2021-08-02 20:41:35 +08:00
"errors"
2021-06-08 20:45:26 +08:00
"fmt"
"os"
2021-08-02 20:41:35 +08:00
"reflect"
2021-06-08 20:45:26 +08:00
"runtime"
"strings"
"github.com/bjdgyc/anylink/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
// 提交id
CommitId string
// pass明文
passwd string
// 生成密钥
secret bool
// 显示版本信息
rev bool
2021-08-02 20:41:35 +08:00
// 输出debug信息
debug bool
2021-06-08 20:45:26 +08:00
// Used for flags.
runSrv bool
2021-08-02 20:41:35 +08:00
linkViper *viper.Viper
rootCmd *cobra.Command
2021-06-08 20:45:26 +08:00
)
// Execute executes the root command.
func execute() {
2021-08-02 20:41:35 +08:00
initCmd()
2021-06-08 20:45:26 +08:00
err := rootCmd.Execute()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
// viper.Debug()
2021-08-02 20:41:35 +08:00
ref := reflect.ValueOf(linkViper)
s := ref.Elem()
ee := s.FieldByName("env")
if ee.Kind() != reflect.Map {
panic("Viper env is err")
}
rr := ee.MapRange()
for rr.Next() {
2021-08-26 23:09:52 +08:00
// fmt.Println(rr.Key(), rr.Value().Index(0))
envs[rr.Key().String()] = rr.Value().Index(0).String()
2021-08-02 20:41:35 +08:00
}
2021-06-08 20:45:26 +08:00
if !runSrv {
os.Exit(0)
}
}
2021-08-02 20:41:35 +08:00
func initCmd() {
linkViper = viper.New()
2021-06-08 20:45:26 +08:00
rootCmd = &cobra.Command{
Use: "anylink",
Short: "AnyLink VPN Server",
Long: `AnyLink is a VPN Server application`,
Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("cmd", cmd.Use, args)
runSrv = true
2023-04-26 22:17:10 +08:00
if rev {
printVersion()
os.Exit(0)
}
2021-06-08 20:45:26 +08:00
},
}
2021-08-02 20:41:35 +08:00
linkViper.SetEnvPrefix("link")
2021-06-08 20:45:26 +08:00
// 基础配置
for _, v := range configs {
if v.Typ == cfgStr {
2021-08-02 20:41:35 +08:00
rootCmd.Flags().StringP(v.Name, v.Short, v.ValStr, v.Usage)
2021-06-08 20:45:26 +08:00
}
if v.Typ == cfgInt {
2021-08-02 20:41:35 +08:00
rootCmd.Flags().IntP(v.Name, v.Short, v.ValInt, v.Usage)
2021-06-08 20:45:26 +08:00
}
if v.Typ == cfgBool {
2021-08-02 20:41:35 +08:00
rootCmd.Flags().BoolP(v.Name, v.Short, v.ValBool, v.Usage)
2021-06-08 20:45:26 +08:00
}
2021-08-02 20:41:35 +08:00
_ = linkViper.BindPFlag(v.Name, rootCmd.Flags().Lookup(v.Name))
_ = linkViper.BindEnv(v.Name)
2021-06-08 20:45:26 +08:00
// viper.SetDefault(v.Name, v.Value)
}
2023-04-26 22:17:10 +08:00
rootCmd.Flags().BoolVarP(&rev, "version", "v", false, "display version info")
2021-06-08 20:45:26 +08:00
rootCmd.AddCommand(initToolCmd())
2021-08-02 20:41:35 +08:00
cobra.OnInitialize(func() {
linkViper.AutomaticEnv()
conf := linkViper.GetString("conf")
_, err := os.Stat(conf)
if errors.Is(err, os.ErrNotExist) {
// 没有配置文件,不做处理
2021-12-29 11:43:26 +08:00
panic(err)
2021-08-02 20:41:35 +08:00
}
linkViper.SetConfigFile(conf)
err = linkViper.ReadInConfig()
if err != nil {
fmt.Println("Using config file:", err)
}
})
2021-06-08 20:45:26 +08:00
}
func initToolCmd() *cobra.Command {
toolCmd := &cobra.Command{
Use: "tool",
Short: "AnyLink tool",
Long: `AnyLink tool is a application`,
}
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")
2021-08-02 20:41:35 +08:00
toolCmd.Flags().BoolVarP(&debug, "debug", "d", false, "list the config viper.Debug() info")
2021-06-08 20:45:26 +08:00
toolCmd.Run = func(cmd *cobra.Command, args []string) {
switch {
case rev:
2023-04-26 22:17:10 +08:00
printVersion()
2021-06-08 20:45:26 +08:00
case secret:
s, _ := utils.RandSecret(40, 60)
s = strings.Trim(s, "=")
fmt.Printf("Secret:%s\n", s)
case passwd != "":
pass, _ := utils.PasswordHash(passwd)
fmt.Printf("Passwd:%s\n", pass)
2021-08-02 20:41:35 +08:00
case debug:
linkViper.Debug()
2021-06-08 20:45:26 +08:00
default:
fmt.Println("Using [anylink tool -h] for help")
}
}
return toolCmd
}
2023-04-26 22:17:10 +08:00
func printVersion() {
fmt.Printf("%s v%s build on %s [%s, %s] commit_id(%s) \n",
APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, CommitId)
}