on main help sign in
feat: add ability to set envs to override config directories (#1661)
a326efeAndrey Nering
README.md+5
@@ -232,6 +232,11 @@ $HOME/.local/share/crush/crush.json
232%LOCALAPPDATA%\crush\crush.json
232%LOCALAPPDATA%\crush\crush.json
233```
233```
234
234
235+> [!TIP]
236+> You can override the user and data config locations by setting:
237+> * `CRUSH_GLOBAL_CONFIG`
238+> * `CRUSH_GLOBAL_DATA`
239+
235### LSPs
240### LSPs
236
241
237Crush can use LSPs for additional context to help inform its decisions, just
242Crush can use LSPs for additional context to help inform its decisions, just
internal/config/load.go+13−9
@@ -1,6 +1,7 @@
1package config
1package config
2
2
3import (
3import (
4+ "cmp"
4 "context"
5 "context"
5 "encoding/json"
6 "encoding/json"
6 "fmt"
7 "fmt"
@@ -695,19 +696,22 @@ func hasAWSCredentials(env env.Env) bool {
695
696
696// GlobalConfig returns the global configuration file path for the application.
697// GlobalConfig returns the global configuration file path for the application.
697func GlobalConfig() string {
698func GlobalConfig() string {
699+ if crushGlobal := os.Getenv("CRUSH_GLOBAL_CONFIG"); crushGlobal != "" {
700+ return filepath.Join(crushGlobal, fmt.Sprintf("%s.json", appName))
701+ }
698− xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
702+ if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
699− if xdgConfigHome != "" {
700 return filepath.Join(xdgConfigHome, appName, fmt.Sprintf("%s.json", appName))
703 return filepath.Join(xdgConfigHome, appName, fmt.Sprintf("%s.json", appName))
701 }
704 }
702−
703 return filepath.Join(home.Dir(), ".config", appName, fmt.Sprintf("%s.json", appName))
705 return filepath.Join(home.Dir(), ".config", appName, fmt.Sprintf("%s.json", appName))
704}
706}
705
707
706// GlobalConfigData returns the path to the main data directory for the application.
708// GlobalConfigData returns the path to the main data directory for the application.
707// this config is used when the app overrides configurations instead of updating the global config.
709// this config is used when the app overrides configurations instead of updating the global config.
708func GlobalConfigData() string {
710func GlobalConfigData() string {
711+ if crushData := os.Getenv("CRUSH_GLOBAL_DATA"); crushData != "" {
712+ return filepath.Join(crushData, fmt.Sprintf("%s.json", appName))
713+ }
709− xdgDataHome := os.Getenv("XDG_DATA_HOME")
714+ if xdgDataHome := os.Getenv("XDG_DATA_HOME"); xdgDataHome != "" {
710− if xdgDataHome != "" {
711 return filepath.Join(xdgDataHome, appName, fmt.Sprintf("%s.json", appName))
715 return filepath.Join(xdgDataHome, appName, fmt.Sprintf("%s.json", appName))
712 }
716 }
713
717
@@ -715,10 +719,10 @@ func GlobalConfigData() string {
715 // for windows, it should be in `%LOCALAPPDATA%/crush/`
719 // for windows, it should be in `%LOCALAPPDATA%/crush/`
716 // for linux and macOS, it should be in `$HOME/.local/share/crush/`
720 // for linux and macOS, it should be in `$HOME/.local/share/crush/`
717 if runtime.GOOS == "windows" {
721 if runtime.GOOS == "windows" {
722+ localAppData := cmp.Or(
718− localAppData := os.Getenv("LOCALAPPDATA")
723+ os.Getenv("LOCALAPPDATA"),
719− if localAppData == "" {
720− localAppData = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Local")
724+ filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Local"),
721− }
725+ )
722 return filepath.Join(localAppData, appName, fmt.Sprintf("%s.json", appName))
726 return filepath.Join(localAppData, appName, fmt.Sprintf("%s.json", appName))
723 }
727 }
724
728