Skip to content

Commit

Permalink
feat: better handling of config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakiyo committed Aug 23, 2023
1 parent f8851e4 commit 7db00e6
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func init() {
v.SetConfigType("toml")
v.SetConfigName("tilde")
v.AddConfigPath(where.Dir())

v.SetDefault("auto_update", true)
v.SetDefault("log_level", "warn")
Expand Down
7 changes: 2 additions & 5 deletions config/read.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package config

import (
"github.com/Yakiyo/tilde/where"
"github.com/charmbracelet/log"
"github.com/spf13/viper"
)

// Read config file and handle error cases
func Read(file string) {
if file == "" {
viper.SetConfigFile(where.Config())
} else {
if file != "" {
viper.SetConfigFile(file)
}
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")
log.Info("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)
}
Expand Down
2 changes: 1 addition & 1 deletion where/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Index() string {
return filepath.Join(Cache(), "index.json")
}

// path to config.toml file located inside root dir
// path to default config.toml file located inside root dir
func Config() string {
return filepath.Join(Dir(), "tilde.toml")
}
Expand Down

0 comments on commit 7db00e6

Please # to comment.