From d58a94597aa9f195be708b627c9b0641dbafe171 Mon Sep 17 00:00:00 2001 From: razzle Date: Fri, 15 Mar 2024 12:45:59 -0500 Subject: [PATCH] add errors Signed-off-by: razzle --- src/config/lang/english.go | 8 -------- src/pkg/interactive/components.go | 9 ++------- src/pkg/packager/filters/deploy.go | 24 +++++++++++++++++------- src/pkg/packager/filters/deploy_test.go | 7 ++++++- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/config/lang/english.go b/src/config/lang/english.go index 479a86d835..99fdab80e3 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -639,14 +639,6 @@ const ( PkgCreateErrDifferentialNoVersion = "unable to create differential package. Please ensure both package versions are set" ) -// Package deploy -const ( - PkgDeployErrMultipleComponentsSameGroup = "cannot specify multiple components (%q, %q) within the same group (%q) when using the --components flag" - PkgDeployErrNoDefaultOrSelection = "no selection made from %q with the --components flag and there is no default in the group" - PkgDeployErrNoCompatibleComponentsForSelection = "no compatible components found that matched %q, suggestion(s): %s" - PkgDeployErrComponentSelectionCanceled = "component selection canceled: %s" -) - // Package validate const ( PkgValidateTemplateDeprecation = "Package template %q is using the deprecated syntax ###ZARF_PKG_VAR_%s###. This will be removed in Zarf v1.0.0. Please update to ###ZARF_PKG_TMPL_%s###." diff --git a/src/pkg/interactive/components.go b/src/pkg/interactive/components.go index 9189130209..4e24f3f083 100644 --- a/src/pkg/interactive/components.go +++ b/src/pkg/interactive/components.go @@ -8,7 +8,6 @@ import ( "fmt" "github.com/AlecAivazis/survey/v2" - "github.com/defenseunicorns/zarf/src/config/lang" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/utils" "github.com/defenseunicorns/zarf/src/types" @@ -35,7 +34,7 @@ func SelectOptionalComponent(component types.ZarfComponent) (confirm bool, err e } // SelectChoiceGroup prompts to select component groups -func SelectChoiceGroup(componentGroup []types.ZarfComponent) types.ZarfComponent { +func SelectChoiceGroup(componentGroup []types.ZarfComponent) (types.ZarfComponent, error) { message.HorizontalRule() var chosen int @@ -53,9 +52,5 @@ func SelectChoiceGroup(componentGroup []types.ZarfComponent) types.ZarfComponent pterm.Println() - if err := survey.AskOne(prompt, &chosen); err != nil { - message.Fatalf(nil, lang.PkgDeployErrComponentSelectionCanceled, err.Error()) - } - - return componentGroup[chosen] + return componentGroup[chosen], survey.AskOne(prompt, &chosen) } diff --git a/src/pkg/packager/filters/deploy.go b/src/pkg/packager/filters/deploy.go index f4a64a9f8c..fc53ef9427 100644 --- a/src/pkg/packager/filters/deploy.go +++ b/src/pkg/packager/filters/deploy.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/agnivade/levenshtein" - "github.com/defenseunicorns/zarf/src/config/lang" "github.com/defenseunicorns/zarf/src/pkg/interactive" "github.com/defenseunicorns/zarf/src/pkg/utils/helpers" "github.com/defenseunicorns/zarf/src/types" @@ -32,6 +31,14 @@ type deploymentFilter struct { isInteractive bool } +// Errors for the deployment filter. +var ( + ErrMultipleSameGroup = fmt.Errorf("cannot specify multiple components from the same group") + ErrNoDefaultOrSelection = fmt.Errorf("no default or selected component found") + ErrNotFound = fmt.Errorf("no compatible components found") + ErrSelectionCanceled = fmt.Errorf("selection canceled") +) + // Apply applies the filter. func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, error) { var selectedComponents []types.ZarfComponent @@ -88,7 +95,7 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, // Then check for already selected groups if groupSelected != nil { - return []types.ZarfComponent{}, fmt.Errorf(lang.PkgDeployErrMultipleComponentsSameGroup, groupSelected.Name, component.Name, component.DeprecatedGroup) + return nil, fmt.Errorf("%w: group: %s selected: %s, %s", ErrMultipleSameGroup, component.DeprecatedGroup, groupSelected.Name, component.Name) } // Then append to the final list @@ -106,7 +113,7 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, for _, component := range groupedComponents[groupKey] { componentNames = append(componentNames, component.Name) } - return []types.ZarfComponent{}, fmt.Errorf(lang.PkgDeployErrNoDefaultOrSelection, strings.Join(componentNames, ", ")) + return nil, fmt.Errorf("%w: choose from %s", ErrNoDefaultOrSelection, strings.Join(componentNames, ", ")) } } @@ -120,7 +127,7 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, closeEnough = append(closeEnough, c.Name) } } - return nil, fmt.Errorf(lang.PkgDeployErrNoCompatibleComponentsForSelection, requestedComponent, strings.Join(closeEnough, ", ")) + return nil, fmt.Errorf("%w: %s, suggestions (%s)", ErrNotFound, requestedComponent, strings.Join(closeEnough, ", ")) } } } else { @@ -128,7 +135,10 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, group := groupedComponents[groupKey] if len(group) > 1 { if f.isInteractive { - component := interactive.SelectChoiceGroup(group) + component, err := interactive.SelectChoiceGroup(group) + if err != nil { + return nil, fmt.Errorf("%w: %w", ErrSelectionCanceled, err) + } selectedComponents = append(selectedComponents, component) } else { foundDefault := false @@ -145,7 +155,7 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, } if !foundDefault { // If no default component was found, give up - return []types.ZarfComponent{}, fmt.Errorf(lang.PkgDeployErrNoDefaultOrSelection, strings.Join(componentNames, ", ")) + return nil, fmt.Errorf("%w: choose from %s", ErrNoDefaultOrSelection, strings.Join(componentNames, ", ")) } } } else { @@ -154,7 +164,7 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, if f.isInteractive { selected, err := interactive.SelectOptionalComponent(component) if err != nil { - return []types.ZarfComponent{}, fmt.Errorf(lang.PkgDeployErrComponentSelectionCanceled, err.Error()) + return nil, fmt.Errorf("%w: %w", ErrSelectionCanceled, err) } if selected { selectedComponents = append(selectedComponents, component) diff --git a/src/pkg/packager/filters/deploy_test.go b/src/pkg/packager/filters/deploy_test.go index b21f2ff75a..92f5c57578 100644 --- a/src/pkg/packager/filters/deploy_test.go +++ b/src/pkg/packager/filters/deploy_test.go @@ -119,6 +119,7 @@ func TestDeployFilter_Apply(t *testing.T) { pkg types.ZarfPackage optionalComponents string want []types.ZarfComponent + expectedErr error }{ "Test when version is less than v0.33.0 w/ no optional components selected": { pkg: types.ZarfPackage{ @@ -164,7 +165,11 @@ func TestDeployFilter_Apply(t *testing.T) { filter := ForDeploy(tc.optionalComponents, isInteractive) result, err := filter.Apply(tc.pkg) - require.NoError(t, err) + if tc.expectedErr != nil { + require.ErrorIs(t, err, tc.expectedErr) + } else { + require.NoError(t, err) + } equal := reflect.DeepEqual(tc.want, result) if !equal { left := []string{}