mirror of
https://github.com/garethgeorge/backrest.git
synced 2026-08-28 03:46:29 +00:00
feat: add flags to configure backrest options e.g. --config-file, --data-dir, --restic-cmd, --bind-address
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
@@ -15,10 +16,18 @@ var (
|
||||
EnvVarBinPath = "BACKREST_RESTIC_COMMAND" // path to restic binary (default restic)
|
||||
)
|
||||
|
||||
var flagDataDir = flag.String("data-dir", "", "path to data directory, defaults to XDG_DATA_HOME/.local/backrest. Overrides BACKREST_DATA environment variable.")
|
||||
var flagConfigPath = flag.String("config-file", "", "path to config file, defaults to XDG_CONFIG_HOME/backrest/config.json. Overrides BACKREST_CONFIG environment variable.")
|
||||
var flagBindAddress = flag.String("bind-address", "", "address to bind to, defaults to :9898. Use 127.0.0.1:9898 to listen only on localhost. Overrides BACKREST_PORT environment variable.")
|
||||
var flagResticBinPath = flag.String("restic-cmd", "", "path to restic binary, defaults to a backrest managed version of restic. Overrides BACKREST_RESTIC_COMMAND environment variable.")
|
||||
|
||||
// ConfigFilePath
|
||||
// - *nix systems use $XDG_CONFIG_HOME/backrest/config.json
|
||||
// - windows uses %APPDATA%/backrest/config.json
|
||||
func ConfigFilePath() string {
|
||||
if *flagConfigPath != "" {
|
||||
return *flagConfigPath
|
||||
}
|
||||
if val := os.Getenv(EnvVarConfigPath); val != "" {
|
||||
return val
|
||||
}
|
||||
@@ -29,6 +38,9 @@ func ConfigFilePath() string {
|
||||
// - *nix systems use $XDG_DATA_HOME/backrest
|
||||
// - windows uses %APPDATA%/backrest/data
|
||||
func DataDir() string {
|
||||
if *flagDataDir != "" {
|
||||
return *flagDataDir
|
||||
}
|
||||
if val := os.Getenv(EnvVarDataDir); val != "" {
|
||||
return val
|
||||
}
|
||||
@@ -43,16 +55,19 @@ func DataDir() string {
|
||||
}
|
||||
|
||||
func BindAddress() string {
|
||||
if *flagBindAddress != "" {
|
||||
return formatBindAddress(*flagBindAddress)
|
||||
}
|
||||
if val := os.Getenv(EnvVarBindAddress); val != "" {
|
||||
if !strings.Contains(val, ":") {
|
||||
return ":" + val
|
||||
}
|
||||
return val
|
||||
return formatBindAddress(val)
|
||||
}
|
||||
return ":9898"
|
||||
}
|
||||
|
||||
func ResticBinPath() string {
|
||||
if *flagResticBinPath != "" {
|
||||
return *flagResticBinPath
|
||||
}
|
||||
if val := os.Getenv(EnvVarBinPath); val != "" {
|
||||
return val
|
||||
}
|
||||
@@ -80,3 +95,10 @@ func getConfigDir() string {
|
||||
}
|
||||
return path.Join(getHomeDir(), ".config")
|
||||
}
|
||||
|
||||
func formatBindAddress(addr string) string {
|
||||
if !strings.Contains(addr, ":") {
|
||||
return ":" + addr
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user