-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
91 lines (79 loc) · 1.79 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
package main
import (
"errors"
"fmt"
"os"
"path"
"regexp"
"github.com/BurntSushi/toml"
)
type config struct {
Site siteConfig `toml:"site"`
Repos []repoConfig `toml:"repos"`
}
func (c *config) validate() {
names := map[string]bool{}
for _, r := range c.Repos {
_, duplicate := names[r.Name]
if duplicate {
checkErr(errors.New(fmt.Sprintf("duplicate repo name: '%s'", r.Name)))
}
r.validate()
names[r.Name] = true
}
}
type repoConfig struct {
Name string `toml:"name"`
Description string `toml:"description"`
URL string `toml:"url"`
AltLink string `toml:"alt_link"`
}
func (r *repoConfig) validate() {
ok, err := regexp.MatchString(`[A-Za-z0-9_.-]`, r.Name)
checkErr(err)
if !ok {
checkErr(errors.New("repo names only allow [A-Za-z0-9_.-]"))
}
if r.Name == "git" {
checkErr(errors.New("repo in config cannot have the name 'git'"))
}
}
type siteConfig struct {
BaseURL string `toml:"base_url"`
Name string `toml:"name"`
}
// cloneDir gets the directory that this repo was cloned into using the output directory
// from the program arguments, and this repo's name.
func (r *repoConfig) cloneDir() string {
return path.Join(args.OutputDir, r.Name, "git")
}
func (r *repoConfig) findFileInRoot(oneOfThese map[string]bool) string {
dir, err := os.ReadDir(r.cloneDir())
checkErr(err)
for _, e := range dir {
name := e.Name()
if _, ok := oneOfThese[name]; ok {
return name
}
}
return ""
}
func parseConfig(data string) config {
conf := config{}
_, err := toml.Decode(data, &conf)
checkErr(err)
return conf
}
type repoData struct {
repoConfig
AltLink string
BaseURL string
HeadData HeadData
ReadMePath string
LicenseFilePath string
}
type HeadData struct {
GenTime string
BaseURL string
SiteName string
}