-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
76 lines (63 loc) · 1.65 KB
/
args.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
package main
import (
"os"
"strings"
"time"
"vincent.click/pkg/octostats/commons"
"vincent.click/pkg/octostats/github"
"vincent.click/pkg/octostats/util"
)
// getAuth returns credentials from environment variables
func getAuth() github.Credentials {
auth := github.Credentials{
User: os.Getenv(GitHubUser),
Token: os.Getenv(GitHubToken),
}
if len(auth.User) < 1 {
log.Fatal("GitHub login user is empty. Set %s env variable", GitHubUser)
}
if len(auth.Token) < 1 {
log.Fatal("GitHub login token is empty. Set %s env variable", GitHubToken)
}
return auth
}
// getRepo returns the repository name from command-line arguments
func getRepo() string {
if len(os.Args) < 2 {
log.Fatal("repository name is required.")
}
repo := os.Args[1]
if !util.Matches(repositoryPattern, repo) {
log.Fatal("repository name '%s' is invalid; must be of the form `owner/name`", repo)
}
return repo
}
// getStartTime returns the time to start collecting stats from command-line arguments
func getStartTime() time.Time {
t := time.Time{}
if len(os.Args) >= 3 {
timeArg := os.Args[2]
if strings.HasPrefix(timeArg, "-") {
// use argument as time offset
t = getTimeFromDuration(timeArg)
} else {
// use argument as date
t = getTimeFromDate(timeArg)
}
}
return t
}
func getTimeFromDuration(rawDuration string) time.Time {
duration, err := time.ParseDuration(rawDuration)
if err != nil {
log.Fatal("invalid duration '%s'", rawDuration)
}
return time.Now().Add(duration)
}
func getTimeFromDate(rawDate string) time.Time {
t, err := time.Parse(commons.DateLayout, rawDate)
if err != nil {
log.Fatal("invalid date '%s'", rawDate)
}
return t
}