From ecca52cd7e2a08f87d3465ba878b1839d1f41b7f Mon Sep 17 00:00:00 2001 From: Taylor Price Date: Tue, 16 Jul 2024 16:10:06 -0700 Subject: [PATCH] fix: expand tilde and relative paths to be absolute for cache-dir Signed-off-by: Taylor Price --- pkg/cache/cache.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index bc499aef..031bd166 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -9,7 +9,9 @@ import ( "errors" "io/fs" "os" + "os/user" "path/filepath" + "strings" "github.com/adrg/xdg" "github.com/getkin/kin-openapi/openapi3" @@ -40,10 +42,28 @@ func Complete(opts ...Options) (result Options) { } if result.CacheDir == "" { result.CacheDir = filepath.Join(xdg.CacheHome, version.ProgramName) + } else if !filepath.IsAbs(result.CacheDir) { + var err error + result.CacheDir, err = makeAbsolute(result.CacheDir) + if err != nil { + result.CacheDir = filepath.Join(xdg.CacheHome, version.ProgramName) + } } return } +func makeAbsolute(path string) (string, error) { + if strings.HasPrefix(path, "~"+string(filepath.Separator)) { + usr, err := user.Current() + if err != nil { + return "", err + } + + return filepath.Join(usr.HomeDir, path[2:]), nil + } + return filepath.Abs(path) +} + type noCacheKey struct{} func IsNoCache(ctx context.Context) bool {