-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
80 lines (70 loc) · 1.62 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 (
"fmt"
"log"
"os"
"path/filepath"
"sync"
"text/template"
"github.com/AlphaWong/Stars/services"
"github.com/go-playground/validator/v10"
)
var validate *validator.Validate
type BaseConfig struct {
Token string `validate:"required"`
UserName string `validate:"required"`
BaseTemplate string `validate:"required"`
OutputPath string `validate:"required"`
mu sync.Mutex
}
func main() {
config := boot()
run(config)
}
func boot() (config *BaseConfig) {
// Githun access token
token := os.Getenv("TOKEN")
// Github username
userName := "alphawong"
config = &BaseConfig{
Token: token,
UserName: userName,
BaseTemplate: "./template/starred.md",
OutputPath: "./out.md",
}
// ensure the config is valid
validConfig(config)
return
}
func validConfig(config *BaseConfig) {
validate = validator.New()
err := validate.Struct(config)
if nil != err {
for _, err := range err.(validator.ValidationErrors) {
log.Printf(err.Error())
}
log.Panicln("Invalid config struct")
}
}
func run(config *BaseConfig) {
fetcher, err := services.NewGitHubFetcher(
services.WithToken(config.Token),
services.WithUserName(config.UserName),
)
if nil != err {
fmt.Print(err.Error())
return
}
results := fetcher.GetUsersStars()
baseTemplatePath, _ := filepath.Abs(config.BaseTemplate)
outputPath, _ := filepath.Abs(config.OutputPath)
printer, err := services.NewTplPrinter(
services.WithBaseTemplate(template.ParseFiles(baseTemplatePath)),
services.WithOutputPath(outputPath),
)
if nil != err {
fmt.Print(err.Error())
return
}
printer.PrintSlice(results)
}