diff --git a/config/init.go b/config/init.go new file mode 100644 index 0000000..d2c709d --- /dev/null +++ b/config/init.go @@ -0,0 +1,19 @@ +package config + +import ( + "github.com/Yakiyo/tilde/where" + v "github.com/spf13/viper" +) + +func init() { + v.SetConfigType("toml") + v.SetConfigName("tilde.toml") + + // to be used when setting values from cli + v.RegisterAlias("tilde_dir", "dir") + + v.SetDefault("auto_update", true) + v.SetDefault("log_level", "warn") + v.SetDefault("tilde_dir", where.Dir()) + v.SetDefault("colors", "auto") +} diff --git a/config/read.go b/config/read.go new file mode 100644 index 0000000..8a73b0d --- /dev/null +++ b/config/read.go @@ -0,0 +1,17 @@ +package config + +import ( + "github.com/charmbracelet/log" + "github.com/spf13/viper" +) + +// Read config file and handle error cases +func Read() { + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + log.Warn("Missing config file, switching to defaults. Consider using the `--seed-config` flag to generate the default config file") + } else { + log.Fatal("Error when reading config file", "err", err) + } + } +}