From 6dbf9fad74e60d9080ca183900a363590a860b26 Mon Sep 17 00:00:00 2001 From: xvlet Date: Thu, 30 Jul 2026 12:35:09 +0900 Subject: [PATCH] fix(config): add homebrew etc paths to config lookup fallbacks --- config/config.go | 56 ++++++++++++++++++++++++++++++++++-------------- install.ps1 | 8 +++++++ install.sh | 6 ++++++ 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index f1ab679..d1ed101 100644 --- a/config/config.go +++ b/config/config.go @@ -37,27 +37,51 @@ func LoadConfig(path string) (*Config, error) { // 1. Try reading the specified path (or "config.yml" from current directory) // #nosec G304 -- config path is fixed or loaded from user's CLI argument data, err = os.ReadFile(path) - if err != nil { - // 2. Fallback to home directory config (~/.amqcli.yml) - homeDir, homeErr := os.UserHomeDir() - if homeErr == nil { - homeConfigPath := homeDir + "/.amqcli.yml" - // #nosec G304 -- config path is safe - data, err = os.ReadFile(homeConfigPath) - if err != nil { - // 3. Create default template if it doesn't exist anywhere - fmt.Printf("Config file not found. Creating default template at: %s\n", homeConfigPath) - createDefaultConfig(homeConfigPath) - // #nosec G304 -- config path is safe - data, err = os.ReadFile(homeConfigPath) - } + if err == nil { + return parseConfig(data) + } + + homeDir, homeErr := os.UserHomeDir() + homeConfigPath := "" + if homeErr == nil { + homeConfigPath = homeDir + "/.amqcli.yml" + } + + // 2. Check fallback locations + fallbacks := []string{ + homeConfigPath, + "/opt/homebrew/etc/amqcli/config.yml", + "/usr/local/etc/amqcli/config.yml", + "/home/linuxbrew/.linuxbrew/etc/amqcli/config.yml", + "/etc/amqcli/config.yml", + } + + for _, fb := range fallbacks { + if fb == "" { + continue + } + // #nosec G304 -- fallback paths are hardcoded and safe + data, err = os.ReadFile(fb) + if err == nil { + return parseConfig(data) } } - if err != nil { - return nil, fmt.Errorf("failed to read config file (try creating ~/.amqcli.yml): %w", err) + // 3. Create default template if it doesn't exist anywhere + if homeConfigPath != "" { + fmt.Printf("Config file not found. Creating default template at: %s\n", homeConfigPath) + createDefaultConfig(homeConfigPath) + // #nosec G304 -- config path is safe + data, err = os.ReadFile(homeConfigPath) + if err == nil { + return parseConfig(data) + } } + return nil, fmt.Errorf("failed to read or create config file: %w", err) +} + +func parseConfig(data []byte) (*Config, error) { // Expand environment variables (e.g. ${VAR} or ${VAR:-default}) expandedData := os.Expand(string(data), expandEnvFunc) diff --git a/install.ps1 b/install.ps1 index be200c6..9b1d505 100644 --- a/install.ps1 +++ b/install.ps1 @@ -79,6 +79,14 @@ New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null $DestPath = Join-Path $InstallDir "$BIN.exe" Move-Item -Path $BinPath.FullName -Destination $DestPath -Force +# Install config file if it doesn't exist +$ConfigDest = Join-Path $env:USERPROFILE ".amqcli.yml" +$ConfigSrc = Join-Path $TempDir "config.yml" +if (-not (Test-Path $ConfigDest) -and (Test-Path $ConfigSrc)) { + Copy-Item -Path $ConfigSrc -Destination $ConfigDest + Write-Host " > installed default config to $ConfigDest" -ForegroundColor Green +} + Write-Host " > installed $BIN to $DestPath" -ForegroundColor Green # Cleanup diff --git a/install.sh b/install.sh index 4a20bce..0264cf7 100644 --- a/install.sh +++ b/install.sh @@ -80,6 +80,12 @@ main() { mv "$BIN_PATH" "${INSTALL_DIR}/${BIN}" chmod +x "${INSTALL_DIR}/${BIN}" + # Install config file if it doesn't exist + if [ ! -f "$HOME/.amqcli.yml" ] && [ -f "$TMP/config.yml" ]; then + cp "$TMP/config.yml" "$HOME/.amqcli.yml" + log "installed default config to $HOME/.amqcli.yml" + fi + log "installed ${BIN} to ${INSTALL_DIR}/${BIN}" # check PATH