/
github.go
130 lines (109 loc) · 2.71 KB
/
github.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
package provider
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"github.com/fioncat/grfs/types"
"github.com/google/go-github/v56/github"
"golang.org/x/oauth2"
)
type githubProvider struct {
repo *types.Repository
client *github.Client
}
func newGithub(repo *types.Repository, token string) types.Provider {
var httpCli *http.Client
ctx := context.Background()
if token != "" {
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: token,
})
httpCli = oauth2.NewClient(ctx, ts)
}
client := github.NewClient(httpCli)
return &githubProvider{
repo: repo,
client: client,
}
}
func (p *githubProvider) Check(ctx context.Context) error {
githubRepo, _, err := p.client.Repositories.Get(ctx, p.repo.Owner, p.repo.Name)
if err != nil {
return fmt.Errorf("github get repository: %w", err)
}
if p.repo.Ref == "" {
if githubRepo.DefaultBranch != nil {
p.repo.Ref = *githubRepo.DefaultBranch
}
}
return nil
}
func (p *githubProvider) ReadDir(ctx context.Context, path string) ([]*types.Entry, error) {
fc, dc, _, err := p.client.Repositories.GetContents(ctx, p.repo.Owner, p.repo.Name, path,
&github.RepositoryContentGetOptions{
Ref: p.repo.Ref,
})
if err != nil {
return nil, err
}
if fc != nil {
return nil, fmt.Errorf("%q is a file, not directory", path)
}
ents := make([]*types.Entry, len(dc))
for i, content := range dc {
path := content.GetPath()
name := content.GetName()
if path == "" || name == "" {
return nil, errors.New("github return entry with empty name or path")
}
var (
isDir bool
isSymLink bool
linkName string
size int64
)
switch content.GetType() {
case "dir":
isDir = true
case "file":
size = int64(content.GetSize())
case "symlink":
isSymLink = true
linkName = content.GetTarget()
if linkName == "" {
return nil, fmt.Errorf("entry %q is a symlink, but its target is empty", path)
}
case "":
return nil, fmt.Errorf("entry type is empty for %q", path)
default:
return nil, fmt.Errorf("unknown entry type %q for %q", content.GetType(), path)
}
ents[i] = &types.Entry{
Path: path,
Name: name,
IsDir: isDir,
IsSymLink: isSymLink,
LinkName: linkName,
Size: size,
WebUrl: content.GetHTMLURL(),
}
}
return ents, nil
}
func (p *githubProvider) ReadFile(ctx context.Context, path string) ([]byte, error) {
reader, _, err := p.client.Repositories.DownloadContents(ctx, p.repo.Owner, p.repo.Name, path,
&github.RepositoryContentGetOptions{
Ref: p.repo.Ref,
})
if err != nil {
return nil, err
}
defer reader.Close()
data, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("read content for %q: %w", path, err)
}
return data, nil
}