Skip to content

Commit e0db6a2

Browse files
authored
[filecache] add CacheDir Option, and copy to pkg/ directory (#175)
## Summary I added an Option to make CacheDir configurable in `filecache`. This would enable Devbox to continue using the xdg dirs. I also copied the `filecache` to `pkgs/` directory. Once that is published, I'll send another PR so `envsec` can reference that and delete the `envsec/internal/filecache`. ## How was it tested? `devbox run -c envsec/ build`
1 parent d8cb2a6 commit e0db6a2

File tree

4 files changed

+133
-11
lines changed

4 files changed

+133
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.idea

envsec/internal/filecache/filecache.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,39 @@ var NotFound = errors.New("not found")
1414
var Expired = errors.New("expired")
1515

1616
type cache struct {
17-
domain string
18-
}
19-
20-
func New(domain string) *cache {
21-
return &cache{domain: domain}
17+
domain string
18+
cacheDir string
2219
}
2320

2421
type data struct {
2522
Val []byte
2623
Exp time.Time
2724
}
2825

26+
type Option func(*cache)
27+
28+
func New(domain string, opts ...Option) *cache {
29+
result := &cache{domain: domain}
30+
31+
var err error
32+
result.cacheDir, err = os.UserCacheDir()
33+
if err != nil {
34+
result.cacheDir = "~/.cache"
35+
}
36+
37+
for _, opt := range opts {
38+
opt(result)
39+
}
40+
41+
return result
42+
}
43+
44+
func WithCacheDir(dir string) Option {
45+
return func(c *cache) {
46+
c.cacheDir = dir
47+
}
48+
}
49+
2950
func (c *cache) Set(key string, val []byte, dur time.Duration) error {
3051
d, err := json.Marshal(data{Val: val, Exp: time.Now().Add(dur)})
3152
if err != nil {
@@ -65,11 +86,7 @@ func (c *cache) Get(key string) ([]byte, error) {
6586
}
6687

6788
func (c *cache) filename(key string) string {
68-
cacheDir, err := os.UserCacheDir()
69-
if err != nil {
70-
cacheDir = "~/.cache"
71-
}
72-
dir := filepath.Join(cacheDir, c.domain)
89+
dir := filepath.Join(c.cacheDir, c.domain)
7390
_ = os.MkdirAll(dir, 0755)
7491
return filepath.Join(dir, key)
7592
}

go.work.sum

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
cloud.google.com/go/compute v1.20.1 h1:6aKEtlUiwEpJzM001l0yFkpXmUVXaN8W+fbkb2AZNbg=
12
cloud.google.com/go/compute v1.20.1/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM=
23
cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k=
4+
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
35
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
46
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
57
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
68
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
79
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
810
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
11+
github.com/bwesterb/go-ristretto v1.2.3 h1:1w53tCkGhCQ5djbat3+MH0BAQ5Kfgbt56UZQ/JMzngw=
12+
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
913
github.com/cloudflare/ahocorasick v0.0.0-20210425175752-730270c3e184/go.mod h1:tGWUZLZp9ajsxUOnHmFFLnqnlKXsCn6GReG4jAD59H0=
1014
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
1115
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
1216
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
1317
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
18+
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
1419
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7 h1:tYwu/z8Y0NkkzGEh3z21mSWggMg4LwLRFucLS7TjARg=
1520
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
1621
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
@@ -44,6 +49,7 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
4449
github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g=
4550
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
4651
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
52+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
4753
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
4854
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
4955
github.com/valyala/fasthttp v1.40.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
@@ -54,13 +60,19 @@ golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
5460
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
5561
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
5662
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
63+
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
5764
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
5865
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
5966
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
67+
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
68+
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
6069
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
70+
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
6171
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
6272
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6373
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
74+
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
6475
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
6576
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
66-
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
77+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
78+
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=

pkg/filecache/filecache.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// filecache is a simple local file-based cache
2+
package filecache
3+
4+
import (
5+
"encoding/json"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
10+
"github.com/pkg/errors"
11+
)
12+
13+
var NotFound = errors.New("not found")
14+
var Expired = errors.New("expired")
15+
16+
type cache struct {
17+
domain string
18+
cacheDir string
19+
}
20+
21+
type data struct {
22+
Val []byte
23+
Exp time.Time
24+
}
25+
26+
type Option func(*cache)
27+
28+
func New(domain string, opts ...Option) *cache {
29+
result := &cache{domain: domain}
30+
31+
var err error
32+
result.cacheDir, err = os.UserCacheDir()
33+
if err != nil {
34+
result.cacheDir = "~/.cache"
35+
}
36+
37+
for _, opt := range opts {
38+
opt(result)
39+
}
40+
41+
return result
42+
}
43+
44+
func WithCacheDir(dir string) Option {
45+
return func(c *cache) {
46+
c.cacheDir = dir
47+
}
48+
}
49+
50+
func (c *cache) Set(key string, val []byte, dur time.Duration) error {
51+
d, err := json.Marshal(data{Val: val, Exp: time.Now().Add(dur)})
52+
if err != nil {
53+
return errors.WithStack(err)
54+
}
55+
56+
return errors.WithStack(os.WriteFile(c.filename(key), d, 0644))
57+
}
58+
59+
func (c *cache) SetT(key string, val []byte, t time.Time) error {
60+
d, err := json.Marshal(data{Val: val, Exp: t})
61+
if err != nil {
62+
return errors.WithStack(err)
63+
}
64+
65+
return errors.WithStack(os.WriteFile(c.filename(key), d, 0644))
66+
}
67+
68+
func (c *cache) Get(key string) ([]byte, error) {
69+
path := c.filename(key)
70+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
71+
return nil, NotFound
72+
}
73+
74+
content, err := os.ReadFile(path)
75+
if err != nil {
76+
return nil, errors.WithStack(err)
77+
}
78+
d := data{}
79+
if err := json.Unmarshal(content, &d); err != nil {
80+
return nil, errors.WithStack(err)
81+
}
82+
if time.Now().After(d.Exp) {
83+
return nil, Expired
84+
}
85+
return d.Val, nil
86+
}
87+
88+
func (c *cache) filename(key string) string {
89+
dir := filepath.Join(c.cacheDir, c.domain)
90+
_ = os.MkdirAll(dir, 0755)
91+
return filepath.Join(dir, key)
92+
}

0 commit comments

Comments
 (0)