Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"crypto/rand"
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -527,6 +528,33 @@ func setConfigFromEnv() error {
return viper.MergeConfigMap(configMap)
}

// mergeConfigFromPath loads a config file from the given directory and merges
// it into the main viper instance. Viper itself only reads the first config
// file found when multiple paths are configured, so we manually merge any
// additional configs found in subsequent paths.
func mergeConfigFromPath(p string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't viper do this already?

if p == "" {
return
}

tmp := viper.New()
tmp.SetConfigName("config")
tmp.AddConfigPath(p)

if err := tmp.ReadInConfig(); err != nil {
var notFound viper.ConfigFileNotFoundError
if errors.As(err, &notFound) {
return
}
log.Warning(err.Error())
log.Warning("Using default config.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only true if no other config file could be read either.

return
}

log.Infof("Using config file: %s", tmp.ConfigFileUsed())
_ = viper.MergeConfigMap(tmp.AllSettings())
}

// InitConfig initializes the config, sets defaults etc.
func InitConfig() {

Expand All @@ -540,33 +568,22 @@ func InitConfig() {

log.ConfigureLogger(LogEnabled.GetBool(), LogStandard.GetString(), LogPath.GetString(), LogLevel.GetString())

// Load the config file
viper.AddConfigPath(ServiceRootpath.GetString())
viper.AddConfigPath("/etc/vikunja/")
// Load the config file from all known locations
rootPath := ServiceRootpath.GetString()
mergeConfigFromPath(rootPath)
mergeConfigFromPath("/etc/vikunja/")

homeDir, err := os.UserHomeDir()
if err != nil {
log.Debugf("No home directory found, not using config from ~/.config/vikunja/. Error was: %s\n", err.Error())
} else {
viper.AddConfigPath(path.Join(homeDir, ".config", "vikunja"))
mergeConfigFromPath(path.Join(homeDir, ".config", "vikunja"))
}

viper.AddConfigPath(".")
viper.SetConfigName("config")

err = viper.ReadInConfig()

if viper.ConfigFileUsed() != "" {
log.Infof("Using config file: %s", viper.ConfigFileUsed())
mergeConfigFromPath(".")

if err != nil {
log.Warning(err.Error())
log.Warning("Using default config.")
} else {
log.ConfigureLogger(LogEnabled.GetBool(), LogStandard.GetString(), LogPath.GetString(), LogLevel.GetString())
}
} else {
log.Info("No config file found, using default or config from environment variables.")
if ServiceRootpath.GetString() != rootPath {
mergeConfigFromPath(ServiceRootpath.GetString())
}

err = setConfigFromEnv()
Expand Down
5 changes: 4 additions & 1 deletion pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ func initPostgresEngine() (engine *xorm.Engine, err error) {

func initSqliteEngine() (engine *xorm.Engine, err error) {
path := config.DatabasePath.GetString()
if path == "" {
path = "vikunja.db"
}

if path != "memory" && !filepath.IsAbs(path) {
if !filepath.IsAbs(path) && path != "memory" {
path = filepath.Join(config.ServiceRootpath.GetString(), path)
}

Expand Down
Loading