-
Notifications
You must be signed in to change notification settings - Fork 53
/
colors.go
47 lines (37 loc) · 1.12 KB
/
colors.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
package generator
import (
"bytes"
"io"
"io/ioutil"
"gopkg.in/yaml.v2"
)
// Colors generates a map in Go with language name -> color string.
// It is of generator.File type.
func Colors(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
data, err := ioutil.ReadFile(fileToParse)
if err != nil {
return err
}
languages := make(map[string]*languageInfo)
if err := yaml.Unmarshal(data, &languages); err != nil {
return err
}
langColorMap := buildLanguageColorMap(languages)
buf := &bytes.Buffer{}
if err := executeColorTemplate(buf, langColorMap, tmplPath, tmplName, commit); err != nil {
return err
}
return formatedWrite(outPath, buf.Bytes())
}
func buildLanguageColorMap(languages map[string]*languageInfo) map[string]string {
langColorMap := make(map[string]string)
for lang, info := range languages {
if len(info.Color) != 0 {
langColorMap[lang] = info.Color
}
}
return langColorMap
}
func executeColorTemplate(out io.Writer, langColorMap map[string]string, tmplPath, tmplName, commit string) error {
return executeTemplate(out, tmplName, tmplPath, commit, nil, langColorMap)
}