-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linting): Add support for linting decK files
- Loading branch information
Showing
4 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"strings" | ||
|
||
"github.com/daveshanley/vacuum/motor" | ||
"github.com/daveshanley/vacuum/rulesets" | ||
"github.com/kong/go-apiops/filebasics" | ||
"github.com/kong/go-apiops/logbasics" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
cmdLintInputFilename string | ||
cmdLintInputRuleset string | ||
) | ||
|
||
func executeLint(cmd *cobra.Command, _ []string) error { | ||
|
||
severityList := map[string]int{ | ||
"hint": 0, | ||
"info": 1, | ||
"warn": 2, | ||
"error": 3, | ||
} | ||
|
||
verbosity, _ := cmd.Flags().GetInt("verbose") | ||
logbasics.Initialize(log.LstdFlags, verbosity) | ||
|
||
inputFilename, err := cmd.Flags().GetString("state") | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed getting cli argument 'state'; %w", err) | ||
} | ||
|
||
minimumSeverity, err := cmd.Flags().GetString("fail-severity") | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed getting cli argument 'fail-severity'; %w", err) | ||
} | ||
|
||
onlyFailures, err := cmd.Flags().GetBool("display-only-failures") | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed getting cli argument 'display-only-failures'; %w", err) | ||
} | ||
|
||
var outputFormat string | ||
{ | ||
outputFormat, err = cmd.Flags().GetString("format") | ||
if err != nil { | ||
return fmt.Errorf("failed getting cli argument 'format'; %w", err) | ||
} | ||
outputFormat = strings.ToUpper(outputFormat) | ||
} | ||
|
||
inputRuleset, err := cmd.Flags().GetString("ruleset") | ||
|
||
if err != nil || inputRuleset == "" { | ||
return fmt.Errorf("failed getting cli argument 'ruleset';") | ||
} | ||
|
||
ruleSetBytes, err := ioutil.ReadFile(inputRuleset) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
customRuleSet, rsErr := rulesets.CreateRuleSetFromData(ruleSetBytes) | ||
|
||
if rsErr != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
specBytes, err := ioutil.ReadFile(inputFilename) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
ruleSetResults := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{ | ||
RuleSet: customRuleSet, | ||
Spec: specBytes, | ||
SkipDocumentCheck: true, | ||
}) | ||
|
||
failingCount := 0 | ||
totalCount := 0 | ||
|
||
var lintResults []map[string]interface{} | ||
for _, x := range ruleSetResults.Results { | ||
|
||
if onlyFailures && (x.Rule.Severity != "error") { | ||
continue | ||
} | ||
|
||
if severityList[x.Rule.Severity] >= severityList[minimumSeverity] { | ||
failingCount++ | ||
} | ||
|
||
totalCount++ | ||
|
||
lintResults = append(lintResults, map[string]interface{}{ | ||
"message": x.Message, | ||
"path": x.Rule.Given, | ||
"line": x.StartNode.Line, | ||
"column": x.StartNode.Column, | ||
"severity": x.Rule.Severity, | ||
}) | ||
} | ||
|
||
lintErrs := map[string]interface{}{ | ||
"total_count": totalCount, | ||
"fail_count": failingCount, | ||
"results": lintResults, | ||
} | ||
|
||
if outputFormat == "DEFAULT" { | ||
if totalCount > 0 { | ||
fmt.Printf("Linting Violations: %d\n", totalCount) | ||
fmt.Printf("Failures: %d\n\n", failingCount) | ||
for _, violation := range lintErrs["results"].([]map[string]interface{}) { | ||
fmt.Printf("[%d:%d] %s\n", violation["line"], violation["column"], violation["message"]) | ||
} | ||
} | ||
} else { | ||
filebasics.WriteSerializedFile("-", lintErrs, filebasics.OutputFormat(outputFormat)) | ||
} | ||
|
||
if failingCount > 0 { | ||
// We don't want to print the error here as they're already output above | ||
// But we _do_ want to set an exit code of failure | ||
cmd.SilenceErrors = true | ||
return errors.New("linting errors detected") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// | ||
// | ||
// Define the CLI data for the lint command | ||
// | ||
// | ||
|
||
func newLintCmd() *cobra.Command { | ||
lintCmd := &cobra.Command{ | ||
Use: "lint [flags] lint-file", | ||
Short: "Short description here", | ||
Long: `Long description Here`, | ||
RunE: executeLint, | ||
} | ||
|
||
lintCmd.Flags().StringVarP(&cmdLintInputFilename, "state", "s", "-", | ||
"decK file to process. Use - to read from stdin.") | ||
lintCmd.Flags().StringVarP(&cmdLintInputRuleset, "ruleset", "r", "", | ||
"Ruleset to apply to the state file.") | ||
lintCmd.Flags().StringP("format", "", "default", "output format: default, "+ | ||
string(filebasics.OutputFormatJSON)+" or "+string(filebasics.OutputFormatYaml)) | ||
lintCmd.Flags().StringP("fail-severity", "", "warn", "minimum severity to fail on") | ||
lintCmd.Flags().BoolP("display-only-failures", "D", false, "only display failures") | ||
|
||
return lintCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.