Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading