Skip to content

Commit

Permalink
revert to prompt on init
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Nov 4, 2024
1 parent 2088d4e commit 42fb448
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
37 changes: 30 additions & 7 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package cmd

import (
"context"
"errors"
"fmt"
"path"
"path/filepath"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/pkg/oci"
"github.com/zarf-dev/zarf/src/cmd/common"
Expand Down Expand Up @@ -112,6 +114,10 @@ func findInitPackage(ctx context.Context, initPackageName string) (string, error
return filepath.Join(absCachePath, initPackageName), nil
}

if config.CommonOptions.Confirm {
return "", lang.ErrInitNotFound
}

// Finally, if the init-package doesn't exist in the cache directory, suggest downloading it
downloadCacheTarget, err := downloadInitPackage(ctx, absCachePath)
if err != nil {
Expand All @@ -123,14 +129,31 @@ func findInitPackage(ctx context.Context, initPackageName string) (string, error
func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, error) {
l := logger.From(ctx)
url := zoci.GetInitPackageURL(config.CLIVersion)
message.Infof("init package was not found locally. Downloading to cache %s", cacheDirectory)
l.Info("init package was not found locally. Downloading to cache", "cache", cacheDirectory)
remote, err := zoci.NewRemote(ctx, url, oci.PlatformForArch(config.GetArch()))
if err != nil {
return "", err

// Give the user the choice to download the init-package and note that this does require an internet connection
message.Question(fmt.Sprintf(lang.CmdInitPullAsk, url))
message.Note(lang.CmdInitPullNote)
l.Info("the init package was not found locally, but can be pulled from", "url", fmt.Sprintf("oci://%s", url))

var confirmDownload bool
prompt := &survey.Confirm{
Message: lang.CmdInitPullConfirm,
}
if err := survey.AskOne(prompt, &confirmDownload); err != nil {
return "", fmt.Errorf("confirm download canceled: %w", err)
}

// If the user wants to download the init-package, download it
if confirmDownload {
remote, err := zoci.NewRemote(ctx, url, oci.PlatformForArch(config.GetArch()))
if err != nil {
return "", err
}
source := &sources.OCISource{Remote: remote}
return source.Collect(ctx, cacheDirectory)
}
source := &sources.OCISource{Remote: remote}
return source.Collect(ctx, cacheDirectory)
// Otherwise, exit and tell the user to manually download the init-package
return "", errors.New(lang.CmdInitPullErrManual)
}

func validateInitFlags() error {
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@ func setupMessage(cfg messageCfg) error {
message.InitializePTerm(io.Discard)
// Disable all progress bars and spinners
message.NoProgress = true
// Ensures no user input is needed while we maintain backwards compatibility with message
config.CommonOptions.Confirm = true
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ $ zarf init --artifact-push-password={PASSWORD} --artifact-push-username={USERNA
CmdInitErrValidateRegistry = "the 'registry-push-username' and 'registry-push-password' flags must be provided if the 'registry-url' flag is provided"
CmdInitErrValidateArtifact = "the 'artifact-push-username' and 'artifact-push-token' flags must be provided if the 'artifact-url' flag is provided"

CmdInitPullAsk = "It seems the init package could not be found locally, but can be pulled from oci://%s"
CmdInitPullNote = "Note: This will require an internet connection."
CmdInitPullConfirm = "Do you want to pull this init package?"
CmdInitPullErrManual = "pull the init package manually and place it in the current working directory"

CmdInitFlagSet = "Specify deployment variables to set on the command line (KEY=value)"

CmdInitFlagConfirm = "Confirms package deployment without prompting. ONLY use with packages you trust. Skips prompts to review SBOM, configure variables, select optional components and review potential breaking changes."
Expand Down Expand Up @@ -614,6 +619,7 @@ const (

// Collection of reusable error messages.
var (
ErrInitNotFound = errors.New("this command requires a zarf-init package, but one was not found on the local system. Re-run the last command again without '--confirm' to download the package")
ErrUnableToCheckArch = errors.New("unable to get the configured cluster's architecture")
ErrUnableToGetPackages = errors.New("unable to load the Zarf Package data from the cluster")
)
Expand Down

0 comments on commit 42fb448

Please sign in to comment.