promtool: read from stdin if no filenames are provided in check rules

Signed-off-by: François Gouteroux <francois.gouteroux@gmail.com>
This commit is contained in:
François Gouteroux 2023-04-04 13:28:02 +02:00
parent e709b0b36e
commit 034eb2b3f2
2 changed files with 19 additions and 6 deletions

View File

@ -131,8 +131,8 @@ func main() {
checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.")
ruleFiles := checkRulesCmd.Arg(
"rule-files",
"The rule files to check.",
).Required().ExistingFiles()
"The rule files to check, default is read from standard input (STDIN).",
).Default("-").Strings()
checkRulesLint := checkRulesCmd.Flag(
"lint",
"Linting checks to apply. Available options are: "+strings.Join(lintOptions, ", ")+". Use --lint=none to disable linting",
@ -715,9 +715,22 @@ func CheckRules(ls lintConfig, files ...string) int {
}
func checkRules(filename string, lintSettings lintConfig) (int, []error) {
fmt.Println("Checking", filename)
var rgs *rulefmt.RuleGroups
var errs []error
if filename == "-" || filename == "" {
var buf bytes.Buffer
tee := io.TeeReader(os.Stdin, &buf)
if _, err := buf.ReadFrom(tee); err != nil {
errs = append(errs, err)
return failureExitCode, errs
}
fmt.Println("Checking stdin")
rgs, errs = rulefmt.Parse(buf.Bytes())
} else {
fmt.Println("Checking", filename)
rgs, errs = rulefmt.ParseFile(filename)
}
rgs, errs := rulefmt.ParseFile(filename)
if errs != nil {
return successExitCode, errs
}

View File

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