diff --git a/src/cmd/common/table.go b/src/cmd/common/table.go index ee5f9ee190..2bb7f48f9f 100644 --- a/src/cmd/common/table.go +++ b/src/cmd/common/table.go @@ -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) diff --git a/src/cmd/dev.go b/src/cmd/dev.go index e8bffd57c0..d17e923fc4 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -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) { @@ -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 diff --git a/src/pkg/packager/prepare.go b/src/pkg/packager/prepare.go index 2a25239189..3fe79f8815 100644 --- a/src/pkg/packager/prepare.go +++ b/src/pkg/packager/prepare.go @@ -8,7 +8,6 @@ import ( "context" "errors" "fmt" - "github.com/zarf-dev/zarf/src/pkg/logger" "os" "path/filepath" "regexp" @@ -16,6 +15,8 @@ import ( "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" @@ -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 { @@ -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 { @@ -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" @@ -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...) } } @@ -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) - } } } } @@ -364,8 +349,6 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) return nil, nil } - fmt.Println(componentDefinition) - return imagesMap, nil }