Skip to content

Commit

Permalink
refactor: create
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba committed Sep 17, 2024
1 parent 3c8dcb8 commit bd99df4
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ import (
"regexp"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"oras.land/oras-go/v2/registry"

"github.com/zarf-dev/zarf/src/cmd/common"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/internal/packager2"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager"
"github.com/zarf-dev/zarf/src/pkg/packager/filters"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
"github.com/zarf-dev/zarf/src/types"

"oras.land/oras-go/v2/registry"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/packager"
)

var packageCmd = &cobra.Command{
Expand All @@ -55,16 +54,9 @@ var packageCreateCmd = &cobra.Command{
}

v := common.GetViper()
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err
}
defer pkgClient.ClearTempPaths()
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

err = pkgClient.Create(cmd.Context())
err := packager2.Create(cmd.Context(), pkgConfig.CreateOpts)
var lintErr *lint.LintError
if errors.As(err, &lintErr) {
common.PrintFindings(lintErr)
Expand Down
114 changes: 114 additions & 0 deletions src/internal/packager2/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

package packager2

import (
"context"
"fmt"
"os"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/pterm/pterm"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/creator"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"
)

// Create generates a Zarf package tarball for a given PackageConfig and optional base directory.
func Create(ctx context.Context, createOpts types.ZarfCreateOptions) error {
dir, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return err
}
defer os.RemoveAll(dir)
packagePaths := layout.New(dir)

cwd, err := os.Getwd()
if err != nil {
return err
}
err = os.Chdir(createOpts.BaseDir)
if err != nil {
return fmt.Errorf("unable to access directory %q: %w", createOpts.BaseDir, err)
}

pc := creator.NewPackageCreator(createOpts, cwd)
err = helpers.CreatePathAndCopy(layout.ZarfYAML, packagePaths.ZarfYAML)
if err != nil {
return err
}
pkg, warnings, err := pc.LoadPackageDefinition(ctx, packagePaths)
if err != nil {
return err
}

if !confirmAction(config.ZarfCreateStage, warnings, pkg) {
return fmt.Errorf("package creation canceled")
}

err = pc.Assemble(ctx, packagePaths, pkg.Components, pkg.Metadata.Architecture)
if err != nil {
return err
}

// cd back for output
if err := os.Chdir(cwd); err != nil {
return err
}

return pc.Output(ctx, packagePaths, &pkg)
}

func confirmAction(stage string, warnings []string, pkg v1alpha1.ZarfPackage) bool {
pterm.Println()
message.HeaderInfof("📦 PACKAGE DEFINITION")
utils.ColorPrintYAML(pkg, getPackageYAMLHints(), true)

if len(warnings) > 0 {
message.HorizontalRule()
message.Title("Package Warnings", "the following warnings were flagged while reading the package")
for _, warning := range warnings {
message.Warn(warning)
}
}

message.HorizontalRule()

// Display prompt if not auto-confirmed
if config.CommonOptions.Confirm {
pterm.Println()
message.Successf("%s Zarf package confirmed", stage)
return config.CommonOptions.Confirm
}

prompt := &survey.Confirm{
Message: stage + " this Zarf package?",
}

pterm.Println()

// Prompt the user for confirmation, on abort return false
var confirm bool
if err := survey.AskOne(prompt, &confirm); err != nil || !confirm {
// User aborted or declined, cancel the action
return false
}

return true
}

func getPackageYAMLHints() map[string]string {
hints := map[string]string{}
hints = utils.AddRootHint(hints, "metadata", "information about this package\n")
hints = utils.AddRootHint(hints, "build", "info about the machine, zarf version, and user that created this package\n")
hints = utils.AddRootHint(hints, "components", "components selected for this operation")
hints = utils.AddRootHint(hints, "constants", "static values set by the package author")
hints = utils.AddRootHint(hints, "variables", "deployment-specific values that are set on each package deployment")
return hints
}

0 comments on commit bd99df4

Please sign in to comment.