root.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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
  73. should contain the "ldapauth" configuration
  74. file or the configured config-file. This flag
  75. can be set using LDAPAUTH_CONFIG_DIR env var too.
  76. `)
  77. viper.BindPFlag(configDirKey, cmd.Flags().Lookup(configDirFlag))
  78. viper.SetDefault(configFileKey, defaultConfigName)
  79. viper.BindEnv(configFileKey, "LDAPAUTH_CONFIG_FILE")
  80. cmd.Flags().StringVarP(&configFile, configFileFlag, "f", viper.GetString(configFileKey),
  81. `Name for the configuration file. It must be
  82. the name of a file stored in config-dir not
  83. the absolute path to the configuration file.
  84. The specified file name must have no extension
  85. we automatically load JSON, YAML, TOML, HCL and
  86. Java properties. Therefore if you set \"ldapauth\"
  87. then \"ldapauth.toml\", \"ldapauth.yaml\" and
  88. so on are searched. This flag can be set using
  89. LDAPAUTH_CONFIG_FILE env var too.
  90. `)
  91. viper.BindPFlag(configFileKey, cmd.Flags().Lookup(configFileFlag))
  92. }
  93. func addServeFlags(cmd *cobra.Command) {
  94. addConfigFlags(cmd)
  95. viper.SetDefault(logFilePathKey, defaultLogFile)
  96. viper.BindEnv(logFilePathKey, "LDAPAUTH_LOG_FILE_PATH")
  97. cmd.Flags().StringVarP(&logFilePath, logFilePathFlag, "l", viper.GetString(logFilePathKey),
  98. `Location for the log file. Leave empty to write
  99. logs to the standard output. This flag can be
  100. set using LDAPAUTH_LOG_FILE_PATH env var too.
  101. `)
  102. viper.BindPFlag(logFilePathKey, cmd.Flags().Lookup(logFilePathFlag))
  103. viper.SetDefault(logMaxSizeKey, defaultLogMaxSize)
  104. viper.BindEnv(logMaxSizeKey, "LDAPAUTH_LOG_MAX_SIZE")
  105. cmd.Flags().IntVarP(&logMaxSize, logMaxSizeFlag, "s", viper.GetInt(logMaxSizeKey),
  106. `Maximum size in megabytes of the log file
  107. before it gets rotated. This flag can be set
  108. using LDAPAUTH_LOG_MAX_SIZE env var too. It
  109. is unused if log-file-path is empty.`)
  110. viper.BindPFlag(logMaxSizeKey, cmd.Flags().Lookup(logMaxSizeFlag))
  111. viper.SetDefault(logMaxBackupKey, defaultLogMaxBackup)
  112. viper.BindEnv(logMaxBackupKey, "LDAPAUTH_LOG_MAX_BACKUPS")
  113. cmd.Flags().IntVarP(&logMaxBackups, "log-max-backups", "b", viper.GetInt(logMaxBackupKey),
  114. `Maximum number of old log files to retain.
  115. This flag can be set using LDAPAUTH_LOG_MAX_BACKUPS
  116. env var too. It is unused if log-file-path is
  117. empty.`)
  118. viper.BindPFlag(logMaxBackupKey, cmd.Flags().Lookup(logMaxBackupFlag))
  119. viper.SetDefault(logMaxAgeKey, defaultLogMaxAge)
  120. viper.BindEnv(logMaxAgeKey, "LDAPAUTH_LOG_MAX_AGE")
  121. cmd.Flags().IntVarP(&logMaxAge, "log-max-age", "a", viper.GetInt(logMaxAgeKey),
  122. `Maximum number of days to retain old log files.
  123. This flag can be set using LDAPAUTH_LOG_MAX_AGE
  124. env var too. It is unused if log-file-path is
  125. empty.`)
  126. viper.BindPFlag(logMaxAgeKey, cmd.Flags().Lookup(logMaxAgeFlag))
  127. viper.SetDefault(logCompressKey, defaultLogCompress)
  128. viper.BindEnv(logCompressKey, "LDAPAUTH_LOG_COMPRESS")
  129. cmd.Flags().BoolVarP(&logCompress, logCompressFlag, "z", viper.GetBool(logCompressKey),
  130. `Determine if the rotated log files
  131. should be compressed using gzip. This flag can
  132. be set using LDAPAUTH_LOG_COMPRESS env var too.
  133. It is unused if log-file-path is empty.`)
  134. viper.BindPFlag(logCompressKey, cmd.Flags().Lookup(logCompressFlag))
  135. viper.SetDefault(logVerboseKey, defaultLogVerbose)
  136. viper.BindEnv(logVerboseKey, "LDAPAUTH_LOG_VERBOSE")
  137. cmd.Flags().BoolVarP(&logVerbose, logVerboseFlag, "v", viper.GetBool(logVerboseKey),
  138. `Enable verbose logs. This flag can be set
  139. using LDAPAUTH_LOG_VERBOSE env var too.
  140. `)
  141. viper.BindPFlag(logVerboseKey, cmd.Flags().Lookup(logVerboseFlag))
  142. }