-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
31 lines (28 loc) · 832 Bytes
/
github.go
File metadata and controls
31 lines (28 loc) · 832 Bytes
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func GetFile(url string) (b []byte, err error) {
if match := regexp.MustCompile(`^https://github.com/([\w_-]+)/([\w_-]+)/blob/[\w_-]+/([\w/._-]+)$`).FindAllStringSubmatch(url, -1); match != nil {
return getFile(match[0][1], match[0][2], match[0][3])
}
return []byte{}, fmt.Errorf("[Error] Invalid URL format: %s", url)
}
func getFile(org string, repo string, path string) (b []byte, err error) {
client := &http.Client{}
req, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/%s/contents/%s", org, repo, path), nil)
req.Header.Set("Accept", "application/vnd.github.v3.raw")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return
}