forked from lauripiispanen/most-active-github-users-counter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (65 loc) · 1.77 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
package main
import (
"bufio"
"flag"
"log"
"os"
"most-active-github-users-counter/output"
"most-active-github-users-counter/top"
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var locations arrayFlags
var excludeLocations arrayFlags
func main() {
token := flag.String("token", "", "Github auth token")
amount := flag.Int("amount", 256, "Amount of users to show")
considerNum := flag.Int("consider", 1000, "Amount of users to consider")
outputOpt := flag.String("output", "plain", "Output format: plain, csv")
fileName := flag.String("file", "", "Output file (optional, defaults to stdout)")
presetName := flag.String("preset", "", "Preset (optional)")
flag.Var(&locations, "location", "Location to query")
flag.Parse()
if *presetName != "" {
preset := Preset(*presetName)
locations = preset.include
excludeLocations = preset.exclude
}
var format output.Format
if *outputOpt == "plain" {
format = output.PlainOutput
} else if *outputOpt == "yaml" {
format = output.YamlOutput
} else if *outputOpt == "csv" {
format = output.CsvOutput
} else {
log.Fatal("Unrecognized output format: ", *outputOpt)
}
opts := top.Options{Token: *token, Locations: locations, ExcludeLocations: excludeLocations, Amount: *amount, ConsiderNum: *considerNum}
data, err := top.GithubTop(opts)
if err != nil {
log.Fatal(err)
}
var writer *bufio.Writer
if *fileName != "" {
f, err := os.Create(*fileName)
if err != nil {
log.Fatal(err)
}
writer = bufio.NewWriter(f)
defer f.Close()
} else {
writer = bufio.NewWriter(os.Stdout)
}
err = format(data, writer, opts)
if err != nil {
log.Fatal(err)
}
writer.Flush()
}