-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (81 loc) · 2.43 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import "flag"
import "fmt"
import "io/ioutil"
import "github.com/google/go-github/github"
import "golang.org/x/oauth2"
import "os"
import "os/user"
func usage() {
fmt.Println("Usage:")
fmt.Println("\t gogist a.txt b.txt")
fmt.Println("\t gogist -f filename.txt < a.txt")
fmt.Println("\t pass -p to make the gist private")
fmt.Println("\t pass -d 'the gist description' to add a description")
os.Exit(1)
}
func getAccessToken() string {
usr, err := user.Current()
if err != nil {
fmt.Fprintln(os.Stderr, "Error checking current user.")
os.Exit(1)
}
f := usr.HomeDir + "/.config/gogist/GH-ACCESS-TOKEN"
bytes, err := ioutil.ReadFile(f)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading file %s", f)
os.Exit(1)
}
return string(bytes)
}
func create(desc string, pub bool, files map[string]string) (*github.Gist, error) {
ghat := getAccessToken()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghat},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
f := make(map[github.GistFilename]github.GistFile)
for k := range files {
_k := github.GistFilename(k)
f[_k] = github.GistFile{Content: github.String(files[k])}
}
gist := &github.Gist{
Description: github.String(desc),
Public: github.Bool(pub),
Files: f,
}
gist, _, err := client.Gists.Create(gist)
return gist, err
}
func main() {
var fname = flag.String("f", "", "File name")
var desc = flag.String("d", "", "Description")
var public = flag.Bool("p", false, "Is public, default false")
flag.Parse()
files := flag.Args()
fileMap := make(map[string]string)
if len(files) > 0 {
for _, f := range files {
bytes, err := ioutil.ReadFile(f)
if err == nil {
fileMap[f] = string(bytes)
} else {
fmt.Fprintln(os.Stderr, "Error reading file %s", f)
os.Exit(1)
}
}
} else if *fname != "" {
// TODO: Check whether there's anythin to read on stdin.
bytes, err := ioutil.ReadAll(os.Stdin)
if err == nil {
fileMap[*fname] = string(bytes)
}
} else {
usage()
}
res, err := create(*desc, *public, fileMap)
if err == nil {
fmt.Println(*res.HTMLURL)
}
}