Skip to content

Commit

Permalink
refactor: move lint logic from pacakger to command
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Jul 4, 2024
1 parent 014d1d2 commit 6519be7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 91 deletions.
62 changes: 56 additions & 6 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Check warning on line 270 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L270

Added line #L270 was not covered by tests
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)

Check warning on line 272 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L272

Added line #L272 was not covered by tests

pkgClient, err := packager.New(&pkgConfig)
var pkg types.ZarfPackage
err := utils.ReadYaml(filepath.Join(baseDir, layout.ZarfYAML), &pkg)

Check warning on line 275 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L274-L275

Added lines #L274 - L275 were not covered by tests
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)

Check warning on line 281 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L279-L281

Added lines #L279 - L281 were not covered by tests
}
if len(findings) == 0 {
message.Successf("0 findings for %q", pkg.Metadata.Name)
return nil

Check warning on line 285 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L283-L285

Added lines #L283 - L285 were not covered by tests
}
mapOfFindingsByPath := lint.GroupFindingsByPath(findings, types.SevWarn, pkg.Metadata.Name)

Check warning on line 287 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L287

Added line #L287 was not covered by tests

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),
})

Check warning on line 297 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L289-L297

Added lines #L289 - L297 were not covered by tests
}
var packagePathFromUser string
if helpers.IsOCIURL(findings[0].PackagePathOverride) {
packagePathFromUser = findings[0].PackagePathOverride
} else {
packagePathFromUser = filepath.Join(baseDir, findings[0].PackagePathOverride)

Check warning on line 303 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L299-L303

Added lines #L299 - L303 were not covered by tests
}
message.Notef("Linting package %q at %s", findings[0].PackageNameOverride, packagePathFromUser)
message.Table(header, lintData)

Check warning on line 306 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L305-L306

Added lines #L305 - L306 were not covered by tests
}

return pkgClient.Lint(cmd.Context())
if lint.HasSeverity(findings, types.SevErr) {
return errors.New("errors during lint")

Check warning on line 310 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L309-L310

Added lines #L309 - L310 were not covered by tests
}
return nil

Check warning on line 312 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L312

Added line #L312 was not covered by tests
},
}

func itemizedDescription(description string, item string) string {
if item == "" {
return description

Check warning on line 318 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L316-L318

Added lines #L316 - L318 were not covered by tests
}
return fmt.Sprintf("%s - %s", description, item)

Check warning on line 320 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L320

Added line #L320 was not covered by tests
}

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)

Check warning on line 327 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L323-L327

Added lines #L323 - L327 were not covered by tests
}
return "unknown"

Check warning on line 329 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L329

Added line #L329 was not covered by tests
}

func init() {
v := common.GetViper()
rootCmd.AddCommand(devCmd)
Expand Down
71 changes: 0 additions & 71 deletions src/pkg/packager/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ package packager

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/defenseunicorns/pkg/helpers/v2"
Expand All @@ -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
Expand Down Expand Up @@ -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"
}
19 changes: 9 additions & 10 deletions src/pkg/packager/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 30 in src/pkg/packager/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/lint/lint.go#L30

Added line #L30 was not covered by tests
var findings []types.PackageFinding
compFindings, err := lintComponents(ctx, pkg, createOpts)
compFindings, err := lintComponents(ctx, pkg, setVariables, flavor)

Check warning on line 32 in src/pkg/packager/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/lint/lint.go#L32

Added line #L32 was not covered by tests
if err != nil {
return nil, err
}
Expand All @@ -54,25 +54,24 @@ 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
}

node := chain.Head()
for node != nil {
component := node.ZarfComponent
compFindings := fillComponentTemplate(&component, &createOpts)
compFindings := fillComponentTemplate(&component, setVariables)

Check warning on line 74 in src/pkg/packager/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/lint/lint.go#L74

Added line #L74 was not covered by tests
compFindings = append(compFindings, checkComponent(component, node.Index())...)
for i := range compFindings {
compFindings[i].PackagePathOverride = node.ImportLocation()
Expand All @@ -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 {

Check warning on line 87 in src/pkg/packager/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/lint/lint.go#L87

Added line #L87 was not covered by tests
var findings []types.PackageFinding
err := creator.ReloadComponentTemplate(c)
if err != nil {
Expand All @@ -112,15 +111,15 @@ func fillComponentTemplate(c *types.ZarfComponent, createOpts *types.ZarfCreateO
Severity: types.SevWarn,
})
}
_, present := createOpts.SetVariables[key]
_, present := setVariables[key]

Check warning on line 114 in src/pkg/packager/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/lint/lint.go#L114

Added line #L114 was not covered by tests
if !present {
findings = append(findings, types.PackageFinding{
Description: lang.UnsetVarLintWarning,
Severity: types.SevWarn,
})
}
}
for key, value := range createOpts.SetVariables {
for key, value := range setVariables {

Check warning on line 122 in src/pkg/packager/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/lint/lint.go#L122

Added line #L122 was not covered by tests
templateMap[fmt.Sprintf("%s%s###", templatePrefix, key)] = value
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})

Expand Down

0 comments on commit 6519be7

Please sign in to comment.