/
main.go
69 lines (58 loc) · 1.18 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"net/http"
"os"
"github.com/google/go-github/github"
"github.com/mitchellh/go-homedir"
"github.com/pocke/gha"
)
func main() {
err := CLIMain(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func CLIMain(args []string) error {
c, err := FlagParse(args)
if err != nil {
return err
}
token, err := getToken()
if err != nil {
return err
}
return CreateGist(c, token)
}
func CreateGist(cli *CLI, token gha.RoundTripper) error {
fname := github.GistFilename(cli.FileName)
files := make(map[github.GistFilename]github.GistFile)
files[fname] = github.GistFile{Content: &cli.FileContent}
c := github.NewClient(&http.Client{
Transport: token,
})
gist, _, err := c.Gists.Create(&github.Gist{
Files: files,
Public: &cli.Public,
})
if err != nil {
return err
}
fmt.Println(*gist.HTMLURL)
return nil
}
func getToken() (gha.RoundTripper, error) {
keyFile, err := homedir.Expand("~/.config/to-gist.githubkey")
if err != nil {
return "", err
}
token, err := gha.CLI(keyFile, &gha.Request{
Note: "to-gist",
Scopes: []string{"gist"},
})
if err != nil {
return "", err
}
return gha.RoundTripper(token), nil
}