mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 23:00:55 +03:00
allow to set default arguments values from env vars
This commit is contained in:
15
README.md
15
README.md
@@ -49,13 +49,14 @@ Alternately you can use distro packages:
|
|||||||
|
|
||||||
The `sftpgo` executable supports the following command line flags:
|
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 "."
|
- `--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
|
||||||
- `--log-file-path` string. Location for the log file, default "sftpgo.log"
|
- `--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-max-size` int. Maximum size in megabytes of the log file before it gets rotated. Default 10
|
- `--log-file-path` string. Location for the log file, default "sftpgo.log" or the value of `SFTPGO_LOG_FILE_PATH` environment variable
|
||||||
- `--log-max-backups` int. Maximum number of old log files to retain. Default 5
|
- `--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-age` int. Maximum number of days to retain old log files. Default 28
|
- `--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-compress` boolean. Determine if the rotated log files should be compressed using gzip
|
- `--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-verbose` boolean. Enable verbose logs. Default `true`
|
- `--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).
|
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).
|
||||||
|
|
||||||
|
|||||||
27
main.go
27
main.go
@@ -20,15 +20,16 @@ import (
|
|||||||
"github.com/drakkan/sftpgo/dataprovider"
|
"github.com/drakkan/sftpgo/dataprovider"
|
||||||
"github.com/drakkan/sftpgo/logger"
|
"github.com/drakkan/sftpgo/logger"
|
||||||
"github.com/drakkan/sftpgo/sftpd"
|
"github.com/drakkan/sftpgo/sftpd"
|
||||||
|
"github.com/drakkan/sftpgo/utils"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
confName := "sftpgo.conf"
|
|
||||||
logSender := "main"
|
logSender := "main"
|
||||||
var (
|
var (
|
||||||
configDir string
|
configDir string
|
||||||
|
configFileName string
|
||||||
logFilePath string
|
logFilePath string
|
||||||
logMaxSize int
|
logMaxSize int
|
||||||
logMaxBackups int
|
logMaxBackups int
|
||||||
@@ -36,17 +37,23 @@ func main() {
|
|||||||
logCompress bool
|
logCompress bool
|
||||||
logVerbose bool
|
logVerbose bool
|
||||||
)
|
)
|
||||||
flag.StringVar(&configDir, "config-dir", ".", "Location for SFTPGo config dir. It must contain sftpgo.conf "+
|
flag.StringVar(&configDir, "config-dir", utils.GetEnvVar("SFTPGO_CONFIG_DIR", "."), "Location for SFTPGo config dir. It must contain "+
|
||||||
"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).")
|
"sftpgo.conf or the configured config-file-name and it is used as the base for files with a relative path (eg. the private "+
|
||||||
flag.StringVar(&logFilePath, "log-file-path", "sftpgo.log", "Location for the log file")
|
"keys for the SFTP server, the SQLite database if you use SQLite as data provider).")
|
||||||
flag.IntVar(&logMaxSize, "log-max-size", 10, "Maximum size in megabytes of the log file before it gets rotated.")
|
flag.StringVar(&configFileName, "config-file-name", utils.GetEnvVar("SFTPGO_CONFIG_FILE_NAME", "sftpgo.conf"), "Name for SFTPGo "+
|
||||||
flag.IntVar(&logMaxBackups, "log-max-backups", 5, "Maximum number of old log files to retain")
|
"configuration file. It must be the name of a file stored in config-dir not the absolute path to the configuration file")
|
||||||
flag.IntVar(&logMaxAge, "log-max-age", 28, "Maximum number of days to retain old log files")
|
flag.StringVar(&logFilePath, "log-file-path", utils.GetEnvVar("SFTPGO_LOG_FILE_PATH", "sftpgo.log"), "Location for the log file")
|
||||||
flag.BoolVar(&logCompress, "log-compress", false, "Determine if the rotated log files should be compressed using gzip")
|
flag.IntVar(&logMaxSize, "log-max-size", utils.GetEnvVarAsInt("SFTPGO_LOG_MAX_SIZE", 10), "Maximum size in megabytes of the log file "+
|
||||||
flag.BoolVar(&logVerbose, "log-verbose", true, "Enable verbose logs")
|
"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()
|
flag.Parse()
|
||||||
|
|
||||||
configFilePath := filepath.Join(configDir, confName)
|
configFilePath := filepath.Join(configDir, configFileName)
|
||||||
logLevel := zerolog.DebugLevel
|
logLevel := zerolog.DebugLevel
|
||||||
if !logVerbose {
|
if !logVerbose {
|
||||||
logLevel = zerolog.InfoLevel
|
logLevel = zerolog.InfoLevel
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/drakkan/sftpgo/logger"
|
"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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user