fix(store): expand a leading ~ in EMCLI_DB
A literal "~/..." in EMCLI_DB has no shell to expand it, so SQLite opened it relative to the cwd and silently created a stray "~" directory tree. Expand a leading "~" or "~/" to the user's home dir; "~user", mid-path tildes, and absolute/relative paths are left untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+16
-1
@@ -8,6 +8,7 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
@@ -77,10 +78,24 @@ func (s *Store) migrate() error {
|
||||
|
||||
func (s *Store) Close() error { return s.db.Close() }
|
||||
|
||||
// expandUserHome replaces a leading "~" or "~/" in p with the user's home
|
||||
// directory. Only a leading tilde is expanded (the usual shell convention) —
|
||||
// "~user" and a tilde elsewhere in the path are left untouched. This guards
|
||||
// against an EMCLI_DB set to a literal "~/..." (no shell to expand it), which
|
||||
// would otherwise be opened relative to the cwd and create a stray "~" dir.
|
||||
func expandUserHome(p string) string {
|
||||
if p == "~" || strings.HasPrefix(p, "~/") {
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
return filepath.Join(home, strings.TrimPrefix(p[1:], "/"))
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// DefaultDBPath resolves EMCLI_DB or the per-OS default location.
|
||||
func DefaultDBPath() (string, error) {
|
||||
if p := os.Getenv("EMCLI_DB"); p != "" {
|
||||
return p, nil
|
||||
return expandUserHome(p), nil
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
if dir := os.Getenv("AppData"); dir != "" {
|
||||
|
||||
Reference in New Issue
Block a user