Change config behavior

This commit is contained in:
Thomas Miceli 2023-04-07 23:58:06 +02:00
parent db36ec6002
commit 31bec62834
No known key found for this signature in database
GPG key ID: D86C6F6390AF050F
3 changed files with 29 additions and 15 deletions

View file

@ -31,7 +31,7 @@ watch_frontend:
watch_backend: watch_backend:
@echo "Building Opengist binary..." @echo "Building Opengist binary..."
DEV=1 ./node_modules/.bin/nodemon --watch '**/*' -e html,yml,go,js --signal SIGTERM --exec 'go' run . DEV=1 ./node_modules/.bin/nodemon --watch '**/*' -e html,yml,go,js --signal SIGTERM --exec 'go run . --config config.yml'
watch: watch:
@bash ./watch.sh @bash ./watch.sh

View file

@ -70,16 +70,27 @@ func InitConfig(configPath string) error {
return err return err
} }
file, err := os.Open(configPath) if configPath != "" {
if err == nil { absolutePath, _ := filepath.Abs(configPath)
fmt.Println("Using config file: " + configPath) absolutePath = filepath.Clean(absolutePath)
file, err := os.Open(absolutePath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
fmt.Println("No YML config file found at " + absolutePath)
} else {
fmt.Println("Using config file: " + absolutePath)
// Override default values with values from config.yml // Override default values with values from config.yml
d := yaml.NewDecoder(file) d := yaml.NewDecoder(file)
if err = d.Decode(&c); err != nil { if err = d.Decode(&c); err != nil {
return err return err
}
defer file.Close()
} }
defer file.Close() } else {
fmt.Println("No config file specified. Using default values.")
} }
// Override default values with environment variables (as yaml) // Override default values with environment variables (as yaml)
@ -105,7 +116,6 @@ func InitLog() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
multi := zerolog.MultiLevelWriter(zerolog.NewConsoleWriter(), file)
var level zerolog.Level var level zerolog.Level
level, err = zerolog.ParseLevel(C.LogLevel) level, err = zerolog.ParseLevel(C.LogLevel)
@ -113,7 +123,12 @@ func InitLog() {
level = zerolog.InfoLevel level = zerolog.InfoLevel
} }
log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger() if os.Getenv("DEV") == "1" {
multi := zerolog.MultiLevelWriter(zerolog.NewConsoleWriter(), file)
log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger()
} else {
log.Logger = zerolog.New(file).Level(level).With().Timestamp().Logger()
}
} }
func CheckGitVersion(version string) (bool, error) { func CheckGitVersion(version string) (bool, error) {

View file

@ -16,11 +16,10 @@ import (
func initialize() { func initialize() {
fmt.Println("Opengist v" + config.OpengistVersion) fmt.Println("Opengist v" + config.OpengistVersion)
configPath := flag.String("config", "config.yml", "Path to a config file in YML format") configPath := flag.String("config", "", "Path to a config file in YML format")
flag.Parse() flag.Parse()
absolutePath, _ := filepath.Abs(*configPath)
absolutePath = filepath.Clean(absolutePath) if err := config.InitConfig(*configPath); err != nil {
if err := config.InitConfig(absolutePath); err != nil {
panic(err) panic(err)
} }
if err := os.MkdirAll(filepath.Join(config.GetHomeDir()), 0755); err != nil { if err := os.MkdirAll(filepath.Join(config.GetHomeDir()), 0755); err != nil {