Skip to content

Commit

Permalink
Add proper directory handling to LoadUserConfig
Browse files Browse the repository at this point in the history
When the user config path resolves to a directory, return default config values instead of attempting to read it as a file. This makes the config loading more robust by gracefully handling directory paths.
  • Loading branch information
lox committed Feb 17, 2025
1 parent a1db8e0 commit d98bac4
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions app/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (
)

var userConfigSchema = func() string {
schema, err := hcl.Schema(&UserConfig{})
if err != nil {
return ""
}
schema := hcl.MustSchema(&UserConfig{})
data, err := hcl.MarshalAST(schema)
if err != nil {
return ""
panic(err)
}
return string(data)
}()
Expand All @@ -36,10 +33,21 @@ func LoadUserConfig(configPath string) (UserConfig, error) {
// always return a valid config on error, with defaults set.
_ = hcl.Unmarshal([]byte{}, &config)

data, err := os.ReadFile(kong.ExpandPath(configPath))
if os.IsNotExist(err) {
expandedPath := kong.ExpandPath(configPath)
info, err := os.Stat(expandedPath)
if err != nil {
if os.IsNotExist(err) {
return config, nil
}
return config, errors.WithStack(err)
}

if info.IsDir() {
return config, nil
} else if err != nil {
}

data, err := os.ReadFile(expandedPath)
if err != nil {
return config, errors.WithStack(err)
}
err = hcl.Unmarshal(data, &config)
Expand Down Expand Up @@ -71,6 +79,9 @@ func (u *userConfigResolver) Resolve(context *kong.Context, parent *kong.Path, f
case "idea":
return u.config.Idea, nil

case "init-sources":
return u.config.InitSources, nil

default:
return nil, nil
}
Expand Down

0 comments on commit d98bac4

Please sign in to comment.