5
5
"encoding/base64"
6
6
"errors"
7
7
"fmt"
8
+ "github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
8
9
"github.com/google/go-github/v32/github"
9
10
"golang.org/x/oauth2"
10
11
"net/http"
@@ -21,6 +22,12 @@ type GitHub struct {
21
22
reference string
22
23
}
23
24
25
+ type IsDir bool
26
+
27
+ func (isDir IsDir ) IsDir () bool {
28
+ return bool (isDir )
29
+ }
30
+
24
31
func New (owner , repo , reference , token string ) * GitHub {
25
32
return & GitHub {
26
33
token : token ,
@@ -30,18 +37,23 @@ func New(owner, repo, reference, token string) *GitHub {
30
37
}
31
38
}
32
39
33
- func (gh * GitHub ) Get (ctx context.Context , path string ) ([]byte , error ) {
34
- fileContent , _ , resp , err := gh .client (ctx ).Repositories .GetContents (ctx , gh .owner , gh .repo , path ,
35
- & github.RepositoryContentGetOptions {
36
- Ref : gh .reference ,
37
- },
38
- )
40
+ func (gh * GitHub ) Stat (ctx context.Context , path string ) (fs.FileInfo , error ) {
41
+ _ , directoryContent , err := gh .getContentsWrapper (ctx , path )
39
42
if err != nil {
40
- if resp != nil && resp .StatusCode == http .StatusNotFound {
41
- return nil , os .ErrNotExist
42
- }
43
+ return nil , err
44
+ }
43
45
44
- return nil , fmt .Errorf ("%w: %v" , ErrAPI , err )
46
+ if directoryContent != nil {
47
+ return IsDir (true ), nil
48
+ }
49
+
50
+ return IsDir (false ), nil
51
+ }
52
+
53
+ func (gh * GitHub ) Get (ctx context.Context , path string ) ([]byte , error ) {
54
+ fileContent , _ , err := gh .getContentsWrapper (ctx , path )
55
+ if err != nil {
56
+ return nil , err
45
57
}
46
58
47
59
// Simulate os.Read() behavior in case the supplied path points to a directory
@@ -57,6 +69,25 @@ func (gh *GitHub) Get(ctx context.Context, path string) ([]byte, error) {
57
69
return fileBytes , nil
58
70
}
59
71
72
+ func (gh * GitHub ) ReadDir (ctx context.Context , path string ) ([]string , error ) {
73
+ _ , directoryContent , err := gh .getContentsWrapper (ctx , path )
74
+ if err != nil {
75
+ return nil , err
76
+ }
77
+
78
+ // Simulate ioutil.ReadDir() behavior in case the supplied path points to a file
79
+ if directoryContent == nil {
80
+ return nil , syscall .ENOTDIR
81
+ }
82
+
83
+ var entries []string
84
+ for _ , fileContent := range directoryContent {
85
+ entries = append (entries , * fileContent .Name )
86
+ }
87
+
88
+ return entries , nil
89
+ }
90
+
60
91
func (gh * GitHub ) client (ctx context.Context ) * github.Client {
61
92
var client * http.Client
62
93
@@ -69,3 +100,23 @@ func (gh *GitHub) client(ctx context.Context) *github.Client {
69
100
70
101
return github .NewClient (client )
71
102
}
103
+
104
+ func (gh * GitHub ) getContentsWrapper (
105
+ ctx context.Context ,
106
+ path string ,
107
+ ) (* github.RepositoryContent , []* github.RepositoryContent , error ) {
108
+ fileContent , directoryContent , resp , err := gh .client (ctx ).Repositories .GetContents (ctx , gh .owner , gh .repo , path ,
109
+ & github.RepositoryContentGetOptions {
110
+ Ref : gh .reference ,
111
+ },
112
+ )
113
+ if err != nil {
114
+ if resp != nil && resp .StatusCode == http .StatusNotFound {
115
+ return nil , nil , os .ErrNotExist
116
+ }
117
+
118
+ return nil , nil , fmt .Errorf ("%w: %v" , ErrAPI , err )
119
+ }
120
+
121
+ return fileContent , directoryContent , nil
122
+ }
0 commit comments