-
-
Notifications
You must be signed in to change notification settings - Fork 71
feat(storage): default database and cache to OS per-user directories #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Package platformdir resolves the OS-conventional per-user directories for | ||
| // GoModel's durable data and caches, used when no explicit path is | ||
| // configured. Binary installs (install.sh / Homebrew / install.ps1) run from | ||
| // arbitrary working directories, so CWD-relative defaults would scatter | ||
| // state; these follow each platform's convention instead. | ||
| package platformdir | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| ) | ||
|
|
||
| const app = "gomodel" | ||
|
|
||
| // DataDir returns the directory for durable application data such as the | ||
| // SQLite database: | ||
| // | ||
| // Linux $XDG_DATA_HOME/gomodel (default ~/.local/share/gomodel) | ||
| // macOS ~/Library/Application Support/gomodel | ||
| // Windows %LocalAppData%\gomodel | ||
| func DataDir() (string, error) { | ||
| switch runtime.GOOS { | ||
| case "windows": | ||
| base, err := os.UserCacheDir() // %LocalAppData% | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(base, app), nil | ||
| case "darwin": | ||
| base, err := os.UserConfigDir() // ~/Library/Application Support | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(base, app), nil | ||
| default: | ||
| if xdg := os.Getenv("XDG_DATA_HOME"); xdg != "" { | ||
| return filepath.Join(xdg, app), nil | ||
| } | ||
| home, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(home, ".local", "share", app), nil | ||
| } | ||
| } | ||
|
|
||
| // CacheDir returns the directory for re-creatable caches such as the model | ||
| // catalog: | ||
| // | ||
| // Linux $XDG_CACHE_HOME/gomodel (default ~/.cache/gomodel) | ||
| // macOS ~/Library/Caches/gomodel | ||
| // Windows %LocalAppData%\gomodel\cache | ||
| func CacheDir() (string, error) { | ||
| base, err := os.UserCacheDir() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if runtime.GOOS == "windows" { | ||
| // UserCacheDir is %LocalAppData%, shared with DataDir: keep the | ||
| // cache in a subdirectory so the two stay distinguishable. | ||
| return filepath.Join(base, app, "cache"), nil | ||
| } | ||
| return filepath.Join(base, app), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package platformdir | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestDataDir(t *testing.T) { | ||
| dir, err := DataDir() | ||
| if err != nil { | ||
| t.Fatalf("DataDir() error: %v", err) | ||
| } | ||
| if !filepath.IsAbs(dir) { | ||
| t.Errorf("DataDir() = %q, want an absolute path", dir) | ||
| } | ||
| if filepath.Base(dir) != app { | ||
| t.Errorf("DataDir() = %q, want a %q leaf directory", dir, app) | ||
| } | ||
| } | ||
|
|
||
| func TestDataDirHonorsXDGDataHome(t *testing.T) { | ||
| if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { | ||
| t.Skip("XDG_DATA_HOME only applies to the default (Linux/Unix) branch") | ||
| } | ||
| t.Setenv("XDG_DATA_HOME", "/custom/data") | ||
|
|
||
| dir, err := DataDir() | ||
| if err != nil { | ||
| t.Fatalf("DataDir() error: %v", err) | ||
| } | ||
| if want := filepath.Join("/custom/data", app); dir != want { | ||
| t.Errorf("DataDir() = %q, want %q", dir, want) | ||
| } | ||
| } | ||
|
|
||
| func TestCacheDir(t *testing.T) { | ||
| dir, err := CacheDir() | ||
| if err != nil { | ||
| t.Fatalf("CacheDir() error: %v", err) | ||
| } | ||
| if !filepath.IsAbs(dir) { | ||
| t.Errorf("CacheDir() = %q, want an absolute path", dir) | ||
| } | ||
| want := app | ||
| if runtime.GOOS == "windows" { | ||
| want = "cache" | ||
| } | ||
| if filepath.Base(dir) != want { | ||
| t.Errorf("CacheDir() = %q, want a %q leaf directory", dir, want) | ||
| } | ||
| } | ||
|
|
||
| func TestDataAndCacheDirsDiffer(t *testing.T) { | ||
| dataDir, err := DataDir() | ||
| if err != nil { | ||
| t.Fatalf("DataDir() error: %v", err) | ||
| } | ||
| cacheDir, err := CacheDir() | ||
| if err != nil { | ||
| t.Fatalf("CacheDir() error: %v", err) | ||
| } | ||
| if dataDir == cacheDir { | ||
| t.Errorf("DataDir() and CacheDir() are both %q; they must differ", dataDir) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/enterpilot/gomodel/internal/platformdir" | ||
| ) | ||
|
|
||
| func TestDefaultSQLitePath(t *testing.T) { | ||
| platformDataDir, err := platformdir.DataDir() | ||
| if err != nil { | ||
| t.Fatalf("platformdir.DataDir() error: %v", err) | ||
| } | ||
| platformPath := filepath.Join(platformDataDir, "gomodel.db") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| setup func(t *testing.T, dir string) | ||
| want string | ||
| }{ | ||
| { | ||
| name: "data directory exists keeps legacy path", | ||
| setup: func(t *testing.T, dir string) { | ||
| if err := os.Mkdir(filepath.Join(dir, "data"), 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| }, | ||
| want: LegacySQLitePath, | ||
| }, | ||
| { | ||
| name: "regular file named data falls through to platform path", | ||
| setup: func(t *testing.T, dir string) { | ||
| if err := os.WriteFile(filepath.Join(dir, "data"), []byte("not a directory"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| }, | ||
| want: platformPath, | ||
| }, | ||
| { | ||
| name: "no data entry uses platform path", | ||
| setup: func(t *testing.T, dir string) {}, | ||
| want: platformPath, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| tt.setup(t, dir) | ||
| t.Chdir(dir) | ||
|
|
||
| if got := DefaultSQLitePath(); got != tt.want { | ||
| t.Errorf("DefaultSQLitePath() = %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.env.templatenow tells operators to setGOMODEL_SQLITE_PATH, but this tag still only readsSQLITE_PATH. With no YAML path,Load()ignores the documented variable and keeps the default path, so an explicit database location no longer wins as described by the PR and users can start against the wrong SQLite file.Context Used: CLAUDE.md (source)
Artifacts
Repro: focused Go test exercising config.Load SQLite env override behavior
Repro: verbose go test output showing GOMODEL_SQLITE_PATH ignored and SQLITE_PATH honored