on main help sign in
add finders for windows/unix/local
bb8c853Tai Groot
cspell.json+39−1
@@ -1 +1,39 @@
1−{"flagWords":[],"words":["crush","charmbracelet","lipgloss","bubbletea","textinput","Focusable","lsps","Sourcegraph","filepicker","imageorient","rasterx","oksvg","termenv","trashhalo","lucasb","nfnt","srwiley","Lanczos","fsext","GROQ","alecthomas","Preproc","Emph","charmtone","Charple","Guac","diffview","Strikethrough","Unticked","uniseg","rivo"],"version":"0.2","language":"en"}
1+{
2+ "flagWords": [],
3+ "words": [
4+ "afero",
5+ "alecthomas",
6+ "bubbletea",
7+ "charmbracelet",
8+ "charmtone",
9+ "Charple",
10+ "crush",
11+ "diffview",
12+ "Emph",
13+ "filepicker",
14+ "Focusable",
15+ "fsext",
16+ "GROQ",
17+ "Guac",
18+ "imageorient",
19+ "Lanczos",
20+ "lipgloss",
21+ "lsps",
22+ "lucasb",
23+ "nfnt",
24+ "oksvg",
25+ "Preproc",
26+ "rasterx",
27+ "rivo",
28+ "Sourcegraph",
29+ "srwiley",
30+ "Strikethrough",
31+ "termenv",
32+ "textinput",
33+ "trashhalo",
34+ "uniseg",
35+ "Unticked"
36+ ],
37+ "version": "0.2",
38+ "language": "en"
39+}
internal/config/config.go+53−11
@@ -11,6 +11,7 @@ import (
11
11
12 "github.com/charmbracelet/crush/internal/llm/models"
12 "github.com/charmbracelet/crush/internal/llm/models"
13 "github.com/charmbracelet/crush/internal/logging"
13 "github.com/charmbracelet/crush/internal/logging"
14+ "github.com/spf13/afero"
14 "github.com/spf13/viper"
15 "github.com/spf13/viper"
15)
16)
16
17
@@ -199,21 +200,62 @@ func Load(workingDir string, debug bool) (*Config, error) {
199 return cfg, nil
200 return cfg, nil
200}
201}
201
202
203+type configFinder struct {
204+ appName string
205+ dotPrefix bool
206+ paths []string
207+}
208+
209+func (f configFinder) Find(fsys afero.Fs) ([]string, error) {
210+ var configFiles []string
211+ configName := fmt.Sprintf("%s.json", f.appName)
212+ if f.dotPrefix {
213+ configName = fmt.Sprintf(".%s.json", f.appName)
214+ }
215+ paths := []string{}
216+ for _, p := range f.paths {
217+ if p == "" {
218+ continue
219+ }
220+ paths = append(paths, os.ExpandEnv(p))
221+ }
222+
223+ for _, path := range paths {
224+ if path == "" {
225+ continue
226+ }
227+
228+ configPath := filepath.Join(path, configName)
229+ if exists, err := afero.Exists(fsys, configPath); err == nil && exists {
230+ configFiles = append(configFiles, configPath)
231+ }
232+ }
233+ return configFiles, nil
234+}
235+
202// configureViper sets up viper's configuration paths and environment variables.
236// configureViper sets up viper's configuration paths and environment variables.
203func configureViper() {
237func configureViper() {
204− viper.SetConfigName(fmt.Sprintf(".%s", appName))
205 viper.SetConfigType("json")
238 viper.SetConfigType("json")
206
239
207− // Unix-style paths
240+ // Create the three finders
208− viper.AddConfigPath("$HOME")
241+ windowsFinder := configFinder{appName: appName, dotPrefix: false, paths: []string{
242+ "$USERPROFILE",
243+ fmt.Sprintf("$APPDATA/%s", appName),
244+ fmt.Sprintf("$LOCALAPPDATA/%s", appName),
245+ }}
246+
247+ unixFinder := configFinder{appName: appName, dotPrefix: false, paths: []string{
248+ "$HOME",
209− viper.AddConfigPath(fmt.Sprintf("$XDG_CONFIG_HOME/%s", appName))
249+ fmt.Sprintf("$XDG_CONFIG_HOME/%s", appName),
210− viper.AddConfigPath(fmt.Sprintf("$HOME/.config/%s", appName))
250+ fmt.Sprintf("$HOME/.config/%s", appName),
211−
251+ }}
212− // Windows-style paths
252+
213− viper.AddConfigPath(fmt.Sprintf("$USERPROFILE"))
253+ localFinder := configFinder{appName: appName, dotPrefix: true, paths: []string{
214− viper.AddConfigPath(fmt.Sprintf("$APPDATA/%s", appName))
254+ ".",
215− viper.AddConfigPath(fmt.Sprintf("$LOCALAPPDATA/%s", appName))
255+ }}
216−
256+
257+ // Use all finders with viper
258+ viper.SetOptions(viper.WithFinder(viper.Finders(windowsFinder, unixFinder, localFinder)))
217 viper.SetEnvPrefix(strings.ToUpper(appName))
259 viper.SetEnvPrefix(strings.ToUpper(appName))
218 viper.AutomaticEnv()
260 viper.AutomaticEnv()
219}
261}