-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghs.go
121 lines (104 loc) · 2.44 KB
/
ghs.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
package main
import (
"errors"
"fmt"
"log"
"os"
"github.com/motemen/go-gitconfig"
"context"
)
// Version is ghs version number
const Version string = "0.0.10"
const (
// ExitCodeOK is 0
ExitCodeOK = iota
// ExitCodeError is 1
ExitCodeError
)
func main() {
num, err := ghs(os.Args[1:])
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(ExitCodeError)
}
Debug("Print %d\n", num)
os.Exit(ExitCodeOK)
}
func ghs(args []string) (int, error) {
flags, err := NewFlags(args)
// --help or error
if err != nil {
Debug("Error : help or parse error\n")
CheckVersion(Version)
flags.PrintHelp()
return 0, errors.New("help or parse error")
}
version, exitCode, sOpt := flags.ParseOption()
// --version
if version {
Printf("ghs %s\n", Version)
CheckVersion(Version)
return 0, nil
}
// error options
if exitCode == ExitCodeError {
Debug("Error : Parse option error flags.ParseOption()\n")
flags.PrintHelp()
CheckVersion(Version)
return 0, errors.New("Parse option error.")
}
getToken := func(optsToken string) string {
// -t or --token option
if optsToken != "" {
Debug("Github token get from option value\n")
return optsToken
}
// GITHUB_TOKEN environment
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
Debug("Github token get from environment value\n")
return token
}
// github.token in gitconfig
if token, err := gitconfig.GetString("github.token"); err == nil {
Debug("Github token get from gitconfig value\n")
return token
}
Debug("Github token not found\n")
return ""
}
sOpt.token = getToken(sOpt.token)
c := context.Background()
repo := NewRepo(NewSearch(c,sOpt))
reposChan, errChan := repo.Search(c)
Debug("main thread select start...\n")
reposNum := 0
for {
select {
case oneReqRepos := <-reposChan:
Debug("main thread chan reposChan\n")
Debug("main thread oneReqRepos length %d\n", len(oneReqRepos))
var end bool
end, reposNum = repo.Print(oneReqRepos)
Debug("reposNum : %d\n", reposNum)
if end {
Debug("over max\n")
return reposNum, nil
}
case err := <-errChan:
Debug("main thread chan err\n")
return 0, err
}
}
}
func Printf(format string, args ...interface{}) {
if os.Getenv("GHS_PRINT") != "no" {
fmt.Printf(format, args...)
}
}
// Debug display values when DEBUG mode
// This is used only for developer
func Debug(format string, args ...interface{}) {
if os.Getenv("GHS_DEBUG") != "" {
log.Printf(format, args...)
}
}