- Install golangci-lint v2 via binary download instead of go install (avoids Go 1.25 requirement of golangci-lint v2.10+) - Add darwin build tags to tests that depend on macOS keychain: derivation_index_test.go, pgpunlock_test.go, validation (keychain tests) - Move generateRandomString to helpers_darwin.go (only called from darwin-only keychainunlocker.go) - Fix unchecked error returns flagged by errcheck linter - Add gnupg to builder stage for PGP-related tests - Use --ulimit memlock=-1:-1 in CI for memguard large secret tests - Add //nolint:unused for intentionally kept but currently unused test helpers
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package secret
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// DetermineStateDir determines the state directory based on environment variables and OS.
|
|
// It returns an error if no usable directory can be determined.
|
|
func DetermineStateDir(customConfigDir string) (string, error) {
|
|
// Check for environment variable first
|
|
if envStateDir := os.Getenv(EnvStateDir); envStateDir != "" {
|
|
return envStateDir, nil
|
|
}
|
|
|
|
// Use custom config dir if provided
|
|
if customConfigDir != "" {
|
|
return filepath.Join(customConfigDir, AppID), nil
|
|
}
|
|
|
|
// Use os.UserConfigDir() which handles platform-specific directories:
|
|
// - On Unix systems, it returns $XDG_CONFIG_HOME or $HOME/.config
|
|
// - On Darwin, it returns $HOME/Library/Application Support
|
|
// - On Windows, it returns %AppData%
|
|
configDir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
// Fallback to a reasonable default if we can't determine user config dir
|
|
homeDir, homeErr := os.UserHomeDir()
|
|
if homeErr != nil {
|
|
return "", fmt.Errorf("unable to determine state directory: config dir: %w, home dir: %w", err, homeErr)
|
|
}
|
|
|
|
fallbackDir := filepath.Join(homeDir, ".config", AppID)
|
|
Warn("Could not determine user config directory, falling back to default", "fallback", fallbackDir, "error", err)
|
|
|
|
return fallbackDir, nil
|
|
}
|
|
|
|
return filepath.Join(configDir, AppID), nil
|
|
}
|