From 55c992c3b53734527ad4ba8bf40ec3c140504cef Mon Sep 17 00:00:00 2001 From: Huskydog9988 <39809509+Huskydog9988@users.noreply.github.com> Date: Thu, 4 Jan 2024 19:57:21 -0500 Subject: [PATCH] add support for more config locations now support /etc/docker-db-backup/config.yaml aside from current directiry --- config.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 12 ++---------- 2 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..5a5ad52 --- /dev/null +++ b/config.go @@ -0,0 +1,54 @@ +package main + +import ( + "os" + + "github.com/knadh/koanf/parsers/yaml" + "github.com/knadh/koanf/providers/file" + "github.com/knadh/koanf/v2" + "github.com/rotisserie/eris" + log "github.com/sirupsen/logrus" +) + +// Global koanf instance. Use "." as the key path delimiter. This can be "/" or any character. +var k = koanf.New(".") + +// loadConfigFile loads the config file from the current directory and /etc/docker-db-backup/config.yaml +func loadConfigFile() { + configPath := "config.yaml" + + if checkIfFileExists("config.yaml") { + // do nothing + } else if checkIfFileExists("/etc/docker-db-backup/config.yaml") { + configPath = "/etc/docker-db-backup/config.yaml" + } else { + log.Fatal("Config file not found") + } + + log.Infof("Loading config file: %s", configPath) + + // Load yaml config. + if err := k.Load(file.Provider(configPath), yaml.Parser()); err != nil { + log.Fatal(eris.Wrap(err, "failed to load config")) + } +} + +// utility function to check if a file exists +func checkIfFileExists(path string) bool { + // based on + // https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go + if _, err := os.Stat(path); err == nil { + // path/to/whatever exists + return true + } else if eris.Is(err, os.ErrNotExist) { + // path/to/whatever does *not* exist + return false + } else { + // Schrodinger: file may or may not exist. See err for details. + + // Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence + return false + } + + // return false +} diff --git a/main.go b/main.go index ebc1b24..f1c4ba8 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,6 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/client" "github.com/go-co-op/gocron/v2" - "github.com/knadh/koanf/parsers/yaml" - "github.com/knadh/koanf/providers/file" - "github.com/knadh/koanf/v2" "github.com/rotisserie/eris" log "github.com/sirupsen/logrus" ) @@ -23,9 +20,6 @@ type JobConfig struct { Config map[string]string } -// Global koanf instance. Use "." as the key path delimiter. This can be "/" or any character. -var k = koanf.New(".") - func init() { // Log as JSON instead of the default ASCII formatter. // log.SetFormatter(&log.JSONFormatter{}) @@ -43,10 +37,8 @@ func init() { func main() { log.Info("Starting backup service") - // Load yaml config. - if err := k.Load(file.Provider("config.yaml"), yaml.Parser()); err != nil { - log.Fatal(eris.Wrap(err, "failed to load config")) - } + // load config file + loadConfigFile() // create backup folder createBackupFolder()