Skip to content

Commit

Permalink
add support for more config locations
Browse files Browse the repository at this point in the history
now support /etc/docker-db-backup/config.yaml aside from current directiry
  • Loading branch information
Huskydog9988 committed Jan 5, 2024
1 parent 5ae3650 commit 55c992c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
54 changes: 54 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 2 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{})
Expand All @@ -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()
Expand Down

0 comments on commit 55c992c

Please sign in to comment.