refactor agent data directory resolution (#991)

This commit is contained in:
henrygd
2025-07-25 13:37:23 -04:00
parent 5f44965c2c
commit 1e675cabb5
2 changed files with 18 additions and 23 deletions

View File

@@ -9,35 +9,30 @@ import (
) )
// getDataDir returns the path to the data directory for the agent and an error // getDataDir returns the path to the data directory for the agent and an error
// if the directory is not valid. Pass an empty string to attempt to find the // if the directory is not valid. Attempts to find the optimal data directory if
// optimal data directory. // no data directories are provided.
func getDataDir(dataDir string) (string, error) { func getDataDir(dataDirs ...string) (string, error) {
if dataDir == "" { if len(dataDirs) > 0 {
dataDir, _ = GetEnv("DATA_DIR") return testDataDirs(dataDirs)
} }
dataDir, _ := GetEnv("DATA_DIR")
if dataDir != "" { if dataDir != "" {
return testDataDirs([]string{dataDir}) dataDirs = append(dataDirs, dataDir)
} }
var dirsToTry []string
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
dirsToTry = []string{ dataDirs = append(dataDirs,
filepath.Join(os.Getenv("APPDATA"), "beszel-agent"), filepath.Join(os.Getenv("APPDATA"), "beszel-agent"),
filepath.Join(os.Getenv("LOCALAPPDATA"), "beszel-agent"), filepath.Join(os.Getenv("LOCALAPPDATA"), "beszel-agent"),
} )
} else { } else {
homeDir, err := os.UserHomeDir() dataDirs = append(dataDirs, "/var/lib/beszel-agent")
if err != nil { if homeDir, err := os.UserHomeDir(); err == nil {
return "", err dataDirs = append(dataDirs, filepath.Join(homeDir, ".config", "beszel"))
}
dirsToTry = []string{
"/var/lib/beszel-agent",
filepath.Join(homeDir, ".config", "beszel"),
} }
} }
return testDataDirs(dirsToTry) return testDataDirs(dataDirs)
} }
func testDataDirs(paths []string) (string, error) { func testDataDirs(paths []string) (string, error) {

View File

@@ -44,15 +44,15 @@ func TestGetDataDir(t *testing.T) {
oldValue := os.Getenv("DATA_DIR") oldValue := os.Getenv("DATA_DIR")
defer func() { defer func() {
if oldValue == "" { if oldValue == "" {
os.Unsetenv("DATA_DIR") os.Unsetenv("BESZEL_AGENT_DATA_DIR")
} else { } else {
os.Setenv("DATA_DIR", oldValue) os.Setenv("BESZEL_AGENT_DATA_DIR", oldValue)
} }
}() }()
os.Setenv("DATA_DIR", tempDir) os.Setenv("BESZEL_AGENT_DATA_DIR", tempDir)
result, err := getDataDir("") result, err := getDataDir()
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, tempDir, result) assert.Equal(t, tempDir, result)
}) })
@@ -79,7 +79,7 @@ func TestGetDataDir(t *testing.T) {
// This will try platform-specific defaults, which may or may not work // This will try platform-specific defaults, which may or may not work
// We're mainly testing that it doesn't panic and returns some result // We're mainly testing that it doesn't panic and returns some result
result, err := getDataDir("") result, err := getDataDir()
// We don't assert success/failure here since it depends on system permissions // We don't assert success/failure here since it depends on system permissions
// Just verify we get a string result if no error // Just verify we get a string result if no error
if err == nil { if err == nil {