@@ -9,11 +9,15 @@ import (
9
9
"os"
10
10
"path/filepath"
11
11
"sort"
12
+
13
+ "github.com/BurntSushi/toml"
12
14
)
13
15
14
16
func main () {
15
17
var rootDirectory string
18
+ var fix bool
16
19
flag .StringVar (& rootDirectory , "root-dir" , "." , "Root directory to search for hugo.toml files in" )
20
+ flag .BoolVar (& fix , "fix" , false , "Fix problems, rather than reporting them" )
17
21
flag .Parse ()
18
22
19
23
var directoriesToCheck []string
@@ -35,10 +39,14 @@ func main() {
35
39
var errors []string
36
40
for _ , directoryToCheck := range directoriesToCheck {
37
41
for _ , expectedSymlink := range []string {"config" , "deploy-netlify.sh" , "netlify.toml" } {
38
- if errorToReport , ok := checkForParentSymlink (directoryToCheck , expectedSymlink ); ! ok {
42
+ if errorToReport , ok := checkForParentSymlink (directoryToCheck , expectedSymlink , fix ); ! ok {
39
43
errors = append (errors , errorToReport )
40
44
}
41
45
}
46
+
47
+ if errorToReport := checkForMarkupConfig (filepath .Join (directoryToCheck , "hugo.toml" ), fix ); errorToReport != nil {
48
+ errors = append (errors , errorToReport .Error ())
49
+ }
42
50
}
43
51
44
52
for _ , errorToReport := range errors {
@@ -51,13 +59,19 @@ func main() {
51
59
52
60
// checkForParentSymlink checks that a symlink exists in the directory with expectedLinkName pointing at something in the parent directory with the same name.
53
61
// 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 ) {
62
+ func checkForParentSymlink (directoryToCheck , expectedLinkName string , fix bool ) (string , bool ) {
55
63
expectedDestination := filepath .Join (".." , "tooling" , "common-config" , expectedLinkName )
56
64
path := filepath .Join (directoryToCheck , expectedLinkName )
57
65
fileInfo , err := os .Lstat (path )
58
66
if err != nil {
59
67
reason := fmt .Sprintf ("an error occurred looking it up: %v" , err )
60
68
if errors .Is (err , fs .ErrNotExist ) {
69
+ if fix {
70
+ if err := os .Symlink (expectedDestination , path ); err != nil {
71
+ panic (fmt .Sprintf ("Failed to create symlink %s: %v" , path , err ))
72
+ }
73
+ return "" , true
74
+ }
61
75
reason = "it didn't exist"
62
76
}
63
77
return formatError (path , expectedDestination , reason ), false
@@ -78,3 +92,58 @@ func checkForParentSymlink(directoryToCheck, expectedLinkName string) (string, b
78
92
func formatError (path , expectedDestination , reason string ) string {
79
93
return fmt .Sprintf ("Expected %s to be a symlink pointing at %s but %s" , path , expectedDestination , reason )
80
94
}
95
+
96
+ func checkForMarkupConfig (path string , fix bool ) error {
97
+ var config hugoToml
98
+ bytes , err := os .ReadFile (path )
99
+ if err != nil {
100
+ return fmt .Errorf ("failed to read %s: %w" , path , err )
101
+ }
102
+ if err := toml .Unmarshal (bytes , & config ); err != nil {
103
+ return fmt .Errorf ("failed to decode %s as toml: %w" , path , err )
104
+ }
105
+
106
+ want := & hugoTomlMarkup {
107
+ TableOfContents : & hugoTomlTableOfContents {
108
+ Endlevel : ptr (2 ),
109
+ Ordered : ptr (true ),
110
+ StartLevel : ptr (2 ),
111
+ },
112
+ Goldmark : & hugoTomlGoldmark {
113
+ Renderer : & hugoTomlGoldmarkRenderer {
114
+ Unsafe : ptr (true ),
115
+ },
116
+ Parser : & hugoTomlGoldmarkParser {
117
+ Attribute : & hugoTomlGoldmarkParserAttribute {
118
+ Block : ptr (true ),
119
+ Title : ptr (true ),
120
+ },
121
+ },
122
+ },
123
+ }
124
+ if config .Markup != want {
125
+ wantConfig := hugoToml {
126
+ Markup : want ,
127
+ }
128
+ marshalledWant , err := toml .Marshal (wantConfig )
129
+ if err != nil {
130
+ panic (fmt .Sprintf ("failed to marshal known-good toml: %v" , err ))
131
+ }
132
+ if fix && config .Markup == nil {
133
+ var out []byte
134
+ out = append (out , bytes ... )
135
+ out = append (out , '\n' , '\n' )
136
+ out = append (out , marshalledWant ... )
137
+ if err := os .WriteFile (path , out , 0644 ); err != nil {
138
+ panic (fmt .Sprintf ("failed to fix %s: %v" , path , err ))
139
+ }
140
+ return nil
141
+ }
142
+ return fmt .Errorf ("%s had wrong or missing [markup] section. Add this:\n %s" , path , string (marshalledWant ))
143
+ }
144
+ return nil
145
+ }
146
+
147
+ func ptr [T any ](v T ) * T {
148
+ return & v
149
+ }
0 commit comments