Merge pull request #12225 from fgouteroux/feat/promtool_check_rules_stdin

promtool: read from stdin if no filenames are provided in check rules
This commit is contained in:
Julien Pivotto 2023-06-27 13:22:00 +02:00 committed by GitHub
commit 1214d314c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 35 deletions

View file

@ -127,8 +127,8 @@ func main() {
checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.") checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.")
ruleFiles := checkRulesCmd.Arg( ruleFiles := checkRulesCmd.Arg(
"rule-files", "rule-files",
"The rule files to check.", "The rule files to check, default is read from standard input.",
).Required().ExistingFiles() ).ExistingFiles()
checkRulesLint := checkRulesCmd.Flag( checkRulesLint := checkRulesCmd.Flag(
"lint", "lint",
"Linting checks to apply. Available options are: "+strings.Join(lintOptions, ", ")+". Use --lint=none to disable linting", "Linting checks to apply. Available options are: "+strings.Join(lintOptions, ", ")+". Use --lint=none to disable linting",
@ -457,20 +457,12 @@ func CheckConfig(agentMode, checkSyntaxOnly bool, lintSettings lintConfig, files
} }
fmt.Println() fmt.Println()
for _, rf := range ruleFiles { rulesFailed, rulesHasErrors := checkRules(ruleFiles, lintSettings)
if n, errs := checkRules(rf, lintSettings); len(errs) > 0 { if rulesFailed {
fmt.Fprintln(os.Stderr, " FAILED:") failed = rulesFailed
for _, err := range errs { }
fmt.Fprintln(os.Stderr, " ", err) if rulesHasErrors {
} hasErrors = rulesHasErrors
failed = true
for _, err := range errs {
hasErrors = hasErrors || !errors.Is(err, lintError)
}
} else {
fmt.Printf(" SUCCESS: %d rules found\n", n)
}
fmt.Println()
} }
} }
if failed && hasErrors { if failed && hasErrors {
@ -698,9 +690,57 @@ func checkSDFile(filename string) ([]*targetgroup.Group, error) {
func CheckRules(ls lintConfig, files ...string) int { func CheckRules(ls lintConfig, files ...string) int {
failed := false failed := false
hasErrors := false hasErrors := false
if len(files) == 0 {
fmt.Println("Checking standard input")
data, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, " FAILED:", err)
return failureExitCode
}
rgs, errs := rulefmt.Parse(data)
for _, e := range errs {
fmt.Fprintln(os.Stderr, e.Error())
return failureExitCode
}
if n, errs := checkRuleGroups(rgs, ls); errs != nil {
fmt.Fprintln(os.Stderr, " FAILED:")
for _, e := range errs {
fmt.Fprintln(os.Stderr, e.Error())
}
failed = true
for _, err := range errs {
hasErrors = hasErrors || !errors.Is(err, lintError)
}
} else {
fmt.Printf(" SUCCESS: %d rules found\n", n)
}
fmt.Println()
} else {
failed, hasErrors = checkRules(files, ls)
}
if failed && hasErrors {
return failureExitCode
}
if failed && ls.fatal {
return lintErrExitCode
}
return successExitCode
}
// checkRules validates rule files.
func checkRules(files []string, ls lintConfig) (bool, bool) {
failed := false
hasErrors := false
for _, f := range files { for _, f := range files {
if n, errs := checkRules(f, ls); errs != nil { fmt.Println("Checking", f)
rgs, errs := rulefmt.ParseFile(f)
if errs != nil {
failed = true
continue
}
if n, errs := checkRuleGroups(rgs, ls); errs != nil {
fmt.Fprintln(os.Stderr, " FAILED:") fmt.Fprintln(os.Stderr, " FAILED:")
for _, e := range errs { for _, e := range errs {
fmt.Fprintln(os.Stderr, e.Error()) fmt.Fprintln(os.Stderr, e.Error())
@ -714,23 +754,10 @@ func CheckRules(ls lintConfig, files ...string) int {
} }
fmt.Println() fmt.Println()
} }
if failed && hasErrors { return failed, hasErrors
return failureExitCode
}
if failed && ls.fatal {
return lintErrExitCode
}
return successExitCode
} }
func checkRules(filename string, lintSettings lintConfig) (int, []error) { func checkRuleGroups(rgs *rulefmt.RuleGroups, lintSettings lintConfig) (int, []error) {
fmt.Println("Checking", filename)
rgs, errs := rulefmt.ParseFile(filename)
if errs != nil {
return successExitCode, errs
}
numRules := 0 numRules := 0
for _, rg := range rgs.Groups { for _, rg := range rgs.Groups {
numRules += len(rg.Rules) numRules += len(rg.Rules)

View file

@ -181,9 +181,9 @@ Check if the rule files are valid or not.
###### Arguments ###### Arguments
| Argument | Description | Required | | Argument | Description |
| --- | --- | --- | | --- | --- |
| rule-files | The rule files to check. | Yes | | rule-files | The rule files to check, default is read from standard input. |