-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (86 loc) · 2.2 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
package main
import (
"flag"
"ghausage/cmd"
"log"
"os"
)
var v bool
var countCmd *flag.FlagSet
var sumCmd *flag.FlagSet
var repo string
var startDate string
var endDate string
func init() {
countCmd = flag.NewFlagSet("count", flag.ExitOnError)
SetFlagOption(countCmd)
countCmd.Usage = PrintCountHelp
sumCmd = flag.NewFlagSet("sum", flag.ExitOnError)
SetFlagOption(sumCmd)
sumCmd.Usage = PrintSumHelp
flag.BoolVar(&v, "v", false, "Show Version")
flag.BoolVar(&v, "version", false, "Show Version")
}
func SetFlagOption(flag *flag.FlagSet) {
flag.StringVar(&repo, "repo", "", "target repository (format: OWNER/REPO)")
flag.StringVar(&startDate, "start", "", "start date of count range (format: yyyy-mm-dd)")
flag.StringVar(&endDate, "end", "", "end date of count range (format: yyyy-mm-dd)")
}
func main() {
flag.Usage = PrintHelp
flag.Parse()
if v {
cmd.PrintVersion()
return
}
token := os.Getenv("GITHUB_TOKEN")
if len(os.Args) == 1 {
PrintHelp()
os.Exit(1)
}
switch os.Args[1] {
case "count":
err := countCmd.Parse(os.Args[2:])
if err != nil {
panic(err)
}
if repo == "" || startDate == "" || endDate == "" {
PrintCountHelp()
os.Exit(1)
}
err = cmd.CountCommand{Logger: log.Default()}.Run(os.Stdout, repo, startDate, endDate, token)
if err != nil {
panic(err)
}
return
case "sum":
err := sumCmd.Parse(os.Args[2:])
if err != nil {
panic(err)
}
if repo == "" || startDate == "" || endDate == "" {
PrintSumHelp()
os.Exit(1)
}
err = cmd.SumCommand{Logger: log.Default()}.Run(os.Stdout, repo, startDate, endDate, token)
if err != nil {
panic(err)
}
return
default:
}
}
func PrintCountHelp() {
println("Usage of count: ./ghausage count --repo OWNER/REPO --start YYYY-MM-DD --end YYYY-MM-DD")
countCmd.PrintDefaults()
}
func PrintSumHelp() {
println("Usage of sum: ./ghausage sum --repo OWNER/REPO --start YYYY-MM-DD --end YYYY-MM-DD")
sumCmd.PrintDefaults()
}
func PrintHelp() {
println("Usage of ./ghausage")
println("./ghausage [-v | --version] [-h | --help]")
println("./ghausage count Count GitHub Actions workflow runs of specified repository")
println("./ghausage sum Sum GitHub Actions usage time of specified repository")
}