2021-09-05 20:33:07 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-12-11 23:17:39 +00:00
|
|
|
"coopcloud.tech/abra/pkg/autocomplete"
|
2021-12-28 00:24:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
2021-12-25 23:00:19 +00:00
|
|
|
"coopcloud.tech/abra/pkg/lint"
|
2021-12-27 03:07:52 +00:00
|
|
|
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
2021-09-05 20:33:07 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-09-05 20:33:07 +00:00
|
|
|
)
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
var recipeLintCommand = cli.Command{
|
|
|
|
Name: "lint",
|
|
|
|
Usage: "Lint a recipe",
|
|
|
|
Aliases: []string{"l"},
|
|
|
|
ArgsUsage: "<recipe>",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
internal.DebugFlag,
|
|
|
|
internal.OnlyErrorFlag,
|
|
|
|
},
|
|
|
|
Before: internal.SubCommandBefore,
|
2021-12-25 22:11:32 +00:00
|
|
|
BashComplete: autocomplete.RecipeNameComplete,
|
2021-09-05 20:33:07 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2023-02-13 15:40:30 +00:00
|
|
|
recipe := internal.ValidateRecipe(c)
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-12-27 03:07:52 +00:00
|
|
|
if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-22 11:24:09 +00:00
|
|
|
tableCol := []string{"ref", "rule", "severity", "satisfied", "skipped", "resolve"}
|
2021-12-25 22:11:32 +00:00
|
|
|
table := formatter.CreateTable(tableCol)
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-12-25 22:11:32 +00:00
|
|
|
hasError := false
|
|
|
|
bar := formatter.CreateProgressbar(-1, "running recipe lint rules...")
|
2021-12-25 23:00:19 +00:00
|
|
|
for level := range lint.LintRules {
|
|
|
|
for _, rule := range lint.LintRules[level] {
|
2023-01-22 11:24:09 +00:00
|
|
|
if internal.OnlyErrors && rule.Level != "error" {
|
|
|
|
logrus.Debugf("skipping %s, does not have level \"error\"", rule.Ref)
|
|
|
|
continue
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 11:24:09 +00:00
|
|
|
skipped := false
|
|
|
|
if rule.Skip(recipe) {
|
|
|
|
skipped = true
|
2021-12-25 22:11:32 +00:00
|
|
|
}
|
2021-11-06 21:36:01 +00:00
|
|
|
|
2023-01-22 11:24:09 +00:00
|
|
|
skippedOutput := "-"
|
|
|
|
if skipped {
|
|
|
|
skippedOutput = "yes"
|
2021-12-25 22:11:32 +00:00
|
|
|
}
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2023-01-22 11:24:09 +00:00
|
|
|
satisfied := false
|
|
|
|
if !skipped {
|
|
|
|
ok, err := rule.Function(recipe)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
|
2021-12-25 22:11:32 +00:00
|
|
|
if !ok && rule.Level == "error" {
|
2023-01-22 11:24:09 +00:00
|
|
|
hasError = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
satisfied = true
|
2021-12-25 22:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-22 11:24:09 +00:00
|
|
|
|
|
|
|
satisfiedOutput := "yes"
|
|
|
|
if !satisfied {
|
|
|
|
satisfiedOutput = "NO"
|
|
|
|
if skipped {
|
|
|
|
satisfiedOutput = "-"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
table.Append([]string{
|
|
|
|
rule.Ref,
|
|
|
|
rule.Description,
|
|
|
|
rule.Level,
|
|
|
|
satisfiedOutput,
|
|
|
|
skippedOutput,
|
|
|
|
rule.HowToResolve,
|
|
|
|
})
|
|
|
|
|
|
|
|
bar.Add(1)
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
2021-12-25 22:11:32 +00:00
|
|
|
}
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-12-25 22:11:32 +00:00
|
|
|
if table.NumLines() > 0 {
|
|
|
|
fmt.Println()
|
|
|
|
table.Render()
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 22:11:32 +00:00
|
|
|
if hasError {
|
|
|
|
logrus.Warn("watch out, some critical errors are present in your recipe config")
|
|
|
|
}
|
2021-09-05 20:33:07 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|