From ad01d78765481f299b8b1869eda68697267493fc Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Fri, 21 Jan 2022 23:23:23 -0800 Subject: [PATCH] Fix issues with db_path config arg Part of https://git.nagee.dev/isthisnagee/diary/issues/6 --- cmd/util.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/cmd/util.go b/cmd/util.go index 83262a2..7182910 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -39,24 +39,22 @@ func initConfig() Cfg { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - - // Search config in home directory with name ".diary" (without extension). - viper.AddConfigPath(home) + viper.AddConfigPath(path.Join(home, ".config", "diary")) viper.SetConfigType("toml") - viper.SetConfigName(".diary") + viper.SetConfigName("diary.toml") } - var db_path = path.Join(home, ".diary.sql") - viper.SetDefault("db_path", db_path) + if viper.Get("db_path") == nil { + var db_path = path.Join(home, ".diary.sql") + viper.SetDefault("db_path", db_path) + } viper.AutomaticEnv() // read in environment variables that match - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) - } + err = viper.ReadInConfig() + cobra.CheckErr(err) - return Cfg{db_path} + return Cfg{viper.GetString("db_path")} } func InitApp() { @@ -64,9 +62,7 @@ func InitApp() { if _, err := os.Stat(cfg.DbPath); errors.Is(err, os.ErrNotExist) { _, err := os.Create(cfg.DbPath) - if err != nil { - log.Fatal(err.Error()) - } + cobra.CheckErr(err) } var app = model.NewApp(cfg.DbPath)