/
fs.go
153 lines (126 loc) · 2.8 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package bassgh
import (
"bytes"
"context"
"fmt"
"io"
"io/fs"
"path"
"sync"
"time"
"github.com/google/go-github/v43/github"
"github.com/vito/bass/pkg/zapctx"
"go.uber.org/zap"
)
// FS is a filesystem that reads from a GitHub repo at a given commit.
type FS struct {
Ctx context.Context
Client *github.Client
Repo *github.Repository
Ref string
cache map[string]*github.RepositoryContent
cacheL *sync.Mutex
}
func NewFS(ctx context.Context, client *github.Client, repo *github.Repository, ref string) fs.FS {
return &FS{
Ctx: ctx,
Client: client,
Repo: repo,
Ref: ref,
cache: map[string]*github.RepositoryContent{},
cacheL: new(sync.Mutex),
}
}
func (ghfs *FS) Open(name string) (fs.File, error) {
logger := zapctx.FromContext(ghfs.Ctx).With(
zap.String("repo", ghfs.Repo.GetFullName()),
zap.String("path", name),
zap.String("sha", ghfs.Ref),
)
ghfs.cacheL.Lock()
defer ghfs.cacheL.Unlock()
file, cached := ghfs.cache[name]
if cached {
logger.Debug("cache hit")
} else {
logger.Info("fetching content")
var err error
file, _, _, err = ghfs.Client.Repositories.GetContents(
ghfs.Ctx,
ghfs.Repo.GetOwner().GetLogin(),
ghfs.Repo.GetName(),
name,
&github.RepositoryContentGetOptions{
Ref: ghfs.Ref,
},
)
if err != nil {
return nil, fmt.Errorf("get %s: %w", name, err)
}
ghfs.cache[name] = file
}
content, err := file.GetContent()
if err != nil {
return nil, fmt.Errorf("get content %s: %w", name, err)
}
return &ghFile{bytes.NewBufferString(content), logger, ghfs, name}, nil
}
type ghFile struct {
io.Reader
logger *zap.Logger
fs *FS
name string
}
func (f *ghFile) Close() error {
return nil
}
func (f *ghFile) Stat() (fs.FileInfo, error) {
// this would require loading the content into memory via GetContents; not
// worth it.
parent := path.Dir(f.name)
filename := path.Base(f.name)
_, dirContents, _, err := f.fs.Client.Repositories.GetContents(
f.fs.Ctx,
f.fs.Repo.GetOwner().GetLogin(),
f.fs.Repo.GetName(),
parent,
&github.RepositoryContentGetOptions{
Ref: f.fs.Ref,
},
)
if err != nil {
return nil, fmt.Errorf("get %s: %w", parent, err)
}
for _, contents := range dirContents {
if *contents.Name == filename {
return &ghInfo{contents}, nil
}
}
return nil, fmt.Errorf("file %s not found", f.name)
}
type ghInfo struct {
*github.RepositoryContent
}
func (i ghInfo) Name() string {
return i.RepositoryContent.GetName()
}
func (i ghInfo) IsDir() bool {
return i.GetType() == "dir"
}
func (i ghInfo) ModTime() time.Time {
return time.Time{}
}
func (i ghInfo) Mode() fs.FileMode {
// made up
if i.IsDir() {
return fs.FileMode(0755)
} else {
return fs.FileMode(0644)
}
}
func (i ghInfo) Size() int64 {
return int64(i.GetSize())
}
func (i ghInfo) Sys() interface{} {
return nil
}