-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (95 loc) · 2.38 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
102
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"github.com/TBXark/github-status/config"
"github.com/TBXark/github-status/query"
"github.com/TBXark/github-status/render"
"github.com/TBXark/github-status/stats"
"log"
"net/http"
"os"
)
func main() {
output := flag.String("output", "output", "The output directory")
debug := flag.Bool("debug", false, "Enable debug mode")
flag.Parse()
conf := config.NewConfig(func(token string) bool {
return query.NewQueries(token).IsValid()
})
if conf == nil {
log.Fatalf("Invalid config")
}
loader := stats.NewStats(
conf.UserName,
conf.AccessToken,
stats.IgnoreForkedRepos(conf.IgnoreForkedRepos),
stats.IgnoreArchivedRepos(conf.IgnoreArchivedRepos),
stats.IgnorePrivateRepos(conf.IgnorePrivateRepos),
stats.IgnoreContributedToRepos(conf.IgnoreContributedToRepos),
stats.IgnoreLinesChanged(conf.IgnoreLinesChanged),
stats.IgnoreRepoViews(conf.IgnoreRepoViews),
stats.ExcludeRepos(conf.ExcludeRepos...),
stats.ExcludeLangs(conf.ExcludeLangs...),
stats.IncludeOwner(conf.IncludeOwner...),
)
stat, err := loader.GetStats(context.Background())
if err != nil {
log.Fatalf("Failed to get stats: %v", err)
}
if e := saveStat(stat, *output); e != nil {
log.Printf("Failed to save stat: %v", e)
}
if e := sendWebhook(conf, stat); e != nil {
log.Printf("Failed to send webhook: %v", e)
}
if *debug {
data, _ := json.MarshalIndent(stat, "", " ")
_ = os.WriteFile(*output+"/data.json", data, 0644)
}
}
func saveStat(stat *stats.Stats, output string) error {
err := os.MkdirAll(output, 0755)
if err != nil {
return err
}
overview, err := render.OverviewSVG(stat)
if err != nil {
return err
}
err = overview.WriteToPath(output + "/overview.svg")
if err != nil {
return err
}
languages, err := render.LanguagesSVG(stat)
if err != nil {
return err
}
err = languages.WriteToPath(output + "/languages.svg")
if err != nil {
return err
}
return nil
}
func sendWebhook(conf *config.Config, obj any) error {
if conf.WebhookURL == "" {
return nil
}
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
req, err := http.NewRequest("POST", conf.WebhookURL, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}