forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
42 lines (39 loc) · 980 Bytes
/
http.go
File metadata and controls
42 lines (39 loc) · 980 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
32
33
34
35
36
37
38
39
40
41
42
package dif
import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
)
func getFileContent(cache Cache, httpClient *http.Client, repo ghrepo.Interface, file, ref string) (string, error) {
filepath := filepath.Join(os.TempDir(), "gh-cli-cache", ref, file)
if !cache.Exists(filepath) {
client := api.NewClientFromHTTP(httpClient)
path := fmt.Sprintf("repos/%s/contents/%s", ghrepo.FullName(repo), file)
if ref != "" {
q := fmt.Sprintf("?ref=%s", url.QueryEscape(ref))
path = path + q
}
type Result struct {
Content string
}
var result Result
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
if err != nil {
return "", err
}
decoded, err := base64.StdEncoding.DecodeString(result.Content)
if err != nil {
return "", err
}
err = cache.Create(filepath, decoded)
if err != nil {
return "", err
}
}
return filepath, nil
}