|
| 1 | +// Package paths centralizes OS-native cache and config directory resolution |
| 2 | +// for gtnh-daily-updater, with lazy migration from previously used |
| 3 | +// XDG-hardcoded locations on macOS and Windows. |
| 4 | +package paths |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/caedis/gtnh-daily-updater/internal/logging" |
| 12 | +) |
| 13 | + |
| 14 | +const appName = "gtnh-daily-updater" |
| 15 | + |
| 16 | +// CacheDir returns <UserCacheDir>/gtnh-daily-updater, migrating from the |
| 17 | +// legacy XDG_CACHE_HOME / ~/.cache location if present. |
| 18 | +func CacheDir() (string, error) { |
| 19 | + base, err := os.UserCacheDir() |
| 20 | + if err != nil { |
| 21 | + return "", err |
| 22 | + } |
| 23 | + dir := filepath.Join(base, appName) |
| 24 | + migrate(legacyCacheDir(), dir) |
| 25 | + return dir, nil |
| 26 | +} |
| 27 | + |
| 28 | +// ModsCacheDir returns the mod jar cache directory. |
| 29 | +func ModsCacheDir() (string, error) { |
| 30 | + d, err := CacheDir() |
| 31 | + if err != nil { |
| 32 | + return "", err |
| 33 | + } |
| 34 | + return filepath.Join(d, "mods"), nil |
| 35 | +} |
| 36 | + |
| 37 | +// LogsDir returns the log directory. |
| 38 | +func LogsDir() (string, error) { |
| 39 | + d, err := CacheDir() |
| 40 | + if err != nil { |
| 41 | + return "", err |
| 42 | + } |
| 43 | + return filepath.Join(d, "logs"), nil |
| 44 | +} |
| 45 | + |
| 46 | +// ConfigDir returns <UserConfigDir>/gtnh-daily-updater, migrating from the |
| 47 | +// legacy XDG_CONFIG_HOME / ~/.config location if present. |
| 48 | +func ConfigDir() (string, error) { |
| 49 | + base, err := os.UserConfigDir() |
| 50 | + if err != nil { |
| 51 | + return "", err |
| 52 | + } |
| 53 | + dir := filepath.Join(base, appName) |
| 54 | + migrate(legacyConfigDir(), dir) |
| 55 | + return dir, nil |
| 56 | +} |
| 57 | + |
| 58 | +// ProfilesDir returns the profiles directory. |
| 59 | +func ProfilesDir() (string, error) { |
| 60 | + d, err := ConfigDir() |
| 61 | + if err != nil { |
| 62 | + return "", err |
| 63 | + } |
| 64 | + return filepath.Join(d, "profiles"), nil |
| 65 | +} |
| 66 | + |
| 67 | +func legacyCacheDir() string { |
| 68 | + base := os.Getenv("XDG_CACHE_HOME") |
| 69 | + if base == "" { |
| 70 | + home, err := os.UserHomeDir() |
| 71 | + if err != nil { |
| 72 | + return "" |
| 73 | + } |
| 74 | + base = filepath.Join(home, ".cache") |
| 75 | + } |
| 76 | + return filepath.Join(base, appName) |
| 77 | +} |
| 78 | + |
| 79 | +func legacyConfigDir() string { |
| 80 | + base := os.Getenv("XDG_CONFIG_HOME") |
| 81 | + if base == "" { |
| 82 | + home, err := os.UserHomeDir() |
| 83 | + if err != nil { |
| 84 | + return "" |
| 85 | + } |
| 86 | + base = filepath.Join(home, ".config") |
| 87 | + } |
| 88 | + return filepath.Join(base, appName) |
| 89 | +} |
| 90 | + |
| 91 | +// migrate moves oldDir to newDir if oldDir exists, newDir does not, and the |
| 92 | +// two paths differ. Failures are logged but non-fatal. |
| 93 | +func migrate(oldDir, newDir string) { |
| 94 | + if oldDir == "" || oldDir == newDir { |
| 95 | + return |
| 96 | + } |
| 97 | + if _, err := os.Stat(oldDir); err != nil { |
| 98 | + return |
| 99 | + } |
| 100 | + if _, err := os.Stat(newDir); err == nil { |
| 101 | + return |
| 102 | + } else if !errors.Is(err, os.ErrNotExist) { |
| 103 | + return |
| 104 | + } |
| 105 | + if err := os.MkdirAll(filepath.Dir(newDir), 0o755); err != nil { |
| 106 | + logging.Infof(" Warning: could not create %s: %v\n", filepath.Dir(newDir), err) |
| 107 | + return |
| 108 | + } |
| 109 | + if err := os.Rename(oldDir, newDir); err != nil { |
| 110 | + logging.Infof(" Warning: could not migrate %s -> %s: %v\n", oldDir, newDir, err) |
| 111 | + return |
| 112 | + } |
| 113 | + logging.Infof("Migrated %s -> %s\n", oldDir, newDir) |
| 114 | +} |
0 commit comments