mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 04:16:25 +08:00
Test different standard location for config file
If no path is given in the environment variable CORE_CONFIGFILE, different standard locations will be probed: - os.UserConfigDir() + /datarhei-core/config.js - os.UserHomeDir() + /.config/datarhei-core/config.js - ./config/config.js If the config.js doesn't exist in any of these locations, it will be assumed at ./config/config.js
This commit is contained in:
@@ -229,6 +229,8 @@ func (a *api) Reload() error {
|
||||
|
||||
logger.Info().WithFields(logfields).Log("")
|
||||
|
||||
logger.Info().WithField("path", a.config.path).Log("Read config file")
|
||||
|
||||
configlogger := logger.WithComponent("Config")
|
||||
cfg.Messages(func(level string, v configvars.Variable, message string) {
|
||||
configlogger = configlogger.WithFields(log.Fields{
|
||||
|
@@ -118,6 +118,10 @@ func (c *jsonStore) load(cfg *config.Config) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(jsondata) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := migrate(jsondata)
|
||||
if err != nil {
|
||||
return err
|
||||
|
53
main.go
53
main.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
|
||||
"github.com/datarhei/core/v16/app/api"
|
||||
"github.com/datarhei/core/v16/log"
|
||||
@@ -13,7 +14,9 @@ import (
|
||||
func main() {
|
||||
logger := log.New("Core").WithOutput(log.NewConsoleWriter(os.Stderr, log.Lwarn, true))
|
||||
|
||||
app, err := api.New(os.Getenv("CORE_CONFIGFILE"), os.Stderr)
|
||||
configfile := findConfigfile()
|
||||
|
||||
app, err := api.New(configfile, os.Stderr)
|
||||
if err != nil {
|
||||
logger.Error().WithError(err).Log("Failed to create new API")
|
||||
os.Exit(1)
|
||||
@@ -54,3 +57,51 @@ 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user