Skip to content

Commit

Permalink
add errors
Browse files Browse the repository at this point in the history
Signed-off-by: razzle <harry@razzle.cloud>
  • Loading branch information
Noxsios committed Mar 15, 2024
1 parent 2517b19 commit d58a945
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
8 changes: 0 additions & 8 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -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###."
Expand Down
9 changes: 2 additions & 7 deletions src/pkg/interactive/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
}
24 changes: 17 additions & 7 deletions src/pkg/packager/filters/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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, ", "))
}
}

Expand All @@ -120,15 +127,18 @@ 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 {
for _, groupKey := range orderedComponentGroups {
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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/pkg/packager/filters/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit d58a945

Please sign in to comment.