Skip to content

Commit

Permalink
Add CLI flag for verbose linting (#146)
Browse files Browse the repository at this point in the history
Fixes #145.

Signed-off-by: Mihai Todor <[email protected]>
  • Loading branch information
mihaitodor authored Jan 13, 2025
1 parent effbd1e commit 7b284f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
- `bloblang` scalar type added to template fields. (@mihaitodor)
- Go API: Method `SetOutputBrokerPattern` added to the `StreamBuilder` type. (@mihaitodor)
- New `error_source_name`, `error_source_label` and `error_source_path` bloblang functions. (@mihaitodor)
- Flag `--verbose` added to the `benthos lint` and `benthos template lint` commands. (@mihaitodor)

### Changed

Expand Down
29 changes: 27 additions & 2 deletions internal/cli/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func lintCliCommand(cliOpts *common.CLIOpts) *cli.Command {
Value: false,
Usage: "Do not produce lint errors when environment interpolations exist without defaults within configs but aren't defined.",
},
&cli.BoolFlag{
Name: "verbose",
Value: false,
Usage: "Print the lint result for each target file.",
},

// General config flags
&cli.StringSliceFlag{
Expand Down Expand Up @@ -75,6 +80,7 @@ files with the .yaml or .yml extension.`)[1:],
var (
red = color.New(color.FgRed).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
)

type pathLint struct {
Expand Down Expand Up @@ -204,11 +210,17 @@ func LintAction(c *cli.Context, opts *common.CLIOpts, stderr io.Writer) error {
lConf.RejectDeprecated = c.Bool("deprecated")
lConf.RequireLabels = c.Bool("labels")
skipEnvVarCheck := c.Bool("skip-env-var-check")
verbose := c.Bool("verbose")

spec := opts.MainConfigSpecCtor()

var pathLintMut sync.Mutex
var pathLints []pathLint
type result struct {
target string
ok bool
}
var lintResults []result
threads := runtime.NumCPU()
var wg sync.WaitGroup
wg.Add(threads)
Expand All @@ -228,16 +240,29 @@ func LintAction(c *cli.Context, opts *common.CLIOpts, stderr io.Writer) error {
} else {
lints = lintFile(opts, target, skipEnvVarCheck, spec, lConf)
}
pathLintMut.Lock()
if len(lints) > 0 {
pathLintMut.Lock()
pathLints = append(pathLints, lints...)
pathLintMut.Unlock()
lintResults = append(lintResults, result{target, false})
} else {
lintResults = append(lintResults, result{target, true})
}
pathLintMut.Unlock()
}
}(i)
}
wg.Wait()

if verbose {
for _, res := range lintResults {
if res.ok {
fmt.Fprintf(opts.Stdout, "%v: %v\n", res.target, green("OK"))
} else {
fmt.Fprintf(opts.Stdout, "%v: %v\n", res.target, red("FAILED"))
}
}
}

if len(pathLints) == 0 {
return nil
}
Expand Down
29 changes: 28 additions & 1 deletion internal/cli/template/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ import (
)

func lintCliCommand(opts *common.CLIOpts) *cli.Command {
flags := []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Value: false,
Usage: "Print the lint result for each target file.",
},
}
flags = append(flags, common.EnvFileAndTemplateFlags(opts, false)...)

return &cli.Command{
Name: "lint",
Flags: common.EnvFileAndTemplateFlags(opts, false),
Flags: flags,
Usage: opts.ExecTemplate("Parse {{.ProductName}} templates and report any linting errors"),
Description: opts.ExecTemplate(`
Exits with a status code 1 if any linting errors are detected:
Expand All @@ -41,6 +50,11 @@ files with the .yaml or .yml extension.`)[1:],
if err != nil {
return fmt.Errorf("lint paths error: %w", err)
}
type result struct {
target string
ok bool
}
var lintResults []result
var pathLints []pathLint
for _, target := range targets {
if target == "" {
Expand All @@ -49,6 +63,18 @@ files with the .yaml or .yml extension.`)[1:],
lints := lintFile(target)
if len(lints) > 0 {
pathLints = append(pathLints, lints...)
lintResults = append(lintResults, result{target, false})
} else {
lintResults = append(lintResults, result{target, true})
}
}
if c.Bool("verbose") {
for _, res := range lintResults {
if res.ok {
fmt.Fprintf(opts.Stdout, "%v: %v\n", res.target, green("OK"))
} else {
fmt.Fprintf(opts.Stdout, "%v: %v\n", res.target, red("FAILED"))
}
}
}
if len(pathLints) == 0 {
Expand All @@ -70,6 +96,7 @@ files with the .yaml or .yml extension.`)[1:],
var (
red = color.New(color.FgRed).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
)

type pathLint struct {
Expand Down

0 comments on commit 7b284f6

Please sign in to comment.