-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
90 lines (75 loc) · 1.73 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"golang.org/x/oauth2"
"github.com/codegangsta/cli"
"github.com/google/go-github/github"
)
var createCommand = cli.Command{
Name: "create",
Usage: "create labels from a definition file",
Action: doCreateCommand,
Flags: []cli.Flag{
cli.StringFlag{Name: "labels", Value: "default.json", Usage: "labels definition file"},
},
}
func doCreateCommand(c *cli.Context) {
f, err := os.Open(c.String("labels"))
if err != nil {
log.Fatal(err)
}
defer f.Close()
var labels []*github.Label
if err := json.NewDecoder(f).Decode(&labels); err != nil {
log.Fatal(err)
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: getGitHubToken(c)})
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
for _, repoSpec := range c.Args() {
repo := strings.SplitN(repoSpec, "/", 2)
for _, l := range labels {
_, _, err := client.Issues.CreateLabel(repo[0], repo[1], l)
if err != nil {
log.Fatalf("Error creating label %q: %v", *l.Name, err)
}
}
}
}
func getGitHubToken(c *cli.Context) string {
if v := c.GlobalString("token"); v != "" {
return c.GlobalString(v)
}
if v := c.GlobalString("token-file"); v != "" {
if b, err := ioutil.ReadFile(v); err == nil {
return string(b)
}
}
return ""
}
func main() {
app := cli.NewApp()
app.Name = "label-all-the-things"
app.Usage = "Manipulate GitHub labels"
app.Version = "0.1.0"
app.Commands = []cli.Command{
createCommand,
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "token",
Usage: "GitHub API token",
},
cli.StringFlag{
Name: "token-file",
Usage: "GitHub API token file",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}