root.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/utils"
  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. defaultConfigDir = "."
  31. defaultConfigName = config.DefaultConfigName
  32. defaultLogFile = "sftpgo.log"
  33. defaultLogMaxSize = 10
  34. defaultLogMaxBackup = 5
  35. defaultLogMaxAge = 28
  36. defaultLogCompress = false
  37. defaultLogVerbose = true
  38. defaultProfiler = false
  39. )
  40. var (
  41. configDir string
  42. configFile string
  43. logFilePath string
  44. logMaxSize int
  45. logMaxBackups int
  46. logMaxAge int
  47. logCompress bool
  48. logVerbose bool
  49. profiler bool
  50. rootCmd = &cobra.Command{
  51. Use: "sftpgo",
  52. Short: "Full featured and highly configurable SFTP server",
  53. }
  54. )
  55. func init() {
  56. version := utils.GetAppVersion()
  57. rootCmd.Flags().BoolP("version", "v", false, "")
  58. rootCmd.Version = version.GetVersionAsString()
  59. rootCmd.SetVersionTemplate(`{{printf "SFTPGo "}}{{printf "%s" .Version}}
  60. `)
  61. }
  62. // Execute adds all child commands to the root command and sets flags appropriately.
  63. // This is called by main.main(). It only needs to happen once to the rootCmd.
  64. func Execute() {
  65. if err := rootCmd.Execute(); err != nil {
  66. fmt.Println(err)
  67. os.Exit(1)
  68. }
  69. }
  70. func addConfigFlags(cmd *cobra.Command) {
  71. viper.SetDefault(configDirKey, defaultConfigDir)
  72. viper.BindEnv(configDirKey, "SFTPGO_CONFIG_DIR") //nolint:errcheck // err is not nil only if the key to bind is missing
  73. cmd.Flags().StringVarP(&configDir, configDirFlag, "c", viper.GetString(configDirKey),
  74. "Location for SFTPGo config dir. This directory should contain the \"sftpgo\" configuration file or the configured "+
  75. "config-file and it is used as the base for files with a relative path (eg. the private keys for the SFTP server, "+
  76. "the SQLite database if you use SQLite as data provider). This flag can be set using SFTPGO_CONFIG_DIR env var too.")
  77. viper.BindPFlag(configDirKey, cmd.Flags().Lookup(configDirFlag)) //nolint:errcheck
  78. viper.SetDefault(configFileKey, defaultConfigName)
  79. viper.BindEnv(configFileKey, "SFTPGO_CONFIG_FILE") //nolint:errcheck
  80. cmd.Flags().StringVarP(&configFile, configFileFlag, "f", viper.GetString(configFileKey),
  81. "Name for SFTPGo configuration file. It must be the name of a file stored in config-dir not the absolute path to the "+
  82. "configuration file. The specified file name must have no extension we automatically load JSON, YAML, TOML, HCL and "+
  83. "Java properties. Therefore if you set \"sftpgo\" then \"sftpgo.json\", \"sftpgo.yaml\" and so on are searched. "+
  84. "This flag can be set using SFTPGO_CONFIG_FILE env var too.")
  85. viper.BindPFlag(configFileKey, cmd.Flags().Lookup(configFileFlag)) //nolint:errcheck
  86. }
  87. func addServeFlags(cmd *cobra.Command) {
  88. addConfigFlags(cmd)
  89. viper.SetDefault(logFilePathKey, defaultLogFile)
  90. viper.BindEnv(logFilePathKey, "SFTPGO_LOG_FILE_PATH") //nolint:errcheck
  91. cmd.Flags().StringVarP(&logFilePath, logFilePathFlag, "l", viper.GetString(logFilePathKey),
  92. "Location for the log file. Leave empty to write logs to the standard output. This flag can be set using SFTPGO_LOG_FILE_PATH "+
  93. "env var too.")
  94. viper.BindPFlag(logFilePathKey, cmd.Flags().Lookup(logFilePathFlag)) //nolint:errcheck
  95. viper.SetDefault(logMaxSizeKey, defaultLogMaxSize)
  96. viper.BindEnv(logMaxSizeKey, "SFTPGO_LOG_MAX_SIZE") //nolint:errcheck
  97. cmd.Flags().IntVarP(&logMaxSize, logMaxSizeFlag, "s", viper.GetInt(logMaxSizeKey),
  98. "Maximum size in megabytes of the log file before it gets rotated. This flag can be set using SFTPGO_LOG_MAX_SIZE "+
  99. "env var too. It is unused if log-file-path is empty.")
  100. viper.BindPFlag(logMaxSizeKey, cmd.Flags().Lookup(logMaxSizeFlag)) //nolint:errcheck
  101. viper.SetDefault(logMaxBackupKey, defaultLogMaxBackup)
  102. viper.BindEnv(logMaxBackupKey, "SFTPGO_LOG_MAX_BACKUPS") //nolint:errcheck
  103. cmd.Flags().IntVarP(&logMaxBackups, "log-max-backups", "b", viper.GetInt(logMaxBackupKey),
  104. "Maximum number of old log files to retain. This flag can be set using SFTPGO_LOG_MAX_BACKUPS env var too. "+
  105. "It is unused if log-file-path is empty.")
  106. viper.BindPFlag(logMaxBackupKey, cmd.Flags().Lookup(logMaxBackupFlag)) //nolint:errcheck
  107. viper.SetDefault(logMaxAgeKey, defaultLogMaxAge)
  108. viper.BindEnv(logMaxAgeKey, "SFTPGO_LOG_MAX_AGE") //nolint:errcheck
  109. cmd.Flags().IntVarP(&logMaxAge, "log-max-age", "a", viper.GetInt(logMaxAgeKey),
  110. "Maximum number of days to retain old log files. This flag can be set using SFTPGO_LOG_MAX_AGE env var too. "+
  111. "It is unused if log-file-path is empty.")
  112. viper.BindPFlag(logMaxAgeKey, cmd.Flags().Lookup(logMaxAgeFlag)) //nolint:errcheck
  113. viper.SetDefault(logCompressKey, defaultLogCompress)
  114. viper.BindEnv(logCompressKey, "SFTPGO_LOG_COMPRESS") //nolint:errcheck
  115. cmd.Flags().BoolVarP(&logCompress, logCompressFlag, "z", viper.GetBool(logCompressKey), "Determine if the rotated "+
  116. "log files should be compressed using gzip. This flag can be set using SFTPGO_LOG_COMPRESS env var too. "+
  117. "It is unused if log-file-path is empty.")
  118. viper.BindPFlag(logCompressKey, cmd.Flags().Lookup(logCompressFlag)) //nolint:errcheck
  119. viper.SetDefault(logVerboseKey, defaultLogVerbose)
  120. viper.BindEnv(logVerboseKey, "SFTPGO_LOG_VERBOSE") //nolint:errcheck
  121. cmd.Flags().BoolVarP(&logVerbose, logVerboseFlag, "v", viper.GetBool(logVerboseKey), "Enable verbose logs. "+
  122. "This flag can be set using SFTPGO_LOG_VERBOSE env var too.")
  123. viper.BindPFlag(logVerboseKey, cmd.Flags().Lookup(logVerboseFlag)) //nolint:errcheck
  124. viper.SetDefault(profilerKey, defaultProfiler)
  125. viper.BindEnv(profilerKey, "SFTPGO_PROFILER") //nolint:errcheck
  126. cmd.Flags().BoolVarP(&profiler, profilerFlag, "p", viper.GetBool(profilerKey), "Enable the built-in profiler. "+
  127. "The profiler will be accessible via HTTP/HTTPS using the base URL \"/debug/pprof/\". "+
  128. "This flag can be set using SFTPGO_PROFILER env var too.")
  129. viper.BindPFlag(profilerKey, cmd.Flags().Lookup(profilerFlag)) //nolint:errcheck
  130. }