Skip to content

Commit

Permalink
chore: refactor findImages to remove print statements
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Conlon <software@conlon.dev>
  • Loading branch information
a1994sc committed Nov 13, 2024
1 parent 4c47c7c commit 42bf38a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
21 changes: 21 additions & 0 deletions src/cmd/common/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ import (
"github.com/zarf-dev/zarf/src/pkg/message"
)

const (
COMPONENTS string = "components:"
NAME string = " - name: %s\n"
IMAGES string = " images:"
IMAGE string = " - %s\n"
)

// PrintComponentTable prints the components with found images.
func PrintComponentTable(imagesMap map[string][]string) {
fmt.Println(COMPONENTS)
for component, images := range imagesMap {
fmt.Printf(NAME, component)
if len(images) > 0 {
fmt.Println(IMAGES)
for i := 0; i < len(images); i++ {
fmt.Printf(IMAGE, images[i])
}
}
}
}

// PrintFindings prints the findings in the LintError as a table.
func PrintFindings(lintErr *lint.LintError) {
mapOfFindingsByPath := lint.GroupFindingsByPath(lintErr.Findings, lintErr.PackageName)
Expand Down
6 changes: 4 additions & 2 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ var devFindImagesCmd = &cobra.Command{
}
defer pkgClient.ClearTempPaths()

_, err = pkgClient.FindImages(cmd.Context())
imagesMap, err := pkgClient.FindImages(cmd.Context())

var lintErr *lint.LintError
if errors.As(err, &lintErr) {
Expand All @@ -267,7 +267,9 @@ var devFindImagesCmd = &cobra.Command{
}
common.PrintFindings(lintErr)
}
if err != nil {
if len(imagesMap) > 0 {
common.PrintComponentTable(imagesMap)
} else if err != nil {
return fmt.Errorf("unable to find images: %w", err)
}
return nil
Expand Down
25 changes: 4 additions & 21 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"context"
"errors"
"fmt"
"github.com/zarf-dev/zarf/src/pkg/logger"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"

"github.com/zarf-dev/zarf/src/pkg/logger"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/goccy/go-yaml"
"github.com/google/go-containerregistry/pkg/crane"
Expand Down Expand Up @@ -79,7 +80,6 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error)
return p.findImages(ctx)
}

// TODO: Refactor to return output string instead of printing inside of function.
func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) {
l := logger.From(ctx)
for _, component := range p.cfg.Pkg.Components {
Expand Down Expand Up @@ -117,7 +117,6 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
ArtifactServer: artifactServer,
}

componentDefinition := "\ncomponents:\n"
imagesMap := map[string][]string{}
whyResources := []string{}
for _, component := range p.cfg.Pkg.Components {
Expand Down Expand Up @@ -284,13 +283,7 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
sortedMatchedImages, sortedExpectedImages := getSortedImages(matchedImages, maybeImages)

if len(sortedMatchedImages) > 0 {
// Log the header comment
componentDefinition += fmt.Sprintf("\n - name: %s\n images:\n", component.Name)
for _, image := range sortedMatchedImages {
// Use print because we want this dumped to stdout
imagesMap[component.Name] = append(imagesMap[component.Name], image)
componentDefinition += fmt.Sprintf(" - %s\n", image)
}
imagesMap[component.Name] = append(imagesMap[component.Name], sortedMatchedImages...)
}

// Handle the "maybes"
Expand All @@ -310,11 +303,7 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
}

if len(validImages) > 0 {
componentDefinition += fmt.Sprintf(" # Possible images - %s - %s\n", p.cfg.Pkg.Metadata.Name, component.Name)
for _, image := range validImages {
imagesMap[component.Name] = append(imagesMap[component.Name], image)
componentDefinition += fmt.Sprintf(" - %s\n", image)
}
imagesMap[component.Name] = append(imagesMap[component.Name], validImages...)
}
}

Expand Down Expand Up @@ -348,10 +337,6 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)

if len(cosignArtifactList) > 0 {
imagesMap[component.Name] = append(imagesMap[component.Name], cosignArtifactList...)
componentDefinition += fmt.Sprintf(" # Cosign artifacts for images - %s - %s\n", p.cfg.Pkg.Metadata.Name, component.Name)
for _, cosignArtifact := range cosignArtifactList {
componentDefinition += fmt.Sprintf(" - %s\n", cosignArtifact)
}
}
}
}
Expand All @@ -364,8 +349,6 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
return nil, nil
}

fmt.Println(componentDefinition)

return imagesMap, nil
}

Expand Down

0 comments on commit 42bf38a

Please sign in to comment.