Skip to content

Commit

Permalink
refactor: layout (#3022)
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
  • Loading branch information
phillebaba authored Oct 1, 2024
1 parent f5edaa6 commit 117433c
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 141 deletions.
7 changes: 4 additions & 3 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,15 @@ var packageMirrorCmd = &cobra.Command{
SkipSignatureValidation: pkgConfig.PkgOpts.SkipSignatureValidation,
Filter: filter,
}
pkgPaths, err := packager2.LoadPackage(cmd.Context(), loadOpt)
pkgLayout, err := packager2.LoadPackage(cmd.Context(), loadOpt)
if err != nil {
return err
}
defer os.RemoveAll(pkgPaths.Base)
//nolint: errcheck // ignore
defer pkgLayout.Cleanup()
mirrorOpt := packager2.MirrorOptions{
Cluster: c,
PackagePaths: *pkgPaths,
PkgLayout: pkgLayout,
Filter: filter,
RegistryInfo: pkgConfig.InitOpts.RegistryInfo,
GitInfo: pkgConfig.InitOpts.GitServer,
Expand Down
31 changes: 18 additions & 13 deletions src/internal/packager2/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ package packager2
import (
"context"
"fmt"
"os"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/internal/packager/sbom"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/packager/filters"
"github.com/zarf-dev/zarf/src/pkg/utils"
)

// ZarfInspectOptions tracks the user-defined preferences during a package inspection.
Expand All @@ -35,7 +38,7 @@ func Inspect(ctx context.Context, opt ZarfInspectOptions) (v1alpha1.ZarfPackage,
}

if getSBOM(opt.ViewSBOM, opt.SBOMOutputDir) {
err = handleSBOMOptions(ctx, pkg, opt)
err = handleSBOMOptions(ctx, opt)
if err != nil {
return pkg, err
}
Expand Down Expand Up @@ -75,7 +78,7 @@ func getPackageMetadata(ctx context.Context, opt ZarfInspectOptions) (v1alpha1.Z
return pkg, nil
}

func handleSBOMOptions(ctx context.Context, pkg v1alpha1.ZarfPackage, opt ZarfInspectOptions) error {
func handleSBOMOptions(ctx context.Context, opt ZarfInspectOptions) error {
loadOpt := LoadOptions{
Source: opt.Source,
SkipSignatureValidation: opt.SkipSignatureValidation,
Expand All @@ -86,23 +89,25 @@ func handleSBOMOptions(ctx context.Context, pkg v1alpha1.ZarfPackage, opt ZarfIn
if err != nil {
return err
}
if opt.SBOMOutputDir != "" {
out, err := layout.SBOMs.OutputSBOMFiles(opt.SBOMOutputDir, pkg.Metadata.Name)

sbomDirPath := opt.SBOMOutputDir
if sbomDirPath == "" {
tmpDir, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return err
}
if opt.ViewSBOM {
err := sbom.ViewSBOMFiles(out)
if err != nil {
return err
}
}
} else if opt.ViewSBOM {
err := sbom.ViewSBOMFiles(layout.SBOMs.Path)
defer os.RemoveAll(tmpDir)
sbomDirPath = tmpDir
}
sbomPath, err := layout.GetSBOM(sbomDirPath)
if err != nil {
return err
}
if opt.ViewSBOM {
err := sbom.ViewSBOMFiles(sbomPath)
if err != nil {
return err
}
return err
}
return nil
}
Expand Down
34 changes: 34 additions & 0 deletions src/internal/packager2/layout/layout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package layout contains functions for inteacting the Zarf packages.
package layout

// Constants used in the default package layout.
const (
ZarfYAML = "zarf.yaml"
Signature = "zarf.yaml.sig"
Checksums = "checksums.txt"

ImagesDir = "images"
ComponentsDir = "components"

SBOMDir = "zarf-sbom"
SBOMTar = "sboms.tar"

IndexJSON = "index.json"
OCILayout = "oci-layout"
)

// ComponentDir is the type for the different directories in a component.
type ComponentDir string

// Different component directory types.
const (
RepoComponentDir ComponentDir = "repos"
FilesComponentDir ComponentDir = "files"
ChartsComponentDir ComponentDir = "charts"
ManifestsComponentDir ComponentDir = "manifests"
DataComponentDir ComponentDir = "data"
ValuesComponentDir ComponentDir = "values"
)
Loading

0 comments on commit 117433c

Please sign in to comment.