-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
274 lines (245 loc) · 6.59 KB
/
config.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package website
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"regexp"
"strconv"
"strings"
)
const (
expectedSectionCount = 6
loginSectionIndex = 0
projectsSectionIndex = 1
contributionsSectionIndex = 2
keyboardsSectionIndex = 3
illustrationsSectionIndex = 4
woodworkingSectionIndex = 5
)
// Config represents website configuration settings.
type Config struct {
GitHubLogin string
InstagramHandle string
Projects []*ProjectConfig
Contributions []*ContributionConfig
Keyboards []*CreationConfig
Illustrations []*CreationConfig
Woodworking []*CreationConfig
}
// ProjectConfig represents a project config item.
type ProjectConfig struct {
Owner string
Name string
}
// ContributionConfig represents a contribution config item.
type ContributionConfig struct {
Owner string
Name string
Pull int
Issue int
}
// CreationConfig represents a creation config item.
type CreationConfig struct {
Title string
ImageURL string
BackgroundColor string
Link string
}
// Parse populates the fields of its receiver with unmarshalled contents from the raw config.
func (c *Config) Parse(text string) (*Config, error) {
// Remove comments.
commentPattern := regexp.MustCompile("(^|\n)#[^\n]*")
simplifiedText := commentPattern.ReplaceAllString(text, "")
sections := strings.Split(strings.TrimSpace(simplifiedText), "\n\n")
if len(sections) < expectedSectionCount {
return nil, fmt.Errorf("missing config sections (%v/%v of login, projects, contributions, creations)", len(sections), expectedSectionCount)
}
if len(sections) > expectedSectionCount {
return nil, fmt.Errorf("malformed config, too many sections (%v > %v)", len(sections), expectedSectionCount)
}
loginLines := strings.Split(sections[loginSectionIndex], "\n")
c.GitHubLogin = loginLines[0]
c.InstagramHandle = loginLines[1]
c.Projects = []*ProjectConfig{}
projects := []string{}
if sections[projectsSectionIndex] != "" {
projects = strings.Split(sections[projectsSectionIndex], "\n")
}
for _, project := range projects {
name := strings.Split(project, "/")
if len(name) != 2 {
return nil, fmt.Errorf("malformed project config: \"%v\"", project)
}
c.Projects = append(c.Projects, &ProjectConfig{
Owner: string(name[0]),
Name: string(name[1]),
})
}
c.Contributions = []*ContributionConfig{}
contributions := []string{}
if sections[contributionsSectionIndex] != "" {
contributions = strings.Split(sections[contributionsSectionIndex], "\n")
}
for _, contribution := range contributions {
parts := splitRepeated(contribution, " ")
if len(parts) != 3 {
return nil, fmt.Errorf("malformed contribution config: \"%v\"", contribution)
}
name := strings.Split(parts[0], "/")
if len(name) != 2 {
return nil, fmt.Errorf("malformed contribution config: \"%v\"", contribution)
}
pull, err := strconv.Atoi(string(parts[1]))
if err != nil {
return nil, fmt.Errorf("could not parse contribution pull number in \"%v\": %v", contribution, err)
}
issue, err := strconv.Atoi(string(parts[2]))
if err != nil {
return nil, fmt.Errorf("could not parse contribution issue number in \"%v\": %v", contribution, err)
}
c.Contributions = append(c.Contributions, &ContributionConfig{
Owner: string(name[0]),
Name: string(name[1]),
Pull: pull,
Issue: issue,
})
}
keyboards, err := parseCreationSection(sections[keyboardsSectionIndex])
if err != nil {
return nil, fmt.Errorf("could not parse creation: %v", err)
}
c.Keyboards = keyboards
illustrations, err := parseCreationSection(sections[illustrationsSectionIndex])
if err != nil {
return nil, fmt.Errorf("could not parse creation: %v", err)
}
c.Illustrations = illustrations
woodworking, err := parseCreationSection(sections[woodworkingSectionIndex])
if err != nil {
return nil, fmt.Errorf("could not parse creation: %v", err)
}
c.Woodworking = woodworking
return c, nil
}
func parseCreationSection(section string) ([]*CreationConfig, error) {
items := []*CreationConfig{}
lines := []string{}
if section != "" {
lines = strings.Split(section, "\n")
}
for _, keyboard := range lines {
if !strings.Contains(keyboard, "|") {
return nil, fmt.Errorf("creation missing separator: \"%v\"", keyboard)
}
sections := splitRepeated(keyboard, "|")
if len(sections) != 2 {
return nil, fmt.Errorf("malformed creation config: \"%v\"", keyboard)
}
metadata := splitRepeated(sections[1], " ")
if len(metadata) != 3 {
return nil, fmt.Errorf("missing creation metadata: \"%v\"", keyboard)
}
items = append(items, &CreationConfig{
Title: strings.TrimSpace(sections[0]),
ImageURL: metadata[1],
BackgroundColor: metadata[0],
Link: metadata[2],
})
}
return items, nil
}
// Repeatedly split the input string using the separator.
// Empty items are removed.
func splitRepeated(s, sep string) []string {
raw := strings.Split(s, sep)
clean := []string{}
for i := 0; i < len(raw); i++ {
if raw[i] != "" {
clean = append(clean, raw[i])
}
}
return clean
}
// Query generates a GraphQL query string from its receiver's fields.
func (c *Config) Query() ([]byte, error) {
tmpl, err := template.New("test").Parse(query)
if err != nil {
return nil, fmt.Errorf("could not parse query template: %v", err)
}
templateContent := bytes.Buffer{}
err = tmpl.Execute(&templateContent, c)
if err != nil {
return nil, fmt.Errorf("could not execute query template: %v", err)
}
r := &struct {
Query string `json:"query"`
}{
Query: templateContent.String(),
}
b, err := json.Marshal(r)
if err != nil {
return nil, fmt.Errorf("could not marshal cuest: %v", err)
}
return b, nil
}
// GraphQL query template sent to GitHub api.
var query = `
query {
user(login: "{{.GitHubLogin}}") {
avatarUrl
email
bio
name
login
location
url
}
{{range $i, $p := .Projects}}
p{{$i}}: repository(owner: "{{$p.Owner}}", name: "{{$p.Name}}") {
...RepoInfo
}
{{end}}
{{range $i, $c :=.Contributions}}
c{{$i}}: repository(owner: "{{$c.Owner}}", name: "{{$c.Name}}") {
name
owner {
login
}
url
{{if not (eq $c.Pull 0)}}
pullRequest(number: {{$c.Pull}}) {
number
title
url
}
{{end}}
{{if not (eq $c.Issue 0)}}
issue(number: {{$c.Issue}}) {
number
url
}
{{end}}
}
{{end}}
}
fragment RepoInfo on Repository {
nameWithOwner
name
owner {
login
}
description
url
homepageUrl
stargazers {
totalCount
}
languages(first: 3, orderBy: {field: SIZE, direction: DESC}) {
nodes {
name
color
}
}
}
`