github.com/Jeffail/benthos/v3@v3.65.0/lib/config/lint.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/Jeffail/benthos/v3/internal/docs"
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  // Lint attempts to report errors within a user config. Returns a slice of lint
    12  // results.
    13  //
    14  // TODO: V4 remove this
    15  func Lint(rawBytes []byte, _ Type) ([]string, error) {
    16  	return LintV2(docs.NewLintContext(), rawBytes)
    17  }
    18  
    19  // LintV2 attempts to report errors within a user config. Returns a slice of
    20  // lint results.
    21  func LintV2(ctx docs.LintContext, rawBytes []byte) ([]string, error) {
    22  	if bytes.HasPrefix(rawBytes, []byte("# BENTHOS LINT DISABLE")) {
    23  		return nil, nil
    24  	}
    25  
    26  	var rawNode yaml.Node
    27  	if err := yaml.Unmarshal(rawBytes, &rawNode); err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	var lintStrs []string
    32  	for _, lint := range Spec().LintYAML(ctx, &rawNode) {
    33  		if lint.Level == docs.LintError {
    34  			lintStrs = append(lintStrs, fmt.Sprintf("line %v: %v", lint.Line, lint.What))
    35  		}
    36  	}
    37  	return lintStrs, nil
    38  }