/
gist.go
48 lines (41 loc) · 900 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
package gist
import (
"context"
"errors"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type Gist struct {
Client *github.Client
}
func New(token string) (*Gist, error) {
if token == "" {
return &Gist{}, errors.New("token is missing")
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
return &Gist{Client: github.NewClient(tc)}, nil
}
func (g *Gist) Create(
ctx context.Context,
files map[github.GistFilename]github.GistFile,
desc string,
public bool) (*github.Gist, error) {
item, resp, err := g.Client.Gists.Create(ctx, &github.Gist{
Files: files,
Description: &desc,
Public: &public,
})
if err != nil {
return nil, err
}
if item == nil {
return nil, errors.New("gist item is nil")
}
if resp == nil {
return nil, errors.New("resp is nil")
}
return item, nil
}