Skip to content

Commit

Permalink
refactor: lint (#3000)
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba authored Sep 17, 2024
1 parent 06255b2 commit 3c8dcb8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
8 changes: 1 addition & 7 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 21 additions & 18 deletions src/pkg/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions src/pkg/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
Expand Down
3 changes: 0 additions & 3 deletions src/pkg/lint/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 3c8dcb8

Please sign in to comment.