root.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Package cmd provides Command Line Interface support
  2. package cmd
  3. import (
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "github.com/spf13/viper"
  8. "github.com/drakkan/sftpgo/config"
  9. "github.com/drakkan/sftpgo/version"
  10. )
  11. const (
  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. loadDataFromFlag = "loaddata-from"
  31. loadDataFromKey = "loaddata_from"
  32. loadDataModeFlag = "loaddata-mode"
  33. loadDataModeKey = "loaddata_mode"
  34. loadDataQuotaScanFlag = "loaddata-scan"
  35. loadDataQuotaScanKey = "loaddata_scan"
  36. loadDataCleanFlag = "loaddata-clean"
  37. loadDataCleanKey = "loaddata_clean"
  38. defaultConfigDir = "."
  39. defaultConfigName = config.DefaultConfigName
  40. defaultLogFile = "sftpgo.log"
  41. defaultLogMaxSize = 10
  42. defaultLogMaxBackup = 5
  43. defaultLogMaxAge = 28
  44. defaultLogCompress = false
  45. defaultLogVerbose = true
  46. defaultProfiler = false
  47. defaultLoadDataFrom = ""
  48. defaultLoadDataMode = 1
  49. defaultLoadDataQuotaScan = 0
  50. defaultLoadDataClean = false
  51. )
  52. var (
  53. configDir string
  54. configFile string
  55. logFilePath string
  56. logMaxSize int
  57. logMaxBackups int
  58. logMaxAge int
  59. logCompress bool
  60. logVerbose bool
  61. profiler bool
  62. loadDataFrom string
  63. loadDataMode int
  64. loadDataQuotaScan int
  65. loadDataClean bool
  66. rootCmd = &cobra.Command{
  67. Use: "sftpgo",
  68. Short: "Fully featured and highly configurable SFTP server",
  69. }
  70. )
  71. func init() {
  72. rootCmd.Flags().BoolP("version", "v", false, "")
  73. rootCmd.Version = version.GetAsString()
  74. rootCmd.SetVersionTemplate(`{{printf "SFTPGo "}}{{printf "%s" .Version}}
  75. `)
  76. }
  77. // Execute adds all child commands to the root command and sets flags appropriately.
  78. // This is called by main.main(). It only needs to happen once to the rootCmd.
  79. func Execute() {
  80. if err := rootCmd.Execute(); err != nil {
  81. fmt.Println(err)
  82. os.Exit(1)
  83. }
  84. }
  85. func addConfigFlags(cmd *cobra.Command) {
  86. viper.SetDefault(configDirKey, defaultConfigDir)
  87. viper.BindEnv(configDirKey, "SFTPGO_CONFIG_DIR") //nolint:errcheck // err is not nil only if the key to bind is missing
  88. cmd.Flags().StringVarP(&configDir, configDirFlag, "c", viper.GetString(configDirKey),
  89. `Location for SFTPGo config dir. This directory
  90. should contain the "sftpgo" configuration file
  91. or the configured config-file and it is used as
  92. the base for files with a relative path (eg. the
  93. private keys for the SFTP server, the SQLite
  94. database if you use SQLite as data provider).
  95. This flag can be set using SFTPGO_CONFIG_DIR
  96. env var too.`)
  97. viper.BindPFlag(configDirKey, cmd.Flags().Lookup(configDirFlag)) //nolint:errcheck
  98. viper.SetDefault(configFileKey, defaultConfigName)
  99. viper.BindEnv(configFileKey, "SFTPGO_CONFIG_FILE") //nolint:errcheck
  100. cmd.Flags().StringVarP(&configFile, configFileFlag, "f", viper.GetString(configFileKey),
  101. `Name for SFTPGo configuration file. It must be
  102. the name of a file stored in config-dir not the
  103. absolute path to the configuration file. The
  104. specified file name must have no extension we
  105. automatically load JSON, YAML, TOML, HCL and
  106. Java properties. Therefore if you set "sftpgo"
  107. then "sftpgo.json", "sftpgo.yaml" and so on
  108. are searched.
  109. This flag can be set using SFTPGO_CONFIG_FILE
  110. env var too.`)
  111. viper.BindPFlag(configFileKey, cmd.Flags().Lookup(configFileFlag)) //nolint:errcheck
  112. }
  113. func addServeFlags(cmd *cobra.Command) {
  114. addConfigFlags(cmd)
  115. viper.SetDefault(logFilePathKey, defaultLogFile)
  116. viper.BindEnv(logFilePathKey, "SFTPGO_LOG_FILE_PATH") //nolint:errcheck
  117. cmd.Flags().StringVarP(&logFilePath, logFilePathFlag, "l", viper.GetString(logFilePathKey),
  118. `Location for the log file. Leave empty to write
  119. logs to the standard output. This flag can be
  120. set using SFTPGO_LOG_FILE_PATH env var too.
  121. `)
  122. viper.BindPFlag(logFilePathKey, cmd.Flags().Lookup(logFilePathFlag)) //nolint:errcheck
  123. viper.SetDefault(logMaxSizeKey, defaultLogMaxSize)
  124. viper.BindEnv(logMaxSizeKey, "SFTPGO_LOG_MAX_SIZE") //nolint:errcheck
  125. cmd.Flags().IntVarP(&logMaxSize, logMaxSizeFlag, "s", viper.GetInt(logMaxSizeKey),
  126. `Maximum size in megabytes of the log file
  127. before it gets rotated. This flag can be set
  128. using SFTPGO_LOG_MAX_SIZE env var too. It is
  129. unused if log-file-path is empty.
  130. `)
  131. viper.BindPFlag(logMaxSizeKey, cmd.Flags().Lookup(logMaxSizeFlag)) //nolint:errcheck
  132. viper.SetDefault(logMaxBackupKey, defaultLogMaxBackup)
  133. viper.BindEnv(logMaxBackupKey, "SFTPGO_LOG_MAX_BACKUPS") //nolint:errcheck
  134. cmd.Flags().IntVarP(&logMaxBackups, "log-max-backups", "b", viper.GetInt(logMaxBackupKey),
  135. `Maximum number of old log files to retain.
  136. This flag can be set using SFTPGO_LOG_MAX_BACKUPS
  137. env var too. It is unused if log-file-path is
  138. empty.`)
  139. viper.BindPFlag(logMaxBackupKey, cmd.Flags().Lookup(logMaxBackupFlag)) //nolint:errcheck
  140. viper.SetDefault(logMaxAgeKey, defaultLogMaxAge)
  141. viper.BindEnv(logMaxAgeKey, "SFTPGO_LOG_MAX_AGE") //nolint:errcheck
  142. cmd.Flags().IntVarP(&logMaxAge, "log-max-age", "a", viper.GetInt(logMaxAgeKey),
  143. `Maximum number of days to retain old log files.
  144. This flag can be set using SFTPGO_LOG_MAX_AGE env
  145. var too. It is unused if log-file-path is empty.
  146. `)
  147. viper.BindPFlag(logMaxAgeKey, cmd.Flags().Lookup(logMaxAgeFlag)) //nolint:errcheck
  148. viper.SetDefault(logCompressKey, defaultLogCompress)
  149. viper.BindEnv(logCompressKey, "SFTPGO_LOG_COMPRESS") //nolint:errcheck
  150. cmd.Flags().BoolVarP(&logCompress, logCompressFlag, "z", viper.GetBool(logCompressKey),
  151. `Determine if the rotated log files
  152. should be compressed using gzip. This flag can
  153. be set using SFTPGO_LOG_COMPRESS env var too.
  154. It is unused if log-file-path is empty.
  155. `)
  156. viper.BindPFlag(logCompressKey, cmd.Flags().Lookup(logCompressFlag)) //nolint:errcheck
  157. viper.SetDefault(logVerboseKey, defaultLogVerbose)
  158. viper.BindEnv(logVerboseKey, "SFTPGO_LOG_VERBOSE") //nolint:errcheck
  159. cmd.Flags().BoolVarP(&logVerbose, logVerboseFlag, "v", viper.GetBool(logVerboseKey),
  160. `Enable verbose logs. This flag can be set
  161. using SFTPGO_LOG_VERBOSE env var too.
  162. `)
  163. viper.BindPFlag(logVerboseKey, cmd.Flags().Lookup(logVerboseFlag)) //nolint:errcheck
  164. viper.SetDefault(profilerKey, defaultProfiler)
  165. viper.BindEnv(profilerKey, "SFTPGO_PROFILER") //nolint:errcheck
  166. cmd.Flags().BoolVarP(&profiler, profilerFlag, "p", viper.GetBool(profilerKey),
  167. `Enable the built-in profiler. The profiler will
  168. be accessible via HTTP/HTTPS using the base URL
  169. "/debug/pprof/".
  170. This flag can be set using SFTPGO_PROFILER env
  171. var too.`)
  172. viper.BindPFlag(profilerKey, cmd.Flags().Lookup(profilerFlag)) //nolint:errcheck
  173. viper.SetDefault(loadDataFromKey, defaultLoadDataFrom)
  174. viper.BindEnv(loadDataFromKey, "SFTPGO_LOADDATA_FROM") //nolint:errcheck
  175. cmd.Flags().StringVar(&loadDataFrom, loadDataFromFlag, viper.GetString(loadDataFromKey),
  176. `Load users and folders from this file.
  177. The file must be specified as absolute path
  178. and it must contain a backup obtained using
  179. the "dumpdata" REST API or compatible content.
  180. This flag can be set using SFTPGO_LOADDATA_FROM
  181. env var too.
  182. `)
  183. viper.BindPFlag(loadDataFromKey, cmd.Flags().Lookup(loadDataFromFlag)) //nolint:errcheck
  184. viper.SetDefault(loadDataModeKey, defaultLoadDataMode)
  185. viper.BindEnv(loadDataModeKey, "SFTPGO_LOADDATA_MODE") //nolint:errcheck
  186. cmd.Flags().IntVar(&loadDataMode, loadDataModeFlag, viper.GetInt(loadDataModeKey),
  187. `Restore mode for data to load:
  188. 0 - new users are added, existing users are
  189. updated
  190. 1 - New users are added, existing users are
  191. not modified
  192. This flag can be set using SFTPGO_LOADDATA_MODE
  193. env var too.
  194. `)
  195. viper.BindPFlag(loadDataModeKey, cmd.Flags().Lookup(loadDataModeFlag)) //nolint:errcheck
  196. viper.SetDefault(loadDataQuotaScanKey, defaultLoadDataQuotaScan)
  197. viper.BindEnv(loadDataQuotaScanKey, "SFTPGO_LOADDATA_QUOTA_SCAN") //nolint:errcheck
  198. cmd.Flags().IntVar(&loadDataQuotaScan, loadDataQuotaScanFlag, viper.GetInt(loadDataQuotaScanKey),
  199. `Quota scan mode after data load:
  200. 0 - no quota scan
  201. 1 - scan quota
  202. 2 - scan quota if the user has quota restrictions
  203. This flag can be set using SFTPGO_LOADDATA_QUOTA_SCAN
  204. env var too.
  205. (default 0)`)
  206. viper.BindPFlag(loadDataQuotaScanKey, cmd.Flags().Lookup(loadDataQuotaScanFlag)) //nolint:errcheck
  207. viper.SetDefault(loadDataCleanKey, defaultLoadDataClean)
  208. viper.BindEnv(loadDataCleanKey, "SFTPGO_LOADDATA_CLEAN") //nolint:errcheck
  209. cmd.Flags().BoolVar(&loadDataClean, loadDataCleanFlag, viper.GetBool(loadDataCleanKey),
  210. `Determine if the loaddata-from file should
  211. be removed after a successful load. This flag
  212. can be set using SFTPGO_LOADDATA_CLEAN env var
  213. too. (default "false")
  214. `)
  215. viper.BindPFlag(logCompressKey, cmd.Flags().Lookup(logCompressFlag)) //nolint:errcheck
  216. }