Skip to content

Commit ddbbce2

Browse files
authored
Merge pull request tinkerbell#24 from tstromberg/missing-yaml
Add code to update .yamllint files
2 parents a3f3261 + ccb20cb commit ddbbce2

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
}
@@ -296,18 +300,6 @@ func main() {
296300
continue
297301
}
298302

299-
if needs[Go] {
300-
diff, err := updateGoLint(root, *dryRunFlag)
301-
if err != nil {
302-
klog.Exitf("update go lint config failed: %v", err)
303-
}
304-
if diff != "" {
305-
klog.Infof("go lint config changes:\n%s", diff)
306-
} else {
307-
klog.Infof("go lint config has no changes")
308-
}
309-
}
310-
311303
cfg := Config{
312304
Args: strings.Join(os.Args[1:], " "),
313305
Makefile: *makeFileName,
@@ -317,6 +309,16 @@ func main() {
317309
cfg.Go = *goFlag
318310
cfg.LintCommands = append(cfg.LintCommands, goLintCmd(root, cfg.Go, false))
319311
cfg.FixCommands = append(cfg.FixCommands, goLintCmd(root, cfg.Go, true))
312+
313+
diff, err := updateFile(root, ".golangci.yaml", goLintConfig, *dryRunFlag)
314+
if err != nil {
315+
klog.Exitf("update go lint config failed: %v", err)
316+
}
317+
if diff != "" {
318+
klog.Infof("go lint config changes:\n%s", diff)
319+
} else {
320+
klog.Infof("go lint config has no changes")
321+
}
320322
}
321323
if needs[Dockerfile] {
322324
cfg.Dockerfile = *dockerfileFlag
@@ -330,6 +332,16 @@ func main() {
330332
if needs[YAML] {
331333
cfg.YAML = *yamlFlag
332334
cfg.LintCommands = append(cfg.LintCommands, yamlLintCmd(root, cfg.Shell))
335+
336+
diff, err := updateFile(root, ".yamllint", yamlLintConfig, *dryRunFlag)
337+
if err != nil {
338+
klog.Exitf("update yaml lint config failed: %v", err)
339+
}
340+
if diff != "" {
341+
klog.Infof("yamllint config changes:\n%s", diff)
342+
} else {
343+
klog.Infof("yamllint config has no changes")
344+
}
333345
}
334346

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

0 commit comments

Comments
 (0)