From a326efef45a6b223f0341a548cd5c39cabe4c0a4 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 18 Dec 2025 12:56:11 -0300 Subject: [PATCH] feat: add ability to set envs to override config directories (#1661) --- README.md | 5 +++++ internal/config/load.go | 22 +++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e25c99a5cb84372414d68a63a511eba824ac9b76..c268cb7cedf4b80632dbd75458ad3db90900edf0 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,11 @@ $HOME/.local/share/crush/crush.json %LOCALAPPDATA%\crush\crush.json ``` +> [!TIP] +> You can override the user and data config locations by setting: +> * `CRUSH_GLOBAL_CONFIG` +> * `CRUSH_GLOBAL_DATA` + ### LSPs Crush can use LSPs for additional context to help inform its decisions, just diff --git a/internal/config/load.go b/internal/config/load.go index 059f9da2a39833b70b3635f230b292bb49d5edc6..0d16702dcdd35eb7d431ddfe4a0b35ab48e4debc 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -1,6 +1,7 @@ package config import ( + "cmp" "context" "encoding/json" "fmt" @@ -695,19 +696,22 @@ func hasAWSCredentials(env env.Env) bool { // GlobalConfig returns the global configuration file path for the application. func GlobalConfig() string { - xdgConfigHome := os.Getenv("XDG_CONFIG_HOME") - if xdgConfigHome != "" { + if crushGlobal := os.Getenv("CRUSH_GLOBAL_CONFIG"); crushGlobal != "" { + return filepath.Join(crushGlobal, fmt.Sprintf("%s.json", appName)) + } + if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" { return filepath.Join(xdgConfigHome, appName, fmt.Sprintf("%s.json", appName)) } - return filepath.Join(home.Dir(), ".config", appName, fmt.Sprintf("%s.json", appName)) } // GlobalConfigData returns the path to the main data directory for the application. // this config is used when the app overrides configurations instead of updating the global config. func GlobalConfigData() string { - xdgDataHome := os.Getenv("XDG_DATA_HOME") - if xdgDataHome != "" { + if crushData := os.Getenv("CRUSH_GLOBAL_DATA"); crushData != "" { + return filepath.Join(crushData, fmt.Sprintf("%s.json", appName)) + } + if xdgDataHome := os.Getenv("XDG_DATA_HOME"); xdgDataHome != "" { return filepath.Join(xdgDataHome, appName, fmt.Sprintf("%s.json", appName)) } @@ -715,10 +719,10 @@ func GlobalConfigData() string { // for windows, it should be in `%LOCALAPPDATA%/crush/` // for linux and macOS, it should be in `$HOME/.local/share/crush/` if runtime.GOOS == "windows" { - localAppData := os.Getenv("LOCALAPPDATA") - if localAppData == "" { - localAppData = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Local") - } + localAppData := cmp.Or( + os.Getenv("LOCALAPPDATA"), + filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Local"), + ) return filepath.Join(localAppData, appName, fmt.Sprintf("%s.json", appName)) }