|
|
@ -39,24 +39,22 @@ func initConfig() Cfg {
|
|
|
|
// Use config file from the flag.
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
viper.AddConfigPath(path.Join(home, ".config", "diary"))
|
|
|
|
// Search config in home directory with name ".diary" (without extension).
|
|
|
|
|
|
|
|
viper.AddConfigPath(home)
|
|
|
|
|
|
|
|
viper.SetConfigType("toml")
|
|
|
|
viper.SetConfigType("toml")
|
|
|
|
viper.SetConfigName(".diary")
|
|
|
|
viper.SetConfigName("diary.toml")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if viper.Get("db_path") == nil {
|
|
|
|
var db_path = path.Join(home, ".diary.sql")
|
|
|
|
var db_path = path.Join(home, ".diary.sql")
|
|
|
|
viper.SetDefault("db_path", db_path)
|
|
|
|
viper.SetDefault("db_path", db_path)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
err = viper.ReadInConfig()
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Cfg{db_path}
|
|
|
|
return Cfg{viper.GetString("db_path")}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func InitApp() {
|
|
|
|
func InitApp() {
|
|
|
@ -64,13 +62,13 @@ func InitApp() {
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(cfg.DbPath); errors.Is(err, os.ErrNotExist) {
|
|
|
|
if _, err := os.Stat(cfg.DbPath); errors.Is(err, os.ErrNotExist) {
|
|
|
|
_, err := os.Create(cfg.DbPath)
|
|
|
|
_, err := os.Create(cfg.DbPath)
|
|
|
|
if err != nil {
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var app = model.NewApp(cfg.DbPath)
|
|
|
|
var app, err = model.NewApp(cfg.DbPath)
|
|
|
|
App = &TApp{&app, &cfg}
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
App = &TApp{app, &cfg}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var fmt_str = "%-10s %-20s %s\n"
|
|
|
|
var fmt_str = "%-10s %-20s %s\n"
|
|
|
|