-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
35 lines (29 loc) · 857 Bytes
/
repository.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
package main
import (
"encoding/json"
"os"
"net/http"
)
type repository struct {
Name string `json:"full_name"`
}
func fetchRepositories(user string) []repository {
const usersApiEndpoint = "https://api.github.com/users/"
resp, err := http.Get(usersApiEndpoint + user + "/repos")
if err != nil {
os.Stderr.WriteString("Error: The HTTP get request failed. Error message: ")
os.Stderr.WriteString(err.Error())
os.Stderr.WriteString("\n")
os.Exit(1)
}
defer resp.Body.Close()
repos := []repository{}
err = json.NewDecoder(resp.Body).Decode(&repos)
if err != nil {
os.Stderr.WriteString("Error: Failed to decode JSON data. A likely culprit is that the GitHub API limit was likely reached.\nTry again in a few hours. Error message: ")
os.Stderr.WriteString(err.Error())
os.Stderr.WriteString("\n")
os.Exit(1)
}
return repos
}