Skip to content

Commit 5d9bf2e

Browse files
committed
Ensure every site configures markup
This avoids needing to notice and play whack-a-mole when things aren't configured as we expect. Also, add a --fix mode which will try to fix things for you.
1 parent 70aa8b2 commit 5d9bf2e

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
type hugoToml struct {
4+
Markup *hugoTomlMarkup `toml:"markup"`
5+
}
6+
7+
type hugoTomlMarkup struct {
8+
TableOfContents *hugoTomlTableOfContents `toml:"tableOfContents"`
9+
Goldmark *hugoTomlGoldmark `toml:"goldmark"`
10+
}
11+
12+
type hugoTomlTableOfContents struct {
13+
Endlevel *int `toml:"endLevel"`
14+
Ordered *bool `toml:"ordered"`
15+
StartLevel *int `toml:"startLevel"`
16+
}
17+
18+
type hugoTomlGoldmark struct {
19+
Renderer *hugoTomlGoldmarkRenderer `toml:"renderer"`
20+
Parser *hugoTomlGoldmarkParser `toml:"parser"`
21+
}
22+
23+
type hugoTomlGoldmarkRenderer struct {
24+
Unsafe *bool `toml:"unsafe"`
25+
}
26+
27+
type hugoTomlGoldmarkParser struct {
28+
Attribute *hugoTomlGoldmarkParserAttribute `toml:"attribute"`
29+
}
30+
31+
type hugoTomlGoldmarkParserAttribute struct {
32+
Block *bool `toml:"block"`
33+
Title *bool `toml:"title"`
34+
}

tooling/go/cmd/site-consistency/main.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ import (
88
"log"
99
"os"
1010
"path/filepath"
11+
"reflect"
1112
"sort"
13+
14+
"github.com/BurntSushi/toml"
1215
)
1316

1417
func main() {
1518
var rootDirectory string
19+
var fix bool
1620
flag.StringVar(&rootDirectory, "root-dir", ".", "Root directory to search for hugo.toml files in")
21+
flag.BoolVar(&fix, "fix", false, "Fix problems, rather than reporting them")
1722
flag.Parse()
1823

1924
var directoriesToCheck []string
@@ -35,10 +40,14 @@ func main() {
3540
var errors []string
3641
for _, directoryToCheck := range directoriesToCheck {
3742
for _, expectedSymlink := range []string{"config", "deploy-netlify.sh", "netlify.toml"} {
38-
if errorToReport, ok := checkForParentSymlink(directoryToCheck, expectedSymlink); !ok {
43+
if errorToReport, ok := checkForParentSymlink(directoryToCheck, expectedSymlink, fix); !ok {
3944
errors = append(errors, errorToReport)
4045
}
4146
}
47+
48+
if errorToReport := checkForMarkupConfig(filepath.Join(directoryToCheck, "hugo.toml"), fix); errorToReport != nil {
49+
errors = append(errors, errorToReport.Error())
50+
}
4251
}
4352

4453
for _, errorToReport := range errors {
@@ -51,13 +60,19 @@ func main() {
5160

5261
// checkForParentSymlink checks that a symlink exists in the directory with expectedLinkName pointing at something in the parent directory with the same name.
5362
// It returns a bool indicating whether things were correct, and if the bool is false, returns a non-empty string describing the problem suitable for displaying to a user.
54-
func checkForParentSymlink(directoryToCheck, expectedLinkName string) (string, bool) {
63+
func checkForParentSymlink(directoryToCheck, expectedLinkName string, fix bool) (string, bool) {
5564
expectedDestination := filepath.Join("..", "tooling", "common-config", expectedLinkName)
5665
path := filepath.Join(directoryToCheck, expectedLinkName)
5766
fileInfo, err := os.Lstat(path)
5867
if err != nil {
5968
reason := fmt.Sprintf("an error occurred looking it up: %v", err)
6069
if errors.Is(err, fs.ErrNotExist) {
70+
if fix {
71+
if err := os.Symlink(expectedDestination, path); err != nil {
72+
panic(fmt.Sprintf("Failed to create symlink %s: %v", path, err))
73+
}
74+
return "", true
75+
}
6176
reason = "it didn't exist"
6277
}
6378
return formatError(path, expectedDestination, reason), false
@@ -78,3 +93,58 @@ func checkForParentSymlink(directoryToCheck, expectedLinkName string) (string, b
7893
func formatError(path, expectedDestination, reason string) string {
7994
return fmt.Sprintf("Expected %s to be a symlink pointing at %s but %s", path, expectedDestination, reason)
8095
}
96+
97+
func checkForMarkupConfig(path string, fix bool) error {
98+
var config hugoToml
99+
bytes, err := os.ReadFile(path)
100+
if err != nil {
101+
return fmt.Errorf("failed to read %s: %w", path, err)
102+
}
103+
if err := toml.Unmarshal(bytes, &config); err != nil {
104+
return fmt.Errorf("failed to decode %s as toml: %w", path, err)
105+
}
106+
107+
want := &hugoTomlMarkup{
108+
TableOfContents: &hugoTomlTableOfContents{
109+
Endlevel: ptr(2),
110+
Ordered: ptr(true),
111+
StartLevel: ptr(2),
112+
},
113+
Goldmark: &hugoTomlGoldmark{
114+
Renderer: &hugoTomlGoldmarkRenderer{
115+
Unsafe: ptr(true),
116+
},
117+
Parser: &hugoTomlGoldmarkParser{
118+
Attribute: &hugoTomlGoldmarkParserAttribute{
119+
Block: ptr(true),
120+
Title: ptr(true),
121+
},
122+
},
123+
},
124+
}
125+
if !reflect.DeepEqual(config.Markup, want) {
126+
wantConfig := hugoToml{
127+
Markup: want,
128+
}
129+
marshalledWant, err := toml.Marshal(wantConfig)
130+
if err != nil {
131+
panic(fmt.Sprintf("failed to marshal known-good toml: %v", err))
132+
}
133+
if fix && config.Markup == nil {
134+
var out []byte
135+
out = append(out, bytes...)
136+
out = append(out, '\n', '\n')
137+
out = append(out, marshalledWant...)
138+
if err := os.WriteFile(path, out, 0644); err != nil {
139+
panic(fmt.Sprintf("failed to fix %s: %v", path, err))
140+
}
141+
return nil
142+
}
143+
return fmt.Errorf("%s had wrong or missing [markup] section. Add this:\n%s", path, string(marshalledWant))
144+
}
145+
return nil
146+
}
147+
148+
func ptr[T any](v T) *T {
149+
return &v
150+
}

tooling/go/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module github.com/CodeYourFuture/curriculum/tooling/go
33
go 1.21.5
44

55
require (
6+
github.com/BurntSushi/toml v1.4.0
7+
github.com/adrg/frontmatter v0.2.0
68
github.com/stretchr/testify v1.8.4
79
golang.org/x/mod v0.14.0
810
)
911

1012
require (
11-
github.com/BurntSushi/toml v0.3.1 // indirect
12-
github.com/adrg/frontmatter v0.2.0 // indirect
1313
github.com/davecgh/go-spew v1.1.1 // indirect
1414
github.com/pmezard/go-difflib v1.0.0 // indirect
1515
gopkg.in/yaml.v2 v2.3.0 // indirect

tooling/go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
4+
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
35
github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
46
github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
57
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

0 commit comments

Comments
 (0)