/
github.go
48 lines (37 loc) · 1.07 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
package main
import (
"context"
"fmt"
"net/http"
"github.com/google/go-github/github"
)
func fetchLicenseList() ([]*github.License, error) {
// Create default client
client := github.NewClient(nil)
// Fetch list of LICENSE from Github API
list, res, err := client.Licenses.List(context.Background())
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid status code from GitHub\n %s\n", res.String())
}
return list, nil
}
// fetchLicense fetches LICENSE file from Github API.
// if something wrong returns error.
func fetchLicense(key string) (string, error) {
// Create default client
client := github.NewClient(nil)
// Fetch a LICENSE from Github API
Debugf("Fetch license from GitHub API by key: %s", key)
license, res, err := client.Licenses.Get(context.Background(), key)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("invalid status code from GitHub\n %s\n", res.String())
}
Debugf("Fetched license name: %s", *license.Name)
return *license.Body, nil
}