@@ -8,12 +8,17 @@ import (
8
8
"log"
9
9
"os"
10
10
"path/filepath"
11
+ "reflect"
11
12
"sort"
13
+
14
+ "github.com/BurntSushi/toml"
12
15
)
13
16
14
17
func main () {
15
18
var rootDirectory string
19
+ var fix bool
16
20
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" )
17
22
flag .Parse ()
18
23
19
24
var directoriesToCheck []string
@@ -35,10 +40,14 @@ func main() {
35
40
var errors []string
36
41
for _ , directoryToCheck := range directoriesToCheck {
37
42
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 {
39
44
errors = append (errors , errorToReport )
40
45
}
41
46
}
47
+
48
+ if errorToReport := checkForMarkupConfig (filepath .Join (directoryToCheck , "hugo.toml" ), fix ); errorToReport != nil {
49
+ errors = append (errors , errorToReport .Error ())
50
+ }
42
51
}
43
52
44
53
for _ , errorToReport := range errors {
@@ -51,13 +60,19 @@ func main() {
51
60
52
61
// checkForParentSymlink checks that a symlink exists in the directory with expectedLinkName pointing at something in the parent directory with the same name.
53
62
// 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 ) {
55
64
expectedDestination := filepath .Join (".." , "tooling" , "common-config" , expectedLinkName )
56
65
path := filepath .Join (directoryToCheck , expectedLinkName )
57
66
fileInfo , err := os .Lstat (path )
58
67
if err != nil {
59
68
reason := fmt .Sprintf ("an error occurred looking it up: %v" , err )
60
69
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
+ }
61
76
reason = "it didn't exist"
62
77
}
63
78
return formatError (path , expectedDestination , reason ), false
@@ -78,3 +93,58 @@ func checkForParentSymlink(directoryToCheck, expectedLinkName string) (string, b
78
93
func formatError (path , expectedDestination , reason string ) string {
79
94
return fmt .Sprintf ("Expected %s to be a symlink pointing at %s but %s" , path , expectedDestination , reason )
80
95
}
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
+ }
0 commit comments