Skip to content

Commit

Permalink
Go code cleanup
Browse files Browse the repository at this point in the history
- Use short form `if err := ...; err != nil {` statements where applicable.
- Extract some of the larger raw strings out of tests and into testdata

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Sep 27, 2024
1 parent 75db465 commit 7673aaf
Show file tree
Hide file tree
Showing 44 changed files with 536 additions and 814 deletions.
18 changes: 6 additions & 12 deletions cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ The linter rules with automatic fixes available are currently:
},

RunE: wrapProfiling(func(args []string) error {
err := fix(args, params)
if err != nil {
if err := fix(args, params); err != nil {
log.SetOutput(os.Stderr)
log.Println(err)

Expand Down Expand Up @@ -339,8 +338,7 @@ func fix(args []string, params *fixCommandParams) error {
}

if fixReport.HasConflicts() {
err = r.Report(fixReport)
if err != nil {
if err = r.Report(fixReport); err != nil {
return fmt.Errorf("failed to output fix report: %w", err)
}

Expand Down Expand Up @@ -425,8 +423,7 @@ please run fix from a clean state to support the use of git checkout for undo`,
}

for _, dir := range dirs {
err := os.Remove(dir)
if err != nil {
if err := os.Remove(dir); err != nil {
return fmt.Errorf("failed to delete directory %s: %w", dir, err)
}
}
Expand All @@ -445,20 +442,17 @@ please run fix from a clean state to support the use of git checkout for undo`,
fileMode = fileInfo.Mode()
}

err = os.MkdirAll(filepath.Dir(file), 0o755)
if err != nil {
if err = os.MkdirAll(filepath.Dir(file), 0o755); err != nil {
return fmt.Errorf("failed to create directory for file %s: %w", file, err)
}

err = os.WriteFile(file, fc, fileMode)
if err != nil {
if err = os.WriteFile(file, fc, fileMode); err != nil {
return fmt.Errorf("failed to write file %s: %w", file, err)
}
}
}

err = r.Report(fixReport)
if err != nil {
if err = r.Report(fixReport); err != nil {
return fmt.Errorf("failed to output fix report: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,7 @@ func formatError(format string, err error) error {

buf := &bytes.Buffer{}

err := testSuites.WriteXML(buf)
if err != nil {
if err := testSuites.WriteXML(buf); err != nil {
return fmt.Errorf("failed to format errors for output: %w", err)
}

Expand Down
18 changes: 6 additions & 12 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ func addToDataYAML(params newRuleCommandParams) error {
yamlEncoder := yaml.NewEncoder(&b)
yamlEncoder.SetIndent(2)

err = yamlEncoder.Encode(existingConfig)
if err != nil {
if err = yamlEncoder.Encode(existingConfig); err != nil {
return err
}

Expand Down Expand Up @@ -241,8 +240,7 @@ func scaffoldCustomRule(params newRuleCommandParams) error {
return err
}

err = ruleTmpl.Execute(ruleFile, templateValues(params))
if err != nil {
if err = ruleTmpl.Execute(ruleFile, templateValues(params)); err != nil {
return err
}

Expand All @@ -258,8 +256,7 @@ func scaffoldCustomRule(params newRuleCommandParams) error {
return err
}

err = testTmpl.Execute(testFile, templateValues(params))
if err != nil {
if err = testTmpl.Execute(testFile, templateValues(params)); err != nil {
return err
}

Expand Down Expand Up @@ -287,8 +284,7 @@ func scaffoldBuiltinRule(params newRuleCommandParams) error {
return err
}

err = ruleTmpl.Execute(ruleFile, templateValues(params))
if err != nil {
if err = ruleTmpl.Execute(ruleFile, templateValues(params)); err != nil {
return err
}

Expand All @@ -304,8 +300,7 @@ func scaffoldBuiltinRule(params newRuleCommandParams) error {
return err
}

err = testTmpl.Execute(testFile, templateValues(params))
if err != nil {
if err = testTmpl.Execute(testFile, templateValues(params)); err != nil {
return err
}

Expand Down Expand Up @@ -335,8 +330,7 @@ func createBuiltinDocs(params newRuleCommandParams) error {
return err
}

err = docTmpl.Execute(docFile, templateValues(params))
if err != nil {
if err = docTmpl.Execute(docFile, templateValues(params)); err != nil {
return err
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func init() {
case formatJSON:
e := encoding.JSON().NewEncoder(os.Stdout)
e.SetIndent("", " ")
err := e.Encode(vi)
if err != nil {
if err := e.Encode(vi); err != nil {
log.SetOutput(os.Stderr)
log.Println(err)
os.Exit(1)
Expand Down
Loading

0 comments on commit 7673aaf

Please sign in to comment.