From 42fb4488f828e45bac1cba70b18fc2759b665f64 Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Mon, 4 Nov 2024 19:05:57 +0000 Subject: [PATCH] revert to prompt on init Signed-off-by: Austin Abro --- src/cmd/initialize.go | 37 ++++++++++++++++++++++++++++++------- src/cmd/root.go | 2 -- src/config/lang/english.go | 6 ++++++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/cmd/initialize.go b/src/cmd/initialize.go index 0b78037983..c73d61eb60 100644 --- a/src/cmd/initialize.go +++ b/src/cmd/initialize.go @@ -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" @@ -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 { @@ -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 { diff --git a/src/cmd/root.go b/src/cmd/root.go index 63513c7104..86f01eb1cc 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -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 } diff --git a/src/config/lang/english.go b/src/config/lang/english.go index acb11fbc23..38717db929 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -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." @@ -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") )