Skip to content

Commit

Permalink
chore(lint): slight refactor sync cli args with other cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Oct 17, 2023
1 parent f4d1a8c commit 90ead55
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions cmd/file_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package cmd
import (
"errors"
"fmt"
"io"
"log"
"os"
"strings"

"github.com/daveshanley/vacuum/motor"
Expand All @@ -17,7 +15,6 @@ import (

var (
cmdLintInputFilename string
cmdLintInputRuleset string
cmdLintFormat string
cmdLintFailSeverity string
cmdLintOnlyFailures bool
Expand Down Expand Up @@ -59,8 +56,9 @@ func ParseSeverity(s string) Severity {
return SeverityWarn
}

// getRuleSet reads the ruleset file by the provided name and returns a RuleSet object.
func getRuleSet(ruleSetFile string) (*rulesets.RuleSet, error) {
ruleSetBytes, err := os.ReadFile(ruleSetFile)
ruleSetBytes, err := filebasics.ReadFile(ruleSetFile)
if err != nil {
return nil, fmt.Errorf("error reading ruleset file: %w", err)
}
Expand All @@ -71,29 +69,19 @@ func getRuleSet(ruleSetFile string) (*rulesets.RuleSet, error) {
return customRuleSet, nil
}

func executeLint(cmd *cobra.Command, _ []string) error {
// Executes the CLI command "lint"
func executeLint(cmd *cobra.Command, args []string) error {
verbosity, _ := cmd.Flags().GetInt("verbose")
logbasics.Initialize(log.LstdFlags, verbosity)

if cmdLintInputRuleset == "" {
return errors.New("missing required option: --ruleset")
}
customRuleSet, err := getRuleSet(cmdLintInputRuleset)
customRuleSet, err := getRuleSet(args[0])
if err != nil {
return err
}

var stateFileBytes []byte
if cmdLintInputFilename == "-" {
stateFileBytes, err = io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("error reading state from STDIN: %w", err)
}
} else {
stateFileBytes, err = os.ReadFile(cmdLintInputFilename)
if err != nil {
return fmt.Errorf("error reading state file: %w", err)
}
stateFileBytes, err := filebasics.ReadFile(cmdLintInputFilename)
if err != nil {
return fmt.Errorf("failed to read input file '%s'; %w", cmdLintInputFilename, err)
}

ruleSetResults := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
Expand Down Expand Up @@ -177,18 +165,17 @@ func executeLint(cmd *cobra.Command, _ []string) error {

func newLintCmd() *cobra.Command {
lintCmd := &cobra.Command{
Use: "lint",
Use: "lint [flags] ruleset-file",
Short: "Lint a file against a ruleset",
Long: "Validate a decK state file against a linting ruleset, reporting any violations or failures.\n" +
"Report output can be returned in JSON, YAML, or human readable format (see --format).\n" +
"Ruleset Docs: https://quobix.com/vacuum/rulesets/",
RunE: executeLint,
Args: cobra.ExactArgs(1),
}

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().StringVar(
&cmdLintFormat, "format", plainTextFormat,
fmt.Sprintf(`output format [choices: "%s", "%s", "%s"]`,
Expand Down

0 comments on commit 90ead55

Please sign in to comment.