Skip to content

Commit

Permalink
change where print happens
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinAbro321 committed Jun 26, 2024
1 parent d09a3bf commit 7f25dd3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func Table(header []string, data [][]string) {
// preventing future characters from taking on the given color
// returns string as normal if color is disabled
func ColorWrap(str string, attr color.Attribute) string {
if config.NoColor {
if config.NoColor || str == "" {

Check warning on line 330 in src/pkg/message/message.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/message/message.go#L330

Added line #L330 was not covered by tests
return str
}
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", attr, str)
Expand Down
43 changes: 41 additions & 2 deletions src/pkg/packager/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/defenseunicorns/pkg/helpers/v2"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/packager/filters"
"github.com/defenseunicorns/zarf/src/pkg/packager/lint"
"github.com/defenseunicorns/zarf/src/types"
"github.com/fatih/color"
)

// DevDeploy creates + deploys a package in one shot
Expand Down Expand Up @@ -126,11 +128,48 @@ func (p *Packager) Lint(ctx context.Context) error {
return nil

Check warning on line 128 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L126-L128

Added lines #L126 - L128 were not covered by tests
}

lint.PrintFindings(findings, types.SevWarn, p.cfg.CreateOpts.BaseDir, p.cfg.Pkg.Metadata.Name)
mapOfFindingsByPath := lint.GroupFindingsByPath(findings, types.SevWarn, p.cfg.Pkg.Metadata.Name)

Check warning on line 131 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L131

Added line #L131 was not covered by tests

if lint.HasErrors(findings) {
header := []string{"Type", "Path", "Message"}

Check warning on line 133 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L133

Added line #L133 was not covered by tests

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 142 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L135-L142

Added lines #L135 - L142 were not covered by tests
}
var packagePathFromUser string
if helpers.IsOCIURL(findings[0].PackagePathOverride) {
packagePathFromUser = findings[0].PackagePathOverride
} else {
packagePathFromUser = filepath.Join(p.cfg.CreateOpts.BaseDir, findings[0].PackagePathOverride)

Check warning on line 148 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L144-L148

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

Check warning on line 151 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L150-L151

Added lines #L150 - L151 were not covered by tests
}

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

Check warning on line 155 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L154-L155

Added lines #L154 - L155 were not covered by tests
}

return nil

Check warning on line 158 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L158

Added line #L158 was not covered by tests
}

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

Check warning on line 163 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L161-L163

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

Check warning on line 165 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L165

Added line #L165 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 172 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L168-L172

Added lines #L168 - L172 were not covered by tests
}
return "unknown"

Check warning on line 174 in src/pkg/packager/dev.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/dev.go#L174

Added line #L174 was not covered by tests
}
41 changes: 41 additions & 0 deletions src/pkg/packager/lint/findings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package lint contains functions for verifying zarf yaml files are valid
package lint

import (
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/zarf/src/types"
)

// GroupFindingsByPath groups findings by their package path
func GroupFindingsByPath(findings []types.PackageFinding, severity types.Severity, packageName string) map[string][]types.PackageFinding {
findings = helpers.RemoveMatches(findings, func(finding types.PackageFinding) bool {
return finding.Severity > severity
})
for i := range findings {
if findings[i].PackageNameOverride == "" {
findings[i].PackageNameOverride = packageName
}
if findings[i].PackagePathOverride == "" {
findings[i].PackagePathOverride = "."
}
}

mapOfFindingsByPath := make(map[string][]types.PackageFinding)
for _, finding := range findings {
mapOfFindingsByPath[finding.PackagePathOverride] = append(mapOfFindingsByPath[finding.PackagePathOverride], finding)
}
return mapOfFindingsByPath
}

// HasSeverity returns true if the findings contain a severity equal to or greater than the given severity
func HasSeverity(findings []types.PackageFinding, severity types.Severity) bool {
for _, finding := range findings {
if finding.Severity <= severity {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestGroupFindingsByPath(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
require.Equal(t, tt.want, groupFindingsByPath(tt.findings, tt.severity, tt.packageName))
require.Equal(t, tt.want, GroupFindingsByPath(tt.findings, tt.severity, tt.packageName))
})
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestHasSeverity(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
require.Equal(t, tt.expected, hasSeverity(tt.findings, tt.severity))
require.Equal(t, tt.expected, HasSeverity(tt.findings, tt.severity))
})
}
}
104 changes: 0 additions & 104 deletions src/pkg/packager/lint/validator.go

This file was deleted.

0 comments on commit 7f25dd3

Please sign in to comment.