root.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/drakkan/sftpgo/ldapauthserver/config"
  6. "github.com/drakkan/sftpgo/ldapauthserver/utils"
  7. "github.com/spf13/cobra"
  8. "github.com/spf13/viper"
  9. )
  10. const (
  11. logSender = "cmd"
  12. configDirFlag = "config-dir"
  13. configDirKey = "config_dir"
  14. configFileFlag = "config-file"
  15. configFileKey = "config_file"
  16. logFilePathFlag = "log-file-path"
  17. logFilePathKey = "log_file_path"
  18. logMaxSizeFlag = "log-max-size"
  19. logMaxSizeKey = "log_max_size"
  20. logMaxBackupFlag = "log-max-backups"
  21. logMaxBackupKey = "log_max_backups"
  22. logMaxAgeFlag = "log-max-age"
  23. logMaxAgeKey = "log_max_age"
  24. logCompressFlag = "log-compress"
  25. logCompressKey = "log_compress"
  26. logVerboseFlag = "log-verbose"
  27. logVerboseKey = "log_verbose"
  28. profilerFlag = "profiler"
  29. profilerKey = "profiler"
  30. defaultConfigDir = "."
  31. defaultConfigName = config.DefaultConfigName
  32. defaultLogFile = "ldapauth.log"
  33. defaultLogMaxSize = 10
  34. defaultLogMaxBackup = 5
  35. defaultLogMaxAge = 28
  36. defaultLogCompress = false
  37. defaultLogVerbose = true
  38. )
  39. var (
  40. configDir string
  41. configFile string
  42. logFilePath string
  43. logMaxSize int
  44. logMaxBackups int
  45. logMaxAge int
  46. logCompress bool
  47. logVerbose bool
  48. rootCmd = &cobra.Command{
  49. Use: "ldapauthserver",
  50. Short: "LDAP Authentication Server for SFTPGo",
  51. }
  52. )
  53. func init() {
  54. version := utils.GetAppVersion()
  55. rootCmd.Flags().BoolP("version", "v", false, "")
  56. rootCmd.Version = version.GetVersionAsString()
  57. rootCmd.SetVersionTemplate(`{{printf "LDAP Authentication Server version: "}}{{printf "%s" .Version}}
  58. `)
  59. }
  60. // Execute adds all child commands to the root command and sets flags appropriately.
  61. // This is called by main.main(). It only needs to happen once to the rootCmd.
  62. func Execute() {
  63. if err := rootCmd.Execute(); err != nil {
  64. fmt.Println(err)
  65. os.Exit(1)
  66. }
  67. }
  68. func addConfigFlags(cmd *cobra.Command) {
  69. viper.SetDefault(configDirKey, defaultConfigDir)
  70. viper.BindEnv(configDirKey, "LDAPAUTH_CONFIG_DIR")
  71. cmd.Flags().StringVarP(&configDir, configDirFlag, "c", viper.GetString(configDirKey),
  72. "Location for the config dir. This directory should contain the \"ldapauth\" configuration file or the configured "+
  73. "config-file. This flag can be set using LDAPAUTH_CONFIG_DIR env var too.")
  74. viper.BindPFlag(configDirKey, cmd.Flags().Lookup(configDirFlag))
  75. viper.SetDefault(configFileKey, defaultConfigName)
  76. viper.BindEnv(configFileKey, "LDAPAUTH_CONFIG_FILE")
  77. cmd.Flags().StringVarP(&configFile, configFileFlag, "f", viper.GetString(configFileKey),
  78. "Name for the configuration file. It must be the name of a file stored in config-dir not the absolute path to the "+
  79. "configuration file. The specified file name must have no extension we automatically load JSON, YAML, TOML, HCL and "+
  80. "Java properties. Therefore if you set \"ldapauth\" then \"ldapauth.toml\", \"ldapauth.yaml\" and so on are searched. "+
  81. "This flag can be set using LDAPAUTH_CONFIG_FILE env var too.")
  82. viper.BindPFlag(configFileKey, cmd.Flags().Lookup(configFileFlag))
  83. }
  84. func addServeFlags(cmd *cobra.Command) {
  85. addConfigFlags(cmd)
  86. viper.SetDefault(logFilePathKey, defaultLogFile)
  87. viper.BindEnv(logFilePathKey, "LDAPAUTH_LOG_FILE_PATH")
  88. cmd.Flags().StringVarP(&logFilePath, logFilePathFlag, "l", viper.GetString(logFilePathKey),
  89. "Location for the log file. Leave empty to write logs to the standard output. This flag can be set using LDAPAUTH_LOG_FILE_PATH "+
  90. "env var too.")
  91. viper.BindPFlag(logFilePathKey, cmd.Flags().Lookup(logFilePathFlag))
  92. viper.SetDefault(logMaxSizeKey, defaultLogMaxSize)
  93. viper.BindEnv(logMaxSizeKey, "LDAPAUTH_LOG_MAX_SIZE")
  94. cmd.Flags().IntVarP(&logMaxSize, logMaxSizeFlag, "s", viper.GetInt(logMaxSizeKey),
  95. "Maximum size in megabytes of the log file before it gets rotated. This flag can be set using LDAPAUTH_LOG_MAX_SIZE "+
  96. "env var too. It is unused if log-file-path is empty.")
  97. viper.BindPFlag(logMaxSizeKey, cmd.Flags().Lookup(logMaxSizeFlag))
  98. viper.SetDefault(logMaxBackupKey, defaultLogMaxBackup)
  99. viper.BindEnv(logMaxBackupKey, "LDAPAUTH_LOG_MAX_BACKUPS")
  100. cmd.Flags().IntVarP(&logMaxBackups, "log-max-backups", "b", viper.GetInt(logMaxBackupKey),
  101. "Maximum number of old log files to retain. This flag can be set using LDAPAUTH_LOG_MAX_BACKUPS env var too. "+
  102. "It is unused if log-file-path is empty.")
  103. viper.BindPFlag(logMaxBackupKey, cmd.Flags().Lookup(logMaxBackupFlag))
  104. viper.SetDefault(logMaxAgeKey, defaultLogMaxAge)
  105. viper.BindEnv(logMaxAgeKey, "LDAPAUTH_LOG_MAX_AGE")
  106. cmd.Flags().IntVarP(&logMaxAge, "log-max-age", "a", viper.GetInt(logMaxAgeKey),
  107. "Maximum number of days to retain old log files. This flag can be set using LDAPAUTH_LOG_MAX_AGE env var too. "+
  108. "It is unused if log-file-path is empty.")
  109. viper.BindPFlag(logMaxAgeKey, cmd.Flags().Lookup(logMaxAgeFlag))
  110. viper.SetDefault(logCompressKey, defaultLogCompress)
  111. viper.BindEnv(logCompressKey, "LDAPAUTH_LOG_COMPRESS")
  112. cmd.Flags().BoolVarP(&logCompress, logCompressFlag, "z", viper.GetBool(logCompressKey), "Determine if the rotated "+
  113. "log files should be compressed using gzip. This flag can be set using LDAPAUTH_LOG_COMPRESS env var too. "+
  114. "It is unused if log-file-path is empty.")
  115. viper.BindPFlag(logCompressKey, cmd.Flags().Lookup(logCompressFlag))
  116. viper.SetDefault(logVerboseKey, defaultLogVerbose)
  117. viper.BindEnv(logVerboseKey, "LDAPAUTH_LOG_VERBOSE")
  118. cmd.Flags().BoolVarP(&logVerbose, logVerboseFlag, "v", viper.GetBool(logVerboseKey), "Enable verbose logs. "+
  119. "This flag can be set using LDAPAUTH_LOG_VERBOSE env var too.")
  120. viper.BindPFlag(logVerboseKey, cmd.Flags().Lookup(logVerboseFlag))
  121. }