forked from toolshed/abra
		
	
		
			
				
	
	
		
			107 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package recipe
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	"coopcloud.tech/abra/cli/internal"
 | 
						|
	"coopcloud.tech/abra/pkg/autocomplete"
 | 
						|
	"coopcloud.tech/abra/pkg/formatter"
 | 
						|
	"coopcloud.tech/abra/pkg/lint"
 | 
						|
	recipePkg "coopcloud.tech/abra/pkg/recipe"
 | 
						|
	"coopcloud.tech/abra/pkg/runtime"
 | 
						|
	"github.com/sirupsen/logrus"
 | 
						|
	"github.com/urfave/cli"
 | 
						|
)
 | 
						|
 | 
						|
var recipeLintCommand = cli.Command{
 | 
						|
	Name:      "lint",
 | 
						|
	Usage:     "Lint a recipe",
 | 
						|
	Aliases:   []string{"l"},
 | 
						|
	ArgsUsage: "<recipe>",
 | 
						|
	Flags: []cli.Flag{
 | 
						|
		internal.DebugFlag,
 | 
						|
		internal.OnlyErrorFlag,
 | 
						|
		internal.OfflineFlag,
 | 
						|
	},
 | 
						|
	Before:       internal.SubCommandBefore,
 | 
						|
	BashComplete: autocomplete.RecipeNameComplete,
 | 
						|
	Action: func(c *cli.Context) error {
 | 
						|
		conf := runtime.New(runtime.WithOffline(internal.Offline))
 | 
						|
		recipe := internal.ValidateRecipe(c, conf)
 | 
						|
 | 
						|
		if err := recipePkg.EnsureUpToDate(recipe.Name, conf); err != nil {
 | 
						|
			logrus.Fatal(err)
 | 
						|
		}
 | 
						|
 | 
						|
		tableCol := []string{"ref", "rule", "severity", "satisfied", "skipped", "resolve"}
 | 
						|
		table := formatter.CreateTable(tableCol)
 | 
						|
 | 
						|
		hasError := false
 | 
						|
		bar := formatter.CreateProgressbar(-1, "running recipe lint rules...")
 | 
						|
		for level := range lint.LintRules {
 | 
						|
			for _, rule := range lint.LintRules[level] {
 | 
						|
				if internal.OnlyErrors && rule.Level != "error" {
 | 
						|
					logrus.Debugf("skipping %s, does not have level \"error\"", rule.Ref)
 | 
						|
					continue
 | 
						|
				}
 | 
						|
 | 
						|
				skipped := false
 | 
						|
				if rule.Skip(recipe) {
 | 
						|
					skipped = true
 | 
						|
				}
 | 
						|
 | 
						|
				skippedOutput := "-"
 | 
						|
				if skipped {
 | 
						|
					skippedOutput = "yes"
 | 
						|
				}
 | 
						|
 | 
						|
				satisfied := false
 | 
						|
				if !skipped {
 | 
						|
					ok, err := rule.Function(recipe)
 | 
						|
					if err != nil {
 | 
						|
						logrus.Warn(err)
 | 
						|
					}
 | 
						|
 | 
						|
					if !ok && rule.Level == "error" {
 | 
						|
						hasError = true
 | 
						|
					}
 | 
						|
 | 
						|
					if ok {
 | 
						|
						satisfied = true
 | 
						|
					}
 | 
						|
				}
 | 
						|
 | 
						|
				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)
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		if table.NumLines() > 0 {
 | 
						|
			fmt.Println()
 | 
						|
			table.Render()
 | 
						|
		}
 | 
						|
 | 
						|
		if hasError {
 | 
						|
			logrus.Warn("watch out, some critical errors are present in your recipe config")
 | 
						|
		}
 | 
						|
 | 
						|
		return nil
 | 
						|
	},
 | 
						|
}
 |