From 1bbb7a9c1fe4c073cb24a07099848a649781a955 Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Tue, 3 Jan 2023 11:45:10 +0100 Subject: [PATCH] Use config locations for import and ffmigrage --- app/ffmigrate/main.go | 4 ++- app/import/main.go | 4 ++- config/store/location.go | 53 ++++++++++++++++++++++++++++++++++++++++ main.go | 52 ++------------------------------------- 4 files changed, 61 insertions(+), 52 deletions(-) create mode 100644 config/store/location.go diff --git a/app/ffmigrate/main.go b/app/ffmigrate/main.go index 036af80f..5553ec0e 100644 --- a/app/ffmigrate/main.go +++ b/app/ffmigrate/main.go @@ -22,7 +22,9 @@ func main() { "to": "ffmpeg5", }) - configstore, err := cfgstore.NewJSON(os.Getenv("CORE_CONFIGFILE"), nil) + configfile := cfgstore.Location(os.Getenv("CORE_CONFIGFILE")) + + configstore, err := cfgstore.NewJSON(configfile, nil) if err != nil { logger.Error().WithError(err).Log("Loading configuration failed") os.Exit(1) diff --git a/app/import/main.go b/app/import/main.go index ebfe1aa0..cc2a9fb3 100644 --- a/app/import/main.go +++ b/app/import/main.go @@ -15,7 +15,9 @@ import ( func main() { logger := log.New("Import").WithOutput(log.NewConsoleWriter(os.Stderr, log.Linfo, true)).WithField("version", "v1") - configstore, err := cfgstore.NewJSON(os.Getenv("CORE_CONFIGFILE"), nil) + configfile := cfgstore.Location(os.Getenv("CORE_CONFIGFILE")) + + configstore, err := cfgstore.NewJSON(configfile, nil) if err != nil { logger.Error().WithError(err).Log("Loading configuration failed") os.Exit(1) diff --git a/config/store/location.go b/config/store/location.go new file mode 100644 index 00000000..e073a0c8 --- /dev/null +++ b/config/store/location.go @@ -0,0 +1,53 @@ +package store + +import ( + "os" + "path" +) + +// Location returns the path to the config file. If no path is provided, +// different standard location will be probed: +// - os.UserConfigDir() + /datarhei-core/config.js +// - os.UserHomeDir() + /.config/datarhei-core/config.js +// - ./config/config.js +// If the config doesn't exist in none of these locations, it will be assumed +// at ./config/config.js +func Location(filepath string) string { + configfile := filepath + if len(configfile) != 0 { + return configfile + } + + locations := []string{} + + if dir, err := os.UserConfigDir(); err == nil { + locations = append(locations, dir+"/datarhei-core/config.js") + } + + if dir, err := os.UserHomeDir(); err == nil { + locations = append(locations, dir+"/.config/datarhei-core/config.js") + } + + locations = append(locations, "./config/config.js") + + for _, path := range locations { + info, err := os.Stat(path) + if err != nil { + continue + } + + if info.IsDir() { + continue + } + + configfile = path + } + + if len(configfile) == 0 { + configfile = "./config/config.js" + } + + os.MkdirAll(path.Dir(configfile), 0740) + + return configfile +} diff --git a/main.go b/main.go index 6d16a134..377af7e5 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,9 @@ package main import ( "os" "os/signal" - "path" "github.com/datarhei/core/v16/app/api" + "github.com/datarhei/core/v16/config/store" "github.com/datarhei/core/v16/log" _ "github.com/joho/godotenv/autoload" @@ -14,7 +14,7 @@ import ( func main() { logger := log.New("Core").WithOutput(log.NewConsoleWriter(os.Stderr, log.Lwarn, true)) - configfile := findConfigfile() + configfile := store.Location(os.Getenv("CORE_CONFIGFILE")) app, err := api.New(configfile, os.Stderr) if err != nil { @@ -57,51 +57,3 @@ func main() { // Stop the app app.Destroy() } - -// findConfigfie returns the path to the config file. If no path is given -// in the environment variable CORE_CONFIGFILE, different standard location -// will be probed: -// - os.UserConfigDir() + /datarhei-core/config.js -// - os.UserHomeDir() + /.config/datarhei-core/config.js -// - ./config/config.js -// If the config doesn't exist in none of these locations, it will be assumed -// at ./config/config.js -func findConfigfile() string { - configfile := os.Getenv("CORE_CONFIGFILE") - if len(configfile) != 0 { - return configfile - } - - locations := []string{} - - if dir, err := os.UserConfigDir(); err == nil { - locations = append(locations, dir+"/datarhei-core/config.js") - } - - if dir, err := os.UserHomeDir(); err == nil { - locations = append(locations, dir+"/.config/datarhei-core/config.js") - } - - locations = append(locations, "./config/config.js") - - for _, path := range locations { - info, err := os.Stat(path) - if err != nil { - continue - } - - if info.IsDir() { - continue - } - - configfile = path - } - - if len(configfile) == 0 { - configfile = "./config/config.js" - } - - os.MkdirAll(path.Dir(configfile), 0740) - - return configfile -}