-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
201 lines (176 loc) · 5.07 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"encoding/json"
"io"
"log/slog"
"os"
"strings"
"time"
github "github.com/google/go-github/v58/github"
"github.com/spf13/viper"
)
type MetaRepo struct {
Workflows []*github.Workflow `json:"workflows"`
Repo *github.Repository `json:"repo"`
ExternalBadges []*ExternalBadge `json:"external"`
}
type MergeData struct {
Created string `json:"created"`
Title string `json:"title"`
Header string `json:"header"`
Footer string `json:"footer"`
Repos []*MetaRepo `json:"repos"`
}
var (
// version info
version string
commit string
date string
builtBy string
// workflow flags
IncludeSet map[string]struct{}
ExcludeSet map[string]struct{}
SkipSet map[string]struct{}
// external badge flags
Externals []string
)
func main() {
initConfig(os.Args[1:])
initLogger()
repos := viper_GetStringSlice("repos")
owners := viper_GetStringSlice("owners")
if len(owners) == 0 && len(repos) == 0 {
intro()
os.Exit(1)
}
includes := viper_GetStringSlice("include")
IncludeSet = make(map[string]struct{})
for _, include := range includes {
IncludeSet[strings.ToLower(include)] = struct{}{}
}
excludes := viper_GetStringSlice("exclude")
ExcludeSet = make(map[string]struct{})
for _, exclude := range excludes {
ExcludeSet[strings.ToLower(exclude)] = struct{}{}
}
skips := viper_GetStringSlice("skip")
SkipSet = make(map[string]struct{})
for _, skip := range skips {
SkipSet[strings.ToLower(skip)] = struct{}{}
}
client := github.NewClient(nil)
token := viper.GetString("github-token")
if token != "" {
slog.Info("using a token to make authorized requests")
client = client.WithAuthToken(token)
} else {
slog.Warn("token not set: will make anonymous (and rate-limited) Github API calls")
}
slog.Debug("owners", "owners", owners, "repos", repos)
var allRepos []*github.Repository
if len(repos) > 0 {
if repos[0][0] == '@' {
var atfileErr error
repos, atfileErr = loadAtFile(repos[0][1:])
if atfileErr != nil {
slog.Error("unable to load @file", "error", atfileErr, "filename", repos[0][1:])
os.Exit(11)
}
}
for _, repo := range repos {
theRepo, err := GetRepo(client, repo)
if err != nil {
slog.Error("unable to get repo", "error", err, "repo", repo)
os.Exit(2)
}
allRepos = append(allRepos, theRepo)
}
}
if len(owners) > 0 {
ownerRepos, err := GetRepos(client, owners)
if err != nil {
slog.Error("unable to get repos for owners", "error", err, "owners", owners)
os.Exit(3)
}
allRepos = append(allRepos, ownerRepos...)
}
if len(allRepos) == 0 {
slog.Error("no repos found", "repos", repos, "owners", owners)
os.Exit(4)
}
slog.Debug("repos found", "count", len(allRepos))
var allData []*MetaRepo
empty := viper.GetBool("empty")
for _, repo := range allRepos {
slog.Debug("loading workflows", "repo", *repo.FullName)
workflows, err := GetWorkflowsForRepo(client, repo)
if err != nil {
slog.Error("unable to load workflows", "error", err, "repo", *repo.FullName)
os.Exit(5)
}
slog.Info("workflows found", "repo", *repo.FullName, "count", len(workflows))
if empty || len(workflows) > 0 {
SortWorkflowsCaseInsensitive(workflows)
allData = append(allData, &MetaRepo{Workflows: workflows, Repo: repo})
}
}
slog.Info("repos with workflows", "count", len(allData))
SortReposCaseInsensitive(allData)
externals := viper_GetStringSlice("externals")
if len(externals) > 0 {
slog.Info("loading external badges", "count", len(externals))
for _, metaRepo := range allData {
for _, external := range externals {
xb, xbErr := GenerateExternalBadge(external, metaRepo.Repo)
if xbErr != nil {
slog.Error("unable to expand external badge", "error", xbErr, "external", external, "repo", *metaRepo.Repo.Name)
os.Exit(6)
}
metaRepo.ExternalBadges = append(metaRepo.ExternalBadges, xb)
}
}
}
filename := viper.GetString("output")
var writer io.Writer
if filename == "" || filename == "-" {
slog.Info("writing to stdout")
writer = os.Stdout
} else {
slog.Info("writing to file", "filename", filename)
file, openErr := os.Create(filename)
if openErr != nil {
slog.Error("unable to open file", "error", openErr, "filename", filename)
os.Exit(7)
}
defer file.Close()
writer = file
}
mergeData := &MergeData{
Created: time.Now().UTC().Format("2006-01-02 15:04:05"),
Title: viper.GetString("title"),
Header: viper.GetString("header"),
Footer: viper.GetString("footer"),
Repos: allData,
}
format := viper.GetString("format")
if format == "json" {
jsonStr, jsonErr := json.MarshalIndent(mergeData, "", " ")
if jsonErr != nil {
slog.Error("unable to marshal json", "error", jsonErr)
os.Exit(8)
}
writer.Write(jsonStr)
} else {
tmpl, tmplErr := GetStandardTemplate(format)
if tmplErr != nil {
slog.Error("unable to open template", "error", tmplErr, "format", format)
os.Exit(9)
}
mergeErr := tmpl.Execute(writer, mergeData)
if mergeErr != nil {
slog.Error("unable to merge template", "error", mergeErr, "format", format)
os.Exit(10)
}
}
slog.Info("done")
}