cmd.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package base
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "strings"
  7. "github.com/bjdgyc/anylink/pkg/utils"
  8. "github.com/spf13/cobra"
  9. "github.com/spf13/viper"
  10. )
  11. var (
  12. // 提交id
  13. CommitId string
  14. // 配置文件
  15. cfgFile string
  16. // pass明文
  17. passwd string
  18. // 生成密钥
  19. secret bool
  20. // 显示版本信息
  21. rev bool
  22. // 获取env名称
  23. env bool
  24. // Used for flags.
  25. runSrv bool
  26. rootCmd *cobra.Command
  27. )
  28. // Execute executes the root command.
  29. func execute() {
  30. err := rootCmd.Execute()
  31. if err != nil {
  32. fmt.Println(err)
  33. os.Exit(0)
  34. }
  35. // viper.Debug()
  36. if !runSrv {
  37. os.Exit(0)
  38. }
  39. }
  40. func init() {
  41. rootCmd = &cobra.Command{
  42. Use: "anylink",
  43. Short: "AnyLink VPN Server",
  44. Long: `AnyLink is a VPN Server application`,
  45. Run: func(cmd *cobra.Command, args []string) {
  46. // fmt.Println("cmd:", cmd.Use, args)
  47. runSrv = true
  48. },
  49. }
  50. cobra.OnInitialize(func() {
  51. viper.SetConfigFile(cfgFile)
  52. viper.AutomaticEnv()
  53. if cfgFile == "" {
  54. // 没有配置文件,不做处理
  55. return
  56. }
  57. err := viper.ReadInConfig()
  58. if err != nil {
  59. fmt.Println("Using config file:", err)
  60. }
  61. })
  62. viper.SetEnvPrefix("link")
  63. // 基础配置
  64. rootCmd.Flags().StringVarP(&cfgFile, "conf", "c", "", "config file")
  65. for _, v := range configs {
  66. if v.Typ == cfgStr {
  67. rootCmd.Flags().String(v.Name, v.ValStr, v.Usage)
  68. }
  69. if v.Typ == cfgInt {
  70. rootCmd.Flags().Int(v.Name, v.ValInt, v.Usage)
  71. }
  72. if v.Typ == cfgBool {
  73. rootCmd.Flags().Bool(v.Name, v.ValBool, v.Usage)
  74. }
  75. _ = viper.BindPFlag(v.Name, rootCmd.Flags().Lookup(v.Name))
  76. _ = viper.BindEnv(v.Name)
  77. // viper.SetDefault(v.Name, v.Value)
  78. }
  79. rootCmd.AddCommand(initToolCmd())
  80. }
  81. func initToolCmd() *cobra.Command {
  82. toolCmd := &cobra.Command{
  83. Use: "tool",
  84. Short: "AnyLink tool",
  85. Long: `AnyLink tool is a application`,
  86. }
  87. toolCmd.Flags().BoolVarP(&rev, "version", "v", false, "display version info")
  88. toolCmd.Flags().BoolVarP(&secret, "secret", "s", false, "generate a random jwt secret")
  89. toolCmd.Flags().StringVarP(&passwd, "passwd", "p", "", "convert the password plaintext")
  90. toolCmd.Flags().BoolVarP(&env, "env", "e", false, "list the config name and env key")
  91. toolCmd.Run = func(cmd *cobra.Command, args []string) {
  92. switch {
  93. case rev:
  94. fmt.Printf("%s v%s build on %s [%s, %s] commit_id(%s) \n",
  95. APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, CommitId)
  96. case secret:
  97. s, _ := utils.RandSecret(40, 60)
  98. s = strings.Trim(s, "=")
  99. fmt.Printf("Secret:%s\n", s)
  100. case passwd != "":
  101. pass, _ := utils.PasswordHash(passwd)
  102. fmt.Printf("Passwd:%s\n", pass)
  103. case env:
  104. for k, v := range envs {
  105. fmt.Printf("%s => %s\n", k, v)
  106. }
  107. default:
  108. fmt.Println("Using [anylink tool -h] for help")
  109. }
  110. }
  111. return toolCmd
  112. }