-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
87 lines (74 loc) · 1.88 KB
/
set.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"gopkg.in/urfave/cli.v2"
)
func set(user, repo, token string, labels GithubLabels) {
// Prepare url and client
url := githubAPIRoot + user + "/" + repo + "/labels" + "?access_token=" + token
client := &http.Client{}
// Cache len
l := len(labels)
// For each label
for x, label := range labels {
fmt.Printf("%d/%d (%s): ", x+1, l, label.Name)
// Marshal it as json
b, err := json.Marshal(label)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: %s\n", err.Error())
continue
}
// Create our request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: %s\n", err.Error())
continue
}
req.Header.Set("Content-Type", "application/json")
// Exectue
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: %s\n", err.Error())
continue
}
// If status differs from "Created" print error
if resp.StatusCode != 201 {
fmt.Fprintf(os.Stderr, "Warning: %s\n", resp.Status)
continue
}
fmt.Println("Ok")
}
}
// Set reads labels from a json file and set them on the repository passed as parameter
func Set(c *cli.Context) error {
// Check argc
if c.NArg() != 1 {
return cli.Exit(missingRepo, 1)
}
// Parse argument to retrieve repository
user, repo, err := parseRepository(c.Args().Get(0))
if err != nil {
return cli.Exit(err.Error(), 1)
}
// If file has not been passed as parameter
if c.String("labels") == "" {
return cli.Exit("Missing labels file", 1)
}
// Read file
file, err := ioutil.ReadFile(c.String("labels"))
if err != nil {
return cli.Exit(err.Error(), 1)
}
labels := GithubLabels{}
// Unmarshal labels into slice
if err := json.Unmarshal(file, &labels); err != nil {
return cli.Exit(err.Error(), 1)
}
set(user, repo, c.String("token"), labels)
return nil
}