diff --git a/README.md b/README.md index 5fa665bb..0def98aa 100644 --- a/README.md +++ b/README.md @@ -49,13 +49,14 @@ Alternately you can use distro packages: The `sftpgo` executable supports the following command line flags: -- `--config-dir` string. Location of the config dir. This directory should contain the `sftpgo.conf` configuration file and is used as the base for files with a relative path (eg. the private keys for the SFTP server, the SQLite database if you use SQLite as data provider). The default value is "." -- `--log-file-path` string. Location for the log file, default "sftpgo.log" -- `--log-max-size` int. Maximum size in megabytes of the log file before it gets rotated. Default 10 -- `--log-max-backups` int. Maximum number of old log files to retain. Default 5 -- `--log-max-age` int. Maximum number of days to retain old log files. Default 28 -- `--log-compress` boolean. Determine if the rotated log files should be compressed using gzip -- `--log-verbose` boolean. Enable verbose logs. Default `true` +- `--config-dir` string. Location of the config dir. This directory should contain the `sftpgo.conf` configuration file and is used as the base for files with a relative path (eg. the private keys for the SFTP server, the SQLite database if you use SQLite as data provider). The default value is "." or the value of `SFTPGO_CONFIG_DIR` environment variable +- `--config-file-name` string. Name of the configuration file. It must be the name of a file stored in config-dir not the absolute path to the configuration file. The default value is "sftpgo.conf" or the value of `SFTPGO_CONFIG_FILE_NAME` environment variable +- `--log-file-path` string. Location for the log file, default "sftpgo.log" or the value of `SFTPGO_LOG_FILE_PATH` environment variable +- `--log-max-size` int. Maximum size in megabytes of the log file before it gets rotated. Default 10 or the value of `SFTPGO_LOG_MAX_SIZE` environment variable +- `--log-max-backups` int. Maximum number of old log files to retain. Default 5 or the value of `SFTPGO_LOG_MAX_BACKUPS` environment variable +- `--log-max-age` int. Maximum number of days to retain old log files. Default 28 or the value of `SFTPGO_LOG_MAX_AGE` environment variable +- `--log-compress` boolean. Determine if the rotated log files should be compressed using gzip. Default `false` or the integer value of `SFTPGO_LOG_COMPRESS` environment variable (> 0 is `true`, 0 or invalid integer is `false`) +- `--log-verbose` boolean. Enable verbose logs. Default `true` or the integer value of `SFTPGO_LOG_VERBOSE` environment variable (> 0 is `true`, 0 or invalid integer is `false`) If you don't configure any private host keys, the daemon will use `id_rsa` in the configuration directory. If that file doesn't exist, the daemon will attempt to autogenerate it (if the user that executes SFTPGo has write access to the config-dir). The server supports any private key format supported by [`crypto/ssh`](https://github.com/golang/crypto/blob/master/ssh/keys.go#L32). diff --git a/main.go b/main.go index d54fa47b..e55eeb51 100644 --- a/main.go +++ b/main.go @@ -20,33 +20,40 @@ import ( "github.com/drakkan/sftpgo/dataprovider" "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/sftpd" + "github.com/drakkan/sftpgo/utils" "github.com/rs/zerolog" ) func main() { - confName := "sftpgo.conf" logSender := "main" var ( - configDir string - logFilePath string - logMaxSize int - logMaxBackups int - logMaxAge int - logCompress bool - logVerbose bool + configDir string + configFileName string + logFilePath string + logMaxSize int + logMaxBackups int + logMaxAge int + logCompress bool + logVerbose bool ) - flag.StringVar(&configDir, "config-dir", ".", "Location for SFTPGo config dir. It must contain sftpgo.conf "+ - "and is used as the base for files with a relative path (eg. the private keys for the SFTP server, the SQLite database if you use SQLite as data provider).") - flag.StringVar(&logFilePath, "log-file-path", "sftpgo.log", "Location for the log file") - flag.IntVar(&logMaxSize, "log-max-size", 10, "Maximum size in megabytes of the log file before it gets rotated.") - flag.IntVar(&logMaxBackups, "log-max-backups", 5, "Maximum number of old log files to retain") - flag.IntVar(&logMaxAge, "log-max-age", 28, "Maximum number of days to retain old log files") - flag.BoolVar(&logCompress, "log-compress", false, "Determine if the rotated log files should be compressed using gzip") - flag.BoolVar(&logVerbose, "log-verbose", true, "Enable verbose logs") + flag.StringVar(&configDir, "config-dir", utils.GetEnvVar("SFTPGO_CONFIG_DIR", "."), "Location for SFTPGo config dir. It must contain "+ + "sftpgo.conf or the configured config-file-name and it is used as the base for files with a relative path (eg. the private "+ + "keys for the SFTP server, the SQLite database if you use SQLite as data provider).") + flag.StringVar(&configFileName, "config-file-name", utils.GetEnvVar("SFTPGO_CONFIG_FILE_NAME", "sftpgo.conf"), "Name for SFTPGo "+ + "configuration file. It must be the name of a file stored in config-dir not the absolute path to the configuration file") + flag.StringVar(&logFilePath, "log-file-path", utils.GetEnvVar("SFTPGO_LOG_FILE_PATH", "sftpgo.log"), "Location for the log file") + flag.IntVar(&logMaxSize, "log-max-size", utils.GetEnvVarAsInt("SFTPGO_LOG_MAX_SIZE", 10), "Maximum size in megabytes of the log file "+ + "before it gets rotated.") + flag.IntVar(&logMaxBackups, "log-max-backups", utils.GetEnvVarAsInt("SFTPGO_LOG_MAX_BACKUPS", 5), "Maximum number of old log files "+ + "to retain") + flag.IntVar(&logMaxAge, "log-max-age", utils.GetEnvVarAsInt("SFTPGO_LOG_MAX_AGE", 28), "Maximum number of days to retain old log files") + flag.BoolVar(&logCompress, "log-compress", utils.GetEnvVarAsInt("SFTPGO_LOG_COMPRESS", 0) > 0, "Determine if the rotated log files "+ + "should be compressed using gzip") + flag.BoolVar(&logVerbose, "log-verbose", utils.GetEnvVarAsInt("SFTPGO_LOG_VERBOSE", 1) > 0, "Enable verbose logs") flag.Parse() - configFilePath := filepath.Join(configDir, confName) + configFilePath := filepath.Join(configDir, configFileName) logLevel := zerolog.DebugLevel if !logVerbose { logLevel = zerolog.InfoLevel diff --git a/utils/utils.go b/utils/utils.go index bc5c390b..227ec5ef 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "time" "github.com/drakkan/sftpgo/logger" @@ -69,3 +70,23 @@ func SetPathPermissions(path string, uid int, gid int) { } } } + +// GetEnvVar retrieves the value of the environment variable named +// by the key. If the variable is present in the environment the it +// returns the fallback value +func GetEnvVar(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} + +// GetEnvVarAsInt retrieves the value of the environment variable named +// by the key and returns its value or fallback +func GetEnvVarAsInt(key string, fallback int) int { + stringValue := GetEnvVar(key, strconv.Itoa(fallback)) + if value, err := strconv.Atoi(stringValue); err == nil { + return value + } + return fallback +}