Skip to content

Commit

Permalink
fix: Create config file if it does not exist (#379)
Browse files Browse the repository at this point in the history
Create config file if it does not exist
  • Loading branch information
dbolson authored Jul 15, 2024
1 parent 83cf380 commit 3e672ab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,11 @@ func setFlagsFromConfig() error {
return err
}

_ = viper.ReadInConfig()
err = viper.ReadInConfig()
if err != nil {
// attempt to write config file if it does not exist
_ = viper.SafeWriteConfigAs(configFile)
}

return nil
}
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ type Config struct {
func New(filename string, readFile ReadFile) (Config, error) {
data, err := readFile(filename)
if err != nil {
return Config{}, err
return Config{}, errors.NewError(
fmt.Sprintf("unable to open config file. The file or directory may not exist: %s", filename),
)
}

var c Config
Expand Down

0 comments on commit 3e672ab

Please sign in to comment.