@@ -28,11 +28,11 @@ var (
28
28
// Repository defines methods for interacting with GitHub.
29
29
type Repository interface {
30
30
extract (url string ) error
31
- downloadContents (ctx context.Context ) error
31
+ download (ctx context.Context ) error
32
32
contents (ctx context.Context , wg * sync.WaitGroup , path string , errCh chan error )
33
- downloadFile (url , path string ) error
34
- clientStatus (ctx context.Context ) error
35
- clientAuth (ctx context.Context ) error
33
+ getFile (url , path string ) error
34
+ status (ctx context.Context ) error
35
+ auth (ctx context.Context ) error
36
36
}
37
37
38
38
// Ensure GitHub implements the Repository interface.
@@ -69,10 +69,12 @@ func (g *GitHub) extract(url string) error {
69
69
return nil
70
70
}
71
71
72
- // downloadContents downloads the contents concurrently.
73
- func (g * GitHub ) downloadContents (ctx context.Context ) error {
72
+ // download downloads the contents concurrently.
73
+ func (g * GitHub ) download (ctx context.Context ) error {
74
74
wg := & sync.WaitGroup {}
75
75
errCh := make (chan error , 1 )
76
+ ctx , cancel := context .WithTimeout (ctx , downloadLimit * time .Second )
77
+ defer cancel ()
76
78
77
79
wg .Add (1 )
78
80
go g .contents (ctx , wg , g .Path , errCh )
@@ -87,7 +89,7 @@ func (g *GitHub) downloadContents(ctx context.Context) error {
87
89
select {
88
90
case err := <- errCh :
89
91
if err != nil {
90
- return err
92
+ return fmt . Errorf ( "failed to download: %v" , err )
91
93
}
92
94
case <- ctx .Done ():
93
95
if errors .Is (ctx .Err (), context .Canceled ) {
@@ -112,7 +114,7 @@ func (g *GitHub) contents(ctx context.Context, wg *sync.WaitGroup, path string,
112
114
113
115
// If the URL points to a file, only the file is downloaded.
114
116
if len (directoryContent ) == 0 && fileContent != nil {
115
- if err := g .downloadFile (fileContent .GetDownloadURL (), fileContent .GetPath ()); err != nil {
117
+ if err := g .getFile (fileContent .GetDownloadURL (), fileContent .GetPath ()); err != nil {
116
118
errCh <- err
117
119
return
118
120
}
@@ -127,7 +129,7 @@ func (g *GitHub) contents(ctx context.Context, wg *sync.WaitGroup, path string,
127
129
switch content .GetType () {
128
130
case "file" :
129
131
// Download the file directly.
130
- if err := g .downloadFile (content .GetDownloadURL (), content .GetPath ()); err != nil {
132
+ if err := g .getFile (content .GetDownloadURL (), content .GetPath ()); err != nil {
131
133
errCh <- err
132
134
return
133
135
}
@@ -140,8 +142,8 @@ func (g *GitHub) contents(ctx context.Context, wg *sync.WaitGroup, path string,
140
142
}
141
143
}
142
144
143
- // downloadFile downloads a file from the given URL and saves it.
144
- func (g * GitHub ) downloadFile (url , path string ) error {
145
+ // getFile retrieves a file from the given URL and saves it.
146
+ func (g * GitHub ) getFile (url , path string ) error {
145
147
if url == "" || path == "" {
146
148
return ErrInvalidPathURL
147
149
}
@@ -156,14 +158,17 @@ func (g *GitHub) downloadFile(url, path string) error {
156
158
return saveFile (g .Path , path , resp .Body )
157
159
}
158
160
159
- // clientStatus reports the status of the client, the remaining hourly
161
+ // status reports the status of the client, the remaining hourly
160
162
// rate limit, and the time at which the current rate limit will reset.
161
163
// This function does not reduce the rate limit. It can be used freely.
162
- func (g * GitHub ) clientStatus (ctx context.Context ) error {
164
+ func (g * GitHub ) status (ctx context.Context ) error {
163
165
auth := "NOT Authorized"
166
+ ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
167
+ defer cancel ()
168
+
164
169
rate , _ , err := g .Client .RateLimit (ctx )
165
170
if err != nil {
166
- return err
171
+ return fmt . Errorf ( "failed to check status: %v" , err )
167
172
}
168
173
169
174
if token .Get () != "" && rate .Core .Limit > baseRateLimit {
@@ -176,13 +181,17 @@ func (g *GitHub) clientStatus(ctx context.Context) error {
176
181
return nil
177
182
}
178
183
179
- // clientAuth reports the authenticated username, if applicable.
184
+ // auth reports the authenticated username, if applicable.
180
185
// This function reduces the rate limit for each request.
181
- func (g * GitHub ) clientAuth (ctx context.Context ) error {
186
+ func (g * GitHub ) auth (ctx context.Context ) error {
187
+ ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
188
+ defer cancel ()
189
+
182
190
u , _ , err := g .Client .GetUser (ctx , "" )
183
191
if err != nil {
184
- return err
192
+ return fmt . Errorf ( "failed to check auth: %v" , err )
185
193
}
194
+
186
195
fmt .Printf ("Authenticated as @%s \n " , u .GetLogin ())
187
196
188
197
return nil
0 commit comments