diff --git a/src/cmd/dev.go b/src/cmd/dev.go index 7077f5dccd..85f08e9c11 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -286,13 +286,7 @@ var devLintCmd = &cobra.Command{ pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) - if err != nil { - return err - } - defer pkgClient.ClearTempPaths() - - err = lint.Validate(cmd.Context(), pkgConfig.CreateOpts) + err := lint.Validate(cmd.Context(), pkgConfig.CreateOpts.BaseDir, pkgConfig.CreateOpts.Flavor, pkgConfig.CreateOpts.SetVariables) var lintErr *lint.LintError if errors.As(err, &lintErr) { common.PrintFindings(lintErr) diff --git a/src/pkg/lint/lint.go b/src/pkg/lint/lint.go index 344f3b9db0..dfffccc2f4 100644 --- a/src/pkg/lint/lint.go +++ b/src/pkg/lint/lint.go @@ -9,13 +9,14 @@ import ( "fmt" "os" + goyaml "github.com/goccy/go-yaml" + "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/pkg/layout" "github.com/zarf-dev/zarf/src/pkg/packager/composer" "github.com/zarf-dev/zarf/src/pkg/utils" - "github.com/zarf-dev/zarf/src/types" ) // LintError represents an error containing lint findings. @@ -42,22 +43,28 @@ func (e *LintError) OnlyWarnings() bool { } // Validate lints the given Zarf package -func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) error { - var findings []PackageFinding - if err := os.Chdir(createOpts.BaseDir); err != nil { - return fmt.Errorf("unable to access directory %q: %w", createOpts.BaseDir, err) +func Validate(ctx context.Context, baseDir, flavor string, setVariables map[string]string) error { + err := os.Chdir(baseDir) + if err != nil { + return fmt.Errorf("unable to access directory %q: %w", baseDir, err) + } + b, err := os.ReadFile(layout.ZarfYAML) + if err != nil { + return err } var pkg v1alpha1.ZarfPackage - if err := utils.ReadYaml(layout.ZarfYAML, &pkg); err != nil { + err = goyaml.Unmarshal(b, &pkg) + if err != nil { return err } - compFindings, err := lintComponents(ctx, pkg, createOpts) + findings := []PackageFinding{} + compFindings, err := lintComponents(ctx, pkg, flavor, setVariables) if err != nil { return err } findings = append(findings, compFindings...) - schemaFindings, err := ValidatePackageSchema(createOpts.SetVariables) + schemaFindings, err := ValidatePackageSchema(setVariables) if err != nil { return err } @@ -66,31 +73,27 @@ func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) error { return nil } return &LintError{ - BaseDir: createOpts.BaseDir, + BaseDir: baseDir, PackageName: pkg.Metadata.Name, Findings: findings, } } -func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts types.ZarfCreateOptions) ([]PackageFinding, error) { - var findings []PackageFinding - +func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, flavor string, setVariables map[string]string) ([]PackageFinding, error) { + findings := []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 } - node := chain.Head() for node != nil { component := node.ZarfComponent - compFindings, err := templateZarfObj(&component, createOpts.SetVariables) + compFindings, err := templateZarfObj(&component, setVariables) if err != nil { return nil, err } diff --git a/src/pkg/lint/lint_test.go b/src/pkg/lint/lint_test.go index 84ea5e4cd2..e2e5493734 100644 --- a/src/pkg/lint/lint_test.go +++ b/src/pkg/lint/lint_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/require" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config/lang" - "github.com/zarf-dev/zarf/src/types" ) func TestLintError(t *testing.T) { @@ -54,8 +53,7 @@ func TestLintComponents(t *testing.T) { Metadata: v1alpha1.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) }) } diff --git a/src/pkg/lint/schema.go b/src/pkg/lint/schema.go index adf41e935b..b6cb5f6e3e 100644 --- a/src/pkg/lint/schema.go +++ b/src/pkg/lint/schema.go @@ -23,17 +23,14 @@ func ValidatePackageSchema(setVariables map[string]string) ([]PackageFinding, er if err := utils.ReadYaml(layout.ZarfYAML, &untypedZarfPackage); err != nil { return nil, err } - jsonSchema, err := ZarfSchema.ReadFile("zarf.schema.json") if err != nil { return nil, err } - _, err = templateZarfObj(&untypedZarfPackage, setVariables) if err != nil { return nil, err } - return getSchemaFindings(jsonSchema, untypedZarfPackage) }