-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (107 loc) · 2.62 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"./model"
"github.com/urfave/cli"
)
var GlobalFlags = []cli.Flag{}
var Commands = []cli.Command{}
func CommandNotFound(c *cli.Context, command string) {
fmt.Fprintf(os.Stderr, "%s: '%s' is not a %s command. See '%s --help'.", c.App.Name, command, c.App.Name, c.App.Name)
os.Exit(2)
}
func existUser(u string) bool {
url := "http://github.com/" + u
response, err := http.Get(url)
if err != nil {
os.Exit(2)
}
if response.StatusCode == 200 {
return true
} else {
return false
}
}
func validate(u string, s string, e string) {
r := regexp.MustCompile(`^(\d{4})-(\d{2})-(\d{2})$`)
if len(u) == 0 || len(s) == 0 || len(e) == 0 {
fmt.Printf("-u, -s, -e is required.]\n")
os.Exit(1)
} else if !r.MatchString(s) || !r.MatchString(e) {
fmt.Printf("Invalid arguments --start %s --end %s\n", s, e)
os.Exit(1)
} else if !existUser(u) {
fmt.Printf("User %s is not found\n", u)
os.Exit(1)
} else {
fmt.Printf("Validation is OK!\n")
}
}
func getContrib(u string, s string, e string) string {
url := "https://api.github.com/search/issues?q=type:pr+in:body+is:merged+merged:" + s + ".." + e + "+author:" + u
response, err := http.Get(url)
if err != nil {
os.Exit(2)
}
defer response.Body.Close()
body, error := ioutil.ReadAll(response.Body)
if error != nil {
log.Fatal(error)
}
return string(body)
}
func main() {
app := cli.NewApp()
app.Name = Name
app.Version = Version
app.Author = "Takeshi Kondo"
app.Email = "take.she12@gmail.com"
app.Usage = ""
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "user, u",
Usage: "Specify github user name",
},
cli.StringFlag{
Name: "start, s",
Usage: "Specify start date. format is YYYY-MM-DD",
},
cli.StringFlag{
Name: "end, e",
Usage: "Specify end date. format is YYYY-MM-DD",
},
}
app.Commands = Commands
app.CommandNotFound = CommandNotFound
app.Action = func(c *cli.Context) error {
/* parse arguments */
user := c.String("user")
startdate := c.String("start")
enddate := c.String("end")
//validate(user, startdate, enddate)
/* get the contribution as JSON from GitHub.com */
result := getContrib(user, startdate, enddate)
var _ = result // avoid build error
/* parse json */
jsonBytes := ([]byte)(result)
data := new(model.AutoGenerated)
if err := json.Unmarshal(jsonBytes, data); err != nil {
fmt.Println("JSON Unmarshal error:", err)
os.Exit(1)
}
/* Output console */
for _, v := range data.Items {
fmt.Println(v.Title)
fmt.Println(v.URL)
fmt.Println("")
}
return nil
}
app.Run(os.Args)
}