Skip to content

Commit a5a0538

Browse files
authored
Merge pull request #643 from drpebcak/tilde-expansion-cache-path
fix: expand tilde and relative paths to be absolute for cache-dir
2 parents 7a71c3b + ecca52c commit a5a0538

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pkg/cache/cache.go

+20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"errors"
1010
"io/fs"
1111
"os"
12+
"os/user"
1213
"path/filepath"
14+
"strings"
1315

1416
"github.com/adrg/xdg"
1517
"github.com/getkin/kin-openapi/openapi3"
@@ -40,10 +42,28 @@ func Complete(opts ...Options) (result Options) {
4042
}
4143
if result.CacheDir == "" {
4244
result.CacheDir = filepath.Join(xdg.CacheHome, version.ProgramName)
45+
} else if !filepath.IsAbs(result.CacheDir) {
46+
var err error
47+
result.CacheDir, err = makeAbsolute(result.CacheDir)
48+
if err != nil {
49+
result.CacheDir = filepath.Join(xdg.CacheHome, version.ProgramName)
50+
}
4351
}
4452
return
4553
}
4654

55+
func makeAbsolute(path string) (string, error) {
56+
if strings.HasPrefix(path, "~"+string(filepath.Separator)) {
57+
usr, err := user.Current()
58+
if err != nil {
59+
return "", err
60+
}
61+
62+
return filepath.Join(usr.HomeDir, path[2:]), nil
63+
}
64+
return filepath.Abs(path)
65+
}
66+
4767
type noCacheKey struct{}
4868

4969
func IsNoCache(ctx context.Context) bool {

0 commit comments

Comments
 (0)