-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (103 loc) · 2.57 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/fatih/color"
)
var contributors = []string{
"DennyLoko", "hernandev", "joubertredrat", "lucasmezencio", "marcossffilho",
"marcusagm", "naroga", "pedrommone",
}
var contributorsVotes = map[string]bool{}
var colorMap = map[VoteValue]map[bool]color.Attribute{
"+1": map[bool]color.Attribute{
true: color.FgHiGreen,
false: color.FgGreen,
},
"-1": map[bool]color.Attribute{
true: color.FgHiRed,
false: color.FgRed,
},
}
func main() {
if len(os.Args) != 2 {
color.Red("Invalid arguments")
os.Exit(1)
}
fmt.Println("Getting poll details...")
fmt.Println()
url := fmt.Sprintf("https://api.github.com/repos/phpol/phpol/issues/%s", os.Args[1])
voteJSON, err := getJSON(url)
if err != nil {
panic(err)
}
p := NewPollFromJSON(voteJSON)
votesJSON, err := getJSON(p.VotesSummary["url"].(string))
if err != nil {
panic(err)
}
p.LoadVotes(votesJSON)
fmt.Println(fmt.Sprintf("Poll Title: %s", p.Title))
fmt.Println(fmt.Sprintf(
"Author: %s | Date: %s",
p.Author.Login,
p.StartTime.UTC().Format("2006-01-02 15:04:05 MST")))
fmt.Print("State: ")
color.Set(color.FgHiGreen)
if p.State == PollStateClosed {
color.Set(color.FgHiRed)
}
fmt.Println(p.State)
color.Unset()
fmt.Println("------------------------------------------------------------")
fmt.Println(p.Description)
fmt.Println("------------------------------------------------------------")
for _, vote := range p.Votes {
if fgColor, ok := colorMap[vote.Content][isContributor(vote.User.Login)]; ok {
color.Set(fgColor)
}
fmt.Print(fmt.Sprintf("%s ", vote.User.Login))
contributorsVotes[vote.User.Login] = true
color.Unset()
}
color.Set(color.FgYellow)
for _, contributor := range contributors {
if _, ok := contributorsVotes[contributor]; !ok {
fmt.Print(fmt.Sprintf("%s ", contributor))
}
}
color.Unset()
fmt.Println()
}
func getJSON(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header = map[string][]string{
"Accept": {"application/vnd.github.squirrel-girl-preview"},
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GitHub replied with %d status code", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return body, nil
}
func isContributor(name string) bool {
for _, contributor := range contributors {
if name == contributor {
return true
}
}
return false
}