2019-10-08 08:29:16 +00:00
|
|
|
// Package cmd provides Command Line Interface support
|
2019-08-07 20:46:13 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2019-09-16 06:52:58 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/version"
|
2019-08-07 20:46:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-20 16:42:37 +00:00
|
|
|
configDirFlag = "config-dir"
|
|
|
|
configDirKey = "config_dir"
|
|
|
|
configFileFlag = "config-file"
|
2020-12-03 16:16:35 +00:00
|
|
|
configFileKey = "config_file"
|
2020-10-20 16:42:37 +00:00
|
|
|
logFilePathFlag = "log-file-path"
|
|
|
|
logFilePathKey = "log_file_path"
|
|
|
|
logMaxSizeFlag = "log-max-size"
|
|
|
|
logMaxSizeKey = "log_max_size"
|
|
|
|
logMaxBackupFlag = "log-max-backups"
|
|
|
|
logMaxBackupKey = "log_max_backups"
|
|
|
|
logMaxAgeFlag = "log-max-age"
|
|
|
|
logMaxAgeKey = "log_max_age"
|
|
|
|
logCompressFlag = "log-compress"
|
|
|
|
logCompressKey = "log_compress"
|
|
|
|
logVerboseFlag = "log-verbose"
|
|
|
|
logVerboseKey = "log_verbose"
|
2021-11-06 14:18:16 +00:00
|
|
|
logUTCTimeFlag = "log-utc-time"
|
|
|
|
logUTCTimeKey = "log_utc_time"
|
2020-10-20 16:42:37 +00:00
|
|
|
loadDataFromFlag = "loaddata-from"
|
|
|
|
loadDataFromKey = "loaddata_from"
|
|
|
|
loadDataModeFlag = "loaddata-mode"
|
|
|
|
loadDataModeKey = "loaddata_mode"
|
|
|
|
loadDataQuotaScanFlag = "loaddata-scan"
|
|
|
|
loadDataQuotaScanKey = "loaddata_scan"
|
|
|
|
loadDataCleanFlag = "loaddata-clean"
|
|
|
|
loadDataCleanKey = "loaddata_clean"
|
|
|
|
defaultConfigDir = "."
|
2020-12-03 16:16:35 +00:00
|
|
|
defaultConfigFile = ""
|
2020-10-20 16:42:37 +00:00
|
|
|
defaultLogFile = "sftpgo.log"
|
|
|
|
defaultLogMaxSize = 10
|
|
|
|
defaultLogMaxBackup = 5
|
|
|
|
defaultLogMaxAge = 28
|
|
|
|
defaultLogCompress = false
|
|
|
|
defaultLogVerbose = true
|
2021-11-06 14:18:16 +00:00
|
|
|
defaultLogUTCTime = false
|
2020-10-20 16:42:37 +00:00
|
|
|
defaultLoadDataFrom = ""
|
|
|
|
defaultLoadDataMode = 1
|
|
|
|
defaultLoadDataQuotaScan = 0
|
|
|
|
defaultLoadDataClean = false
|
2019-08-07 20:46:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-20 16:42:37 +00:00
|
|
|
configDir string
|
|
|
|
configFile string
|
|
|
|
logFilePath string
|
|
|
|
logMaxSize int
|
|
|
|
logMaxBackups int
|
|
|
|
logMaxAge int
|
|
|
|
logCompress bool
|
|
|
|
logVerbose bool
|
2021-11-06 14:18:16 +00:00
|
|
|
logUTCTime bool
|
2020-10-20 16:42:37 +00:00
|
|
|
loadDataFrom string
|
|
|
|
loadDataMode int
|
|
|
|
loadDataQuotaScan int
|
|
|
|
loadDataClean bool
|
2022-05-07 10:50:49 +00:00
|
|
|
// used if awscontainer build tag is enabled
|
|
|
|
disableAWSInstallationCode bool
|
2019-09-16 06:52:58 +00:00
|
|
|
|
2019-08-07 20:46:13 +00:00
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "sftpgo",
|
2020-07-08 21:21:33 +00:00
|
|
|
Short: "Fully featured and highly configurable SFTP server",
|
2019-08-07 20:46:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-09-26 18:25:37 +00:00
|
|
|
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
2019-08-07 20:46:13 +00:00
|
|
|
rootCmd.Flags().BoolP("version", "v", false, "")
|
2020-06-19 15:08:51 +00:00
|
|
|
rootCmd.Version = version.GetAsString()
|
2020-05-23 09:58:05 +00:00
|
|
|
rootCmd.SetVersionTemplate(`{{printf "SFTPGo "}}{{printf "%s" .Version}}
|
2019-08-07 20:46:13 +00:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
func addConfigFlags(cmd *cobra.Command) {
|
2019-09-16 06:52:58 +00:00
|
|
|
viper.SetDefault(configDirKey, defaultConfigDir)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindEnv(configDirKey, "SFTPGO_CONFIG_DIR") //nolint:errcheck // err is not nil only if the key to bind is missing
|
2019-09-16 06:52:58 +00:00
|
|
|
cmd.Flags().StringVarP(&configDir, configDirFlag, "c", viper.GetString(configDirKey),
|
2020-12-03 16:16:35 +00:00
|
|
|
`Location for the config dir. This directory
|
|
|
|
is used as the base for files with a relative
|
|
|
|
path, eg. the private keys for the SFTP
|
|
|
|
server or the SQLite database if you use
|
|
|
|
SQLite as data provider.
|
|
|
|
The configuration file, if not explicitly set,
|
|
|
|
is looked for in this dir. We support reading
|
|
|
|
from JSON, TOML, YAML, HCL, envfile and Java
|
|
|
|
properties config files. The default config
|
|
|
|
file name is "sftpgo" and therefore
|
|
|
|
"sftpgo.json", "sftpgo.yaml" and so on are
|
|
|
|
searched.
|
2020-07-09 16:58:22 +00:00
|
|
|
This flag can be set using SFTPGO_CONFIG_DIR
|
|
|
|
env var too.`)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindPFlag(configDirKey, cmd.Flags().Lookup(configDirFlag)) //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
|
2020-12-03 16:16:35 +00:00
|
|
|
viper.SetDefault(configFileKey, defaultConfigFile)
|
|
|
|
viper.BindEnv(configFileKey, "SFTPGO_CONFIG_FILE") //nolint:errcheck
|
|
|
|
cmd.Flags().StringVar(&configFile, configFileFlag, viper.GetString(configFileKey),
|
|
|
|
`Path to SFTPGo configuration file.
|
|
|
|
This flag explicitly defines the path, name
|
|
|
|
and extension of the config file. If must be
|
|
|
|
an absolute path or a path relative to the
|
|
|
|
configuration directory. The specified file
|
|
|
|
name must have a supported extension (JSON,
|
|
|
|
YAML, TOML, HCL or Java properties).
|
2020-07-09 16:58:22 +00:00
|
|
|
This flag can be set using SFTPGO_CONFIG_FILE
|
|
|
|
env var too.`)
|
2020-12-03 16:16:35 +00:00
|
|
|
viper.BindPFlag(configFileKey, cmd.Flags().Lookup(configFileFlag)) //nolint:errcheck
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 16:05:18 +00:00
|
|
|
func addBaseLoadDataFlags(cmd *cobra.Command) {
|
|
|
|
viper.SetDefault(loadDataFromKey, defaultLoadDataFrom)
|
|
|
|
viper.BindEnv(loadDataFromKey, "SFTPGO_LOADDATA_FROM") //nolint:errcheck
|
|
|
|
cmd.Flags().StringVar(&loadDataFrom, loadDataFromFlag, viper.GetString(loadDataFromKey),
|
|
|
|
`Load users and folders from this file.
|
|
|
|
The file must be specified as absolute path
|
|
|
|
and it must contain a backup obtained using
|
|
|
|
the "dumpdata" REST API or compatible content.
|
|
|
|
This flag can be set using SFTPGO_LOADDATA_FROM
|
|
|
|
env var too.
|
|
|
|
`)
|
|
|
|
viper.BindPFlag(loadDataFromKey, cmd.Flags().Lookup(loadDataFromFlag)) //nolint:errcheck
|
|
|
|
|
|
|
|
viper.SetDefault(loadDataModeKey, defaultLoadDataMode)
|
|
|
|
viper.BindEnv(loadDataModeKey, "SFTPGO_LOADDATA_MODE") //nolint:errcheck
|
|
|
|
cmd.Flags().IntVar(&loadDataMode, loadDataModeFlag, viper.GetInt(loadDataModeKey),
|
|
|
|
`Restore mode for data to load:
|
|
|
|
0 - new users are added, existing users are
|
|
|
|
updated
|
|
|
|
1 - New users are added, existing users are
|
|
|
|
not modified
|
|
|
|
This flag can be set using SFTPGO_LOADDATA_MODE
|
|
|
|
env var too.
|
|
|
|
`)
|
|
|
|
viper.BindPFlag(loadDataModeKey, cmd.Flags().Lookup(loadDataModeFlag)) //nolint:errcheck
|
|
|
|
|
|
|
|
viper.SetDefault(loadDataCleanKey, defaultLoadDataClean)
|
|
|
|
viper.BindEnv(loadDataCleanKey, "SFTPGO_LOADDATA_CLEAN") //nolint:errcheck
|
|
|
|
cmd.Flags().BoolVar(&loadDataClean, loadDataCleanFlag, viper.GetBool(loadDataCleanKey),
|
|
|
|
`Determine if the loaddata-from file should
|
|
|
|
be removed after a successful load. This flag
|
|
|
|
can be set using SFTPGO_LOADDATA_CLEAN env var
|
|
|
|
too. (default "false")
|
|
|
|
`)
|
|
|
|
viper.BindPFlag(loadDataCleanKey, cmd.Flags().Lookup(loadDataCleanFlag)) //nolint:errcheck
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
func addServeFlags(cmd *cobra.Command) {
|
|
|
|
addConfigFlags(cmd)
|
2019-09-16 06:52:58 +00:00
|
|
|
|
|
|
|
viper.SetDefault(logFilePathKey, defaultLogFile)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindEnv(logFilePathKey, "SFTPGO_LOG_FILE_PATH") //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
cmd.Flags().StringVarP(&logFilePath, logFilePathFlag, "l", viper.GetString(logFilePathKey),
|
2020-07-09 16:58:22 +00:00
|
|
|
`Location for the log file. Leave empty to write
|
|
|
|
logs to the standard output. This flag can be
|
|
|
|
set using SFTPGO_LOG_FILE_PATH env var too.
|
|
|
|
`)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindPFlag(logFilePathKey, cmd.Flags().Lookup(logFilePathFlag)) //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
|
|
|
|
viper.SetDefault(logMaxSizeKey, defaultLogMaxSize)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindEnv(logMaxSizeKey, "SFTPGO_LOG_MAX_SIZE") //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
cmd.Flags().IntVarP(&logMaxSize, logMaxSizeFlag, "s", viper.GetInt(logMaxSizeKey),
|
2020-07-09 16:58:22 +00:00
|
|
|
`Maximum size in megabytes of the log file
|
|
|
|
before it gets rotated. This flag can be set
|
|
|
|
using SFTPGO_LOG_MAX_SIZE env var too. It is
|
|
|
|
unused if log-file-path is empty.
|
|
|
|
`)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindPFlag(logMaxSizeKey, cmd.Flags().Lookup(logMaxSizeFlag)) //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
|
|
|
|
viper.SetDefault(logMaxBackupKey, defaultLogMaxBackup)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindEnv(logMaxBackupKey, "SFTPGO_LOG_MAX_BACKUPS") //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
cmd.Flags().IntVarP(&logMaxBackups, "log-max-backups", "b", viper.GetInt(logMaxBackupKey),
|
2020-07-09 16:58:22 +00:00
|
|
|
`Maximum number of old log files to retain.
|
|
|
|
This flag can be set using SFTPGO_LOG_MAX_BACKUPS
|
|
|
|
env var too. It is unused if log-file-path is
|
|
|
|
empty.`)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindPFlag(logMaxBackupKey, cmd.Flags().Lookup(logMaxBackupFlag)) //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
|
|
|
|
viper.SetDefault(logMaxAgeKey, defaultLogMaxAge)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindEnv(logMaxAgeKey, "SFTPGO_LOG_MAX_AGE") //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
cmd.Flags().IntVarP(&logMaxAge, "log-max-age", "a", viper.GetInt(logMaxAgeKey),
|
2020-07-09 16:58:22 +00:00
|
|
|
`Maximum number of days to retain old log files.
|
|
|
|
This flag can be set using SFTPGO_LOG_MAX_AGE env
|
|
|
|
var too. It is unused if log-file-path is empty.
|
|
|
|
`)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindPFlag(logMaxAgeKey, cmd.Flags().Lookup(logMaxAgeFlag)) //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
|
|
|
|
viper.SetDefault(logCompressKey, defaultLogCompress)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindEnv(logCompressKey, "SFTPGO_LOG_COMPRESS") //nolint:errcheck
|
2020-07-09 16:58:22 +00:00
|
|
|
cmd.Flags().BoolVarP(&logCompress, logCompressFlag, "z", viper.GetBool(logCompressKey),
|
|
|
|
`Determine if the rotated log files
|
|
|
|
should be compressed using gzip. This flag can
|
|
|
|
be set using SFTPGO_LOG_COMPRESS env var too.
|
|
|
|
It is unused if log-file-path is empty.
|
|
|
|
`)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindPFlag(logCompressKey, cmd.Flags().Lookup(logCompressFlag)) //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
|
|
|
|
viper.SetDefault(logVerboseKey, defaultLogVerbose)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindEnv(logVerboseKey, "SFTPGO_LOG_VERBOSE") //nolint:errcheck
|
2020-07-09 16:58:22 +00:00
|
|
|
cmd.Flags().BoolVarP(&logVerbose, logVerboseFlag, "v", viper.GetBool(logVerboseKey),
|
|
|
|
`Enable verbose logs. This flag can be set
|
|
|
|
using SFTPGO_LOG_VERBOSE env var too.
|
|
|
|
`)
|
2020-05-06 17:36:34 +00:00
|
|
|
viper.BindPFlag(logVerboseKey, cmd.Flags().Lookup(logVerboseFlag)) //nolint:errcheck
|
2020-03-15 14:16:35 +00:00
|
|
|
|
2021-11-06 14:18:16 +00:00
|
|
|
viper.SetDefault(logUTCTimeKey, defaultLogUTCTime)
|
|
|
|
viper.BindEnv(logUTCTimeKey, "SFTPGO_LOG_UTC_TIME") //nolint:errcheck
|
|
|
|
cmd.Flags().BoolVar(&logUTCTime, logUTCTimeFlag, viper.GetBool(logUTCTimeKey),
|
|
|
|
`Use UTC time for logging. This flag can be set
|
|
|
|
using SFTPGO_LOG_UTC_TIME env var too.
|
|
|
|
`)
|
|
|
|
viper.BindPFlag(logUTCTimeKey, cmd.Flags().Lookup(logUTCTimeFlag)) //nolint:errcheck
|
|
|
|
|
2022-02-28 16:05:18 +00:00
|
|
|
addBaseLoadDataFlags(cmd)
|
2020-10-20 16:42:37 +00:00
|
|
|
|
|
|
|
viper.SetDefault(loadDataQuotaScanKey, defaultLoadDataQuotaScan)
|
|
|
|
viper.BindEnv(loadDataQuotaScanKey, "SFTPGO_LOADDATA_QUOTA_SCAN") //nolint:errcheck
|
|
|
|
cmd.Flags().IntVar(&loadDataQuotaScan, loadDataQuotaScanFlag, viper.GetInt(loadDataQuotaScanKey),
|
|
|
|
`Quota scan mode after data load:
|
|
|
|
0 - no quota scan
|
|
|
|
1 - scan quota
|
|
|
|
2 - scan quota if the user has quota restrictions
|
|
|
|
This flag can be set using SFTPGO_LOADDATA_QUOTA_SCAN
|
|
|
|
env var too.
|
|
|
|
(default 0)`)
|
|
|
|
viper.BindPFlag(loadDataQuotaScanKey, cmd.Flags().Lookup(loadDataQuotaScanFlag)) //nolint:errcheck
|
2019-09-16 06:52:58 +00:00
|
|
|
}
|