-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for more config locations
now support /etc/docker-db-backup/config.yaml aside from current directiry
- Loading branch information
1 parent
5ae3650
commit 55c992c
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters