Skip to content

Commit a4c0feb

Browse files
committed
Add code to update .yamllint files
Signed-off-by: Thomas Stromberg <[email protected]>
1 parent 4994b77 commit a4c0feb

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

lint-install.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/hexops/gotextdiff"
1414
"github.com/hexops/gotextdiff/myers"
15+
"github.com/hexops/gotextdiff/span"
1516
"github.com/karrick/godirwalk"
1617
"k8s.io/klog/v2"
1718
)
@@ -27,6 +28,9 @@ var (
2728
//go:embed .golangci.yml
2829
goLintConfig []byte
2930

31+
//go:embed .yamllint
32+
yamlLintConfig []byte
33+
3034
//go:embed Makefile.tmpl
3135
makeTmpl string
3236
)
@@ -144,9 +148,9 @@ func updateMakefile(root string, cfg Config, dryRun bool) (string, error) {
144148
return fmt.Sprint(change), nil
145149
}
146150

147-
// updateGoLint updates the golangci-lint configuration for a project.
148-
func updateGoLint(root string, dryRun bool) (string, error) {
149-
dest := filepath.Join(root, ".golangci.yml")
151+
// updateFile updates a configuration file within a project
152+
func updateFile(root string, basename string, content []byte, dryRun bool) (string, error) {
153+
dest := filepath.Join(root, basename)
150154
var existing []byte
151155
var err error
152156

@@ -158,12 +162,12 @@ func updateGoLint(root string, dryRun bool) (string, error) {
158162
}
159163
}
160164

161-
proposed := string(goLintConfig)
162-
edits := myers.ComputeEdits(".golangci.yml", string(existing), proposed)
163-
change := gotextdiff.ToUnified(filepath.Base(dest), filepath.Base(dest), string(existing), edits)
165+
proposed := string(content)
166+
edits := myers.ComputeEdits(span.URI(basename), string(existing), proposed)
167+
change := gotextdiff.ToUnified(basename, basename, string(existing), edits)
164168

165169
if !dryRun {
166-
if err := os.WriteFile(dest, goLintConfig, 0o600); err != nil {
170+
if err := os.WriteFile(dest, content, 0o600); err != nil {
167171
return "", err
168172
}
169173
}
@@ -255,18 +259,6 @@ func main() {
255259
continue
256260
}
257261

258-
if needs[Go] {
259-
diff, err := updateGoLint(root, *dryRunFlag)
260-
if err != nil {
261-
klog.Exitf("update go lint config failed: %v", err)
262-
}
263-
if diff != "" {
264-
klog.Infof("go lint config changes:\n%s", diff)
265-
} else {
266-
klog.Infof("go lint config has no changes")
267-
}
268-
}
269-
270262
cfg := Config{
271263
Args: strings.Join(os.Args[1:], " "),
272264
Makefile: *makeFileName,
@@ -276,6 +268,16 @@ func main() {
276268
cfg.Go = *goFlag
277269
cfg.LintCommands = append(cfg.LintCommands, goLintCmd(root, cfg.Go, false))
278270
cfg.FixCommands = append(cfg.FixCommands, goLintCmd(root, cfg.Go, true))
271+
272+
diff, err := updateFile(root, ".golangci.yaml", goLintConfig, *dryRunFlag)
273+
if err != nil {
274+
klog.Exitf("update go lint config failed: %v", err)
275+
}
276+
if diff != "" {
277+
klog.Infof("go lint config changes:\n%s", diff)
278+
} else {
279+
klog.Infof("go lint config has no changes")
280+
}
279281
}
280282
if needs[Dockerfile] {
281283
cfg.Dockerfile = *dockerfileFlag
@@ -289,6 +291,16 @@ func main() {
289291
if needs[YAML] {
290292
cfg.YAML = *yamlFlag
291293
cfg.LintCommands = append(cfg.LintCommands, yamlLintCmd(root, cfg.Shell))
294+
295+
diff, err := updateFile(root, ".yamllint", yamlLintConfig, *dryRunFlag)
296+
if err != nil {
297+
klog.Exitf("update yaml lint config failed: %v", err)
298+
}
299+
if diff != "" {
300+
klog.Infof("yamllint config changes:\n%s", diff)
301+
} else {
302+
klog.Infof("yamllint config has no changes")
303+
}
292304
}
293305

294306
diff, err := updateMakefile(root, cfg, *dryRunFlag)

0 commit comments

Comments
 (0)