From 6519be7c6dd91b268d8f61c03aa3454116807b68 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Wed, 3 Jul 2024 16:08:10 +0200 Subject: [PATCH] refactor: move lint logic from pacakger to command --- src/cmd/dev.go | 62 +++++++++++++++++++++++--- src/pkg/packager/dev.go | 71 ------------------------------ src/pkg/packager/lint/lint.go | 19 ++++---- src/pkg/packager/lint/lint_test.go | 8 ++-- 4 files changed, 69 insertions(+), 91 deletions(-) diff --git a/src/cmd/dev.go b/src/cmd/dev.go index f6fad80cbe..9b04071462 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -17,11 +17,14 @@ import ( "github.com/defenseunicorns/zarf/src/cmd/common" "github.com/defenseunicorns/zarf/src/config" "github.com/defenseunicorns/zarf/src/config/lang" + "github.com/defenseunicorns/zarf/src/pkg/layout" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/packager" + "github.com/defenseunicorns/zarf/src/pkg/packager/lint" "github.com/defenseunicorns/zarf/src/pkg/transform" "github.com/defenseunicorns/zarf/src/pkg/utils" "github.com/defenseunicorns/zarf/src/types" + "github.com/fatih/color" "github.com/mholt/archiver/v3" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -264,21 +267,68 @@ var devLintCmd = &cobra.Command{ Short: lang.CmdDevLintShort, Long: lang.CmdDevLintLong, RunE: func(cmd *cobra.Command, args []string) error { - pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args) + baseDir := common.SetBaseDirectory(args) v := common.GetViper() - pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap( - v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) + setVariables := helpers.TransformAndMergeMap(v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + var pkg types.ZarfPackage + err := utils.ReadYaml(filepath.Join(baseDir, layout.ZarfYAML), &pkg) if err != nil { return err } - defer pkgClient.ClearTempPaths() + findings, err := lint.Validate(cmd.Context(), pkg, setVariables, pkgConfig.CreateOpts.Flavor) + if err != nil { + return fmt.Errorf("linting failed: %w", err) + } + if len(findings) == 0 { + message.Successf("0 findings for %q", pkg.Metadata.Name) + return nil + } + mapOfFindingsByPath := lint.GroupFindingsByPath(findings, types.SevWarn, pkg.Metadata.Name) + + header := []string{"Type", "Path", "Message"} + for _, findings := range mapOfFindingsByPath { + lintData := [][]string{} + for _, finding := range findings { + lintData = append(lintData, []string{ + colorWrapSev(finding.Severity), + message.ColorWrap(finding.YqPath, color.FgCyan), + itemizedDescription(finding.Description, finding.Item), + }) + } + var packagePathFromUser string + if helpers.IsOCIURL(findings[0].PackagePathOverride) { + packagePathFromUser = findings[0].PackagePathOverride + } else { + packagePathFromUser = filepath.Join(baseDir, findings[0].PackagePathOverride) + } + message.Notef("Linting package %q at %s", findings[0].PackageNameOverride, packagePathFromUser) + message.Table(header, lintData) + } - return pkgClient.Lint(cmd.Context()) + if lint.HasSeverity(findings, types.SevErr) { + return errors.New("errors during lint") + } + return nil }, } +func itemizedDescription(description string, item string) string { + if item == "" { + return description + } + return fmt.Sprintf("%s - %s", description, item) +} + +func colorWrapSev(s types.Severity) string { + if s == types.SevErr { + return message.ColorWrap("Error", color.FgRed) + } else if s == types.SevWarn { + return message.ColorWrap("Warning", color.FgYellow) + } + return "unknown" +} + func init() { v := common.GetViper() rootCmd.AddCommand(devCmd) diff --git a/src/pkg/packager/dev.go b/src/pkg/packager/dev.go index 138d175109..4877e2b768 100644 --- a/src/pkg/packager/dev.go +++ b/src/pkg/packager/dev.go @@ -6,10 +6,8 @@ package packager import ( "context" - "errors" "fmt" "os" - "path/filepath" "runtime" "github.com/defenseunicorns/pkg/helpers/v2" @@ -18,10 +16,7 @@ import ( "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/packager/creator" "github.com/defenseunicorns/zarf/src/pkg/packager/filters" - "github.com/defenseunicorns/zarf/src/pkg/packager/lint" - "github.com/defenseunicorns/zarf/src/pkg/utils" "github.com/defenseunicorns/zarf/src/types" - "github.com/fatih/color" ) // DevDeploy creates + deploys a package in one shot @@ -110,69 +105,3 @@ func (p *Packager) DevDeploy(ctx context.Context) error { // cd back return os.Chdir(cwd) } - -// Lint ensures a package is valid & follows suggested conventions -func (p *Packager) Lint(ctx context.Context) error { - if err := os.Chdir(p.cfg.CreateOpts.BaseDir); err != nil { - return fmt.Errorf("unable to access directory %q: %w", p.cfg.CreateOpts.BaseDir, err) - } - - if err := utils.ReadYaml(layout.ZarfYAML, &p.cfg.Pkg); err != nil { - return err - } - - findings, err := lint.Validate(ctx, p.cfg.Pkg, p.cfg.CreateOpts) - if err != nil { - return fmt.Errorf("linting failed: %w", err) - } - - if len(findings) == 0 { - message.Successf("0 findings for %q", p.cfg.Pkg.Metadata.Name) - return nil - } - - mapOfFindingsByPath := lint.GroupFindingsByPath(findings, types.SevWarn, p.cfg.Pkg.Metadata.Name) - - header := []string{"Type", "Path", "Message"} - - for _, findings := range mapOfFindingsByPath { - lintData := [][]string{} - for _, finding := range findings { - lintData = append(lintData, []string{ - colorWrapSev(finding.Severity), - message.ColorWrap(finding.YqPath, color.FgCyan), - itemizedDescription(finding.Description, finding.Item), - }) - } - var packagePathFromUser string - if helpers.IsOCIURL(findings[0].PackagePathOverride) { - packagePathFromUser = findings[0].PackagePathOverride - } else { - packagePathFromUser = filepath.Join(p.cfg.CreateOpts.BaseDir, findings[0].PackagePathOverride) - } - message.Notef("Linting package %q at %s", findings[0].PackageNameOverride, packagePathFromUser) - message.Table(header, lintData) - } - - if lint.HasSeverity(findings, types.SevErr) { - return errors.New("errors during lint") - } - - return nil -} - -func itemizedDescription(description string, item string) string { - if item == "" { - return description - } - return fmt.Sprintf("%s - %s", description, item) -} - -func colorWrapSev(s types.Severity) string { - if s == types.SevErr { - return message.ColorWrap("Error", color.FgRed) - } else if s == types.SevWarn { - return message.ColorWrap("Warning", color.FgYellow) - } - return "unknown" -} diff --git a/src/pkg/packager/lint/lint.go b/src/pkg/packager/lint/lint.go index ddcdea76db..8b80a22ae4 100644 --- a/src/pkg/packager/lint/lint.go +++ b/src/pkg/packager/lint/lint.go @@ -27,9 +27,9 @@ import ( var ZarfSchema fs.ReadFileFS // Validate the given Zarf package. The Zarf package should not already be composed when sent to this function. -func Validate(ctx context.Context, pkg types.ZarfPackage, createOpts types.ZarfCreateOptions) ([]types.PackageFinding, error) { +func Validate(ctx context.Context, pkg types.ZarfPackage, setVariables map[string]string, flavor string) ([]types.PackageFinding, error) { var findings []types.PackageFinding - compFindings, err := lintComponents(ctx, pkg, createOpts) + compFindings, err := lintComponents(ctx, pkg, setVariables, flavor) if err != nil { return nil, err } @@ -54,17 +54,16 @@ func Validate(ctx context.Context, pkg types.ZarfPackage, createOpts types.ZarfC return findings, nil } -func lintComponents(ctx context.Context, pkg types.ZarfPackage, createOpts types.ZarfCreateOptions) ([]types.PackageFinding, error) { +func lintComponents(ctx context.Context, pkg types.ZarfPackage, setVariables map[string]string, flavor string) ([]types.PackageFinding, error) { var findings []types.PackageFinding for i, component := range pkg.Components { arch := config.GetArch(pkg.Metadata.Architecture) - if !composer.CompatibleComponent(component, arch, createOpts.Flavor) { + if !composer.CompatibleComponent(component, arch, flavor) { continue } - chain, err := composer.NewImportChain(ctx, component, i, pkg.Metadata.Name, arch, createOpts.Flavor) - + chain, err := composer.NewImportChain(ctx, component, i, pkg.Metadata.Name, arch, flavor) if err != nil { return nil, err } @@ -72,7 +71,7 @@ func lintComponents(ctx context.Context, pkg types.ZarfPackage, createOpts types node := chain.Head() for node != nil { component := node.ZarfComponent - compFindings := fillComponentTemplate(&component, &createOpts) + compFindings := fillComponentTemplate(&component, setVariables) compFindings = append(compFindings, checkComponent(component, node.Index())...) for i := range compFindings { compFindings[i].PackagePathOverride = node.ImportLocation() @@ -85,7 +84,7 @@ func lintComponents(ctx context.Context, pkg types.ZarfPackage, createOpts types return findings, nil } -func fillComponentTemplate(c *types.ZarfComponent, createOpts *types.ZarfCreateOptions) []types.PackageFinding { +func fillComponentTemplate(c *types.ZarfComponent, setVariables map[string]string) []types.PackageFinding { var findings []types.PackageFinding err := creator.ReloadComponentTemplate(c) if err != nil { @@ -112,7 +111,7 @@ func fillComponentTemplate(c *types.ZarfComponent, createOpts *types.ZarfCreateO Severity: types.SevWarn, }) } - _, present := createOpts.SetVariables[key] + _, present := setVariables[key] if !present { findings = append(findings, types.PackageFinding{ Description: lang.UnsetVarLintWarning, @@ -120,7 +119,7 @@ func fillComponentTemplate(c *types.ZarfComponent, createOpts *types.ZarfCreateO }) } } - for key, value := range createOpts.SetVariables { + for key, value := range setVariables { templateMap[fmt.Sprintf("%s%s###", templatePrefix, key)] = value } } diff --git a/src/pkg/packager/lint/lint_test.go b/src/pkg/packager/lint/lint_test.go index 78d4c4102d..82e717b3a1 100644 --- a/src/pkg/packager/lint/lint_test.go +++ b/src/pkg/packager/lint/lint_test.go @@ -11,10 +11,11 @@ import ( "os" "testing" - "github.com/defenseunicorns/zarf/src/pkg/variables" - "github.com/defenseunicorns/zarf/src/types" goyaml "github.com/goccy/go-yaml" "github.com/stretchr/testify/require" + + "github.com/defenseunicorns/zarf/src/pkg/variables" + "github.com/defenseunicorns/zarf/src/types" ) func TestZarfSchema(t *testing.T) { @@ -296,8 +297,7 @@ func TestValidateComponent(t *testing.T) { Metadata: types.ZarfMetadata{Name: "test-zarf-package"}, } - createOpts := types.ZarfCreateOptions{Flavor: "", BaseDir: "."} - _, err := lintComponents(context.Background(), zarfPackage, createOpts) + _, err := lintComponents(context.Background(), zarfPackage, nil, "") require.Error(t, err) })