-
Notifications
You must be signed in to change notification settings - Fork 2
/
gist.go
53 lines (45 loc) · 959 Bytes
/
gist.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
49
50
51
52
53
/*
* @Author: snakelu
* @Date: 2023/02/24 9:55 上午
* @Last Modified by: snakelu
* @Last Modified time: 2023/02/24 9:55 上午
*/
package pkg
import (
"bytes"
"context"
"log"
"net/http"
"os"
"path/filepath"
"github.com/google/go-github/v41/github"
"golang.org/x/oauth2"
)
// GistClone clone gist repo
func GistClone(id, dir, accessToken string) error {
ctx := context.Background()
var tc *http.Client
if accessToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc = oauth2.NewClient(ctx, ts)
}
client := github.NewClient(tc)
gist, _, err := client.Gists.Get(ctx, id)
if err != nil {
log.Fatal(err)
return err
}
if !PathExist(dir) {
_ = PathCreate(dir)
}
// copy file to directory
for _, f := range gist.Files {
err = FileOpen(*bytes.NewBufferString(f.GetContent()), filepath.Join(dir, f.GetFilename()), os.ModePerm)
if err != nil {
return err
}
}
return nil
}