Skip to content

Commit

Permalink
Use joined errors
Browse files Browse the repository at this point in the history
Go 1.20 added support for joined errors; using those instead of error
slices allows typical nil/non-nil semantics to be used, and allows all
error messages to be shown simultaneously when asserting.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Aug 30, 2024
1 parent 46036fc commit 74e87da
Show file tree
Hide file tree
Showing 40 changed files with 197 additions and 239 deletions.
11 changes: 6 additions & 5 deletions internal/cmd/skupper/connector/kube/connector_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kube

import (
"context"
"errors"
"fmt"
"strconv"
"time"
Expand All @@ -12,7 +13,7 @@ import (
skupperv1alpha1 "github.com/skupperproject/skupper/pkg/generated/client/clientset/versioned/typed/skupper/v1alpha1"
"github.com/skupperproject/skupper/pkg/utils/validator"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
k8serrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ func NewCmdConnectorCreate() *CmdConnectorCreate {
Example: connectorCreateExample,
PreRun: skupperCmd.NewClient,
Run: func(cmd *cobra.Command, args []string) {
utils.HandleErrorList(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.Run())
},
PostRunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -91,7 +92,7 @@ func (cmd *CmdConnectorCreate) AddFlags() {
cmd.CobraCmd.Flags().StringVarP(&cmd.flags.output, "output", "o", "", "print resources to the console instead of submitting them to the Skupper controller. Choices: json, yaml")
}

func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
func (cmd *CmdConnectorCreate) ValidateInput(args []string) error {
var validationErrors []error
resourceStringValidator := validator.NewResourceStringValidator()
numberValidator := validator.NewNumberValidator()
Expand Down Expand Up @@ -139,7 +140,7 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
// Validate if there is already a Connector with this name in the namespace
if cmd.name != "" {
connector, err := cmd.client.Connectors(cmd.namespace).Get(context.TODO(), cmd.name, metav1.GetOptions{})
if connector != nil && !errors.IsNotFound(err) {
if connector != nil && !k8serrs.IsNotFound(err) {
validationErrors = append(validationErrors, fmt.Errorf("there is already a connector %s created for namespace %s", cmd.name, cmd.namespace))
}
}
Expand Down Expand Up @@ -195,7 +196,7 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
cmd.output = cmd.flags.output
}
}
return validationErrors
return errors.Join(validationErrors...)
}

func (cmd *CmdConnectorCreate) Run() error {
Expand Down
5 changes: 1 addition & 4 deletions internal/cmd/skupper/connector/kube/connector_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,7 @@ func TestCmdConnectorCreate_ValidateInput(t *testing.T) {

actualErrors := command.ValidateInput(test.args)

actualErrorsMessages := utils.ErrorsToMessages(actualErrors)

assert.DeepEqual(t, actualErrorsMessages, test.expectedErrors)

utils.AssertErrorMessagesMatch(t, test.expectedErrors, actualErrors)
})
}
}
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/skupper/connector/kube/connector_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kube

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -41,7 +42,7 @@ func NewCmdConnectorDelete() *CmdConnectorDelete {
Example: connectorDeleteExample,
PreRun: skupperCmd.NewClient,
Run: func(cmd *cobra.Command, args []string) {
utils.HandleErrorList(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.Run())
},
PostRunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -66,7 +67,7 @@ func (cmd *CmdConnectorDelete) AddFlags() {
cmd.CobraCmd.Flags().DurationVarP(&cmd.flags.timeout, "timeout", "t", 60*time.Second, "Raise an error if the operation does not complete in the given period of time.")
}

func (cmd *CmdConnectorDelete) ValidateInput(args []string) []error {
func (cmd *CmdConnectorDelete) ValidateInput(args []string) error {
var validationErrors []error
resourceStringValidator := validator.NewResourceStringValidator()

Expand Down Expand Up @@ -98,7 +99,7 @@ func (cmd *CmdConnectorDelete) ValidateInput(args []string) []error {
}
}

return validationErrors
return errors.Join(validationErrors...)
}

func (cmd *CmdConnectorDelete) Run() error {
Expand Down
5 changes: 1 addition & 4 deletions internal/cmd/skupper/connector/kube/connector_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ func TestCmdConnectorDelete_ValidateInput(t *testing.T) {

actualErrors := command.ValidateInput(test.args)

actualErrorsMessages := utils.ErrorsToMessages(actualErrors)

assert.DeepEqual(t, actualErrorsMessages, test.expectedErrors)

utils.AssertErrorMessagesMatch(t, test.expectedErrors, actualErrors)
})
}
}
Expand Down
11 changes: 6 additions & 5 deletions internal/cmd/skupper/connector/kube/connector_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kube

import (
"context"
"errors"
"fmt"
"os"
"text/tabwriter"
Expand All @@ -11,7 +12,7 @@ import (
skupperv1alpha1 "github.com/skupperproject/skupper/pkg/generated/client/clientset/versioned/typed/skupper/v1alpha1"
"github.com/skupperproject/skupper/pkg/utils/validator"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
k8serrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func NewCmdConnectorStatus() *CmdConnectorStatus {
Example: connectorStatusExample,
PreRun: skupperCmd.NewClient,
Run: func(cmd *cobra.Command, args []string) {
utils.HandleErrorList(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.Run())
},
}
Expand All @@ -67,7 +68,7 @@ func (cmd *CmdConnectorStatus) AddFlags() {
cmd.CobraCmd.Flags().StringVarP(&cmd.flags.output, "output", "o", "", "print status of connectors Choices: json, yaml")
}

func (cmd *CmdConnectorStatus) ValidateInput(args []string) []error {
func (cmd *CmdConnectorStatus) ValidateInput(args []string) error {
var validationErrors []error
resourceStringValidator := validator.NewResourceStringValidator()
outputTypeValidator := validator.NewOptionValidator(utils.OutputTypes)
Expand All @@ -91,7 +92,7 @@ func (cmd *CmdConnectorStatus) ValidateInput(args []string) []error {
// Validate that there is a connector with this name in the namespace
if cmd.name != "" {
connector, err := cmd.client.Connectors(cmd.namespace).Get(context.TODO(), cmd.name, metav1.GetOptions{})
if connector == nil || errors.IsNotFound(err) {
if connector == nil || k8serrs.IsNotFound(err) {
validationErrors = append(validationErrors, fmt.Errorf("connector %s does not exist in namespace %s", cmd.name, cmd.namespace))
}
}
Expand All @@ -105,7 +106,7 @@ func (cmd *CmdConnectorStatus) ValidateInput(args []string) []error {
}
}

return validationErrors
return errors.Join(validationErrors...)
}
func (cmd *CmdConnectorStatus) Run() error {
if cmd.name == "" {
Expand Down
5 changes: 1 addition & 4 deletions internal/cmd/skupper/connector/kube/connector_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ func TestCmdConnectorStatus_ValidateInput(t *testing.T) {

actualErrors := command.ValidateInput(test.args)

actualErrorsMessages := utils.ErrorsToMessages(actualErrors)

assert.DeepEqual(t, actualErrorsMessages, test.expectedErrors)

utils.AssertErrorMessagesMatch(t, test.expectedErrors, actualErrors)
})
}
}
Expand Down
11 changes: 6 additions & 5 deletions internal/cmd/skupper/connector/kube/connector_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kube

import (
"context"
"errors"
"fmt"
"time"

Expand All @@ -11,7 +12,7 @@ import (
skupperv1alpha1 "github.com/skupperproject/skupper/pkg/generated/client/clientset/versioned/typed/skupper/v1alpha1"
"github.com/skupperproject/skupper/pkg/utils/validator"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
k8serrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ func NewCmdConnectorUpdate() *CmdConnectorUpdate {
Example: connectorUpdateExample,
PreRun: skupperCmd.NewClient,
Run: func(cmd *cobra.Command, args []string) {
utils.HandleErrorList(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.Run())
},
PostRunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -95,7 +96,7 @@ func (cmd *CmdConnectorUpdate) AddFlags() {
cmd.CobraCmd.Flags().StringVarP(&cmd.flags.output, "output", "o", "", "Print resources to the console instead of submitting them to the Skupper controller. Choices: json, yaml")
}

func (cmd *CmdConnectorUpdate) ValidateInput(args []string) []error {
func (cmd *CmdConnectorUpdate) ValidateInput(args []string) error {
var validationErrors []error
resourceStringValidator := validator.NewResourceStringValidator()
numberValidator := validator.NewNumberValidator()
Expand All @@ -122,7 +123,7 @@ func (cmd *CmdConnectorUpdate) ValidateInput(args []string) []error {
// Validate that there is already a connector with this name in the namespace
if cmd.name != "" {
connector, err := cmd.client.Connectors(cmd.namespace).Get(context.TODO(), cmd.name, metav1.GetOptions{})
if connector == nil || errors.IsNotFound(err) {
if connector == nil || k8serrs.IsNotFound(err) {
validationErrors = append(validationErrors, fmt.Errorf("connector %s must exist in namespace %s to be updated", cmd.name, cmd.namespace))
} else {
// save existing values
Expand Down Expand Up @@ -202,7 +203,7 @@ func (cmd *CmdConnectorUpdate) ValidateInput(args []string) []error {
}
}

return validationErrors
return errors.Join(validationErrors...)
}

func (cmd *CmdConnectorUpdate) Run() error {
Expand Down
5 changes: 1 addition & 4 deletions internal/cmd/skupper/connector/kube/connector_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,7 @@ func TestCmdConnectorUpdate_ValidateInput(t *testing.T) {

actualErrors := command.ValidateInput(test.args)

actualErrorsMessages := utils.ErrorsToMessages(actualErrors)

assert.DeepEqual(t, actualErrorsMessages, test.expectedErrors)

utils.AssertErrorMessagesMatch(t, test.expectedErrors, actualErrors)
})
}
}
Expand Down
10 changes: 6 additions & 4 deletions internal/cmd/skupper/link/kube/link_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package kube

import (
"context"
"errors"
"fmt"
"strconv"

"github.com/skupperproject/skupper/internal/cmd/skupper/utils"
"github.com/skupperproject/skupper/internal/kube/client"
skupperv1alpha1 "github.com/skupperproject/skupper/pkg/generated/client/clientset/versioned/typed/skupper/v1alpha1"
"github.com/skupperproject/skupper/pkg/utils/validator"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strconv"
)

var (
Expand Down Expand Up @@ -40,7 +42,7 @@ func NewCmdLinkDelete() *CmdLinkDelete {
Example: "skupper site delete my-link",
PreRun: skupperCmd.NewClient,
Run: func(cmd *cobra.Command, args []string) {
utils.HandleErrorList(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.ValidateInput(args))
skupperCmd.InputToOptions()
utils.HandleError(skupperCmd.Run())
},
Expand All @@ -67,7 +69,7 @@ func (cmd *CmdLinkDelete) AddFlags() {
cmd.CobraCmd.Flags().StringVar(&cmd.flags.timeout, "timeout", "60", "raise an error if the operation does not complete in the given period of time (expressed in seconds).")
}

func (cmd *CmdLinkDelete) ValidateInput(args []string) []error {
func (cmd *CmdLinkDelete) ValidateInput(args []string) error {
var validationErrors []error
timeoutValidator := validator.NewTimeoutInSecondsValidator()

Expand Down Expand Up @@ -99,7 +101,7 @@ func (cmd *CmdLinkDelete) ValidateInput(args []string) []error {
}
}

return validationErrors
return errors.Join(validationErrors...)
}
func (cmd *CmdLinkDelete) InputToOptions() {
cmd.timeout, _ = strconv.Atoi(cmd.flags.timeout)
Expand Down
8 changes: 3 additions & 5 deletions internal/cmd/skupper/link/kube/link_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package kube

import (
"fmt"
"testing"

"github.com/skupperproject/skupper/internal/cmd/skupper/utils"
fakeclient "github.com/skupperproject/skupper/internal/kube/client/fake"
"github.com/skupperproject/skupper/pkg/apis/skupper/v1alpha1"
"github.com/spf13/pflag"
"gotest.tools/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"testing"
)

func TestCmdLinkDelete_NewCmdLinkDelete(t *testing.T) {
Expand Down Expand Up @@ -240,10 +241,7 @@ func TestCmdLinkDelete_ValidateInput(t *testing.T) {

actualErrors := command.ValidateInput(test.args)

actualErrorsMessages := utils.ErrorsToMessages(actualErrors)

assert.DeepEqual(t, actualErrorsMessages, test.expectedErrors)

utils.AssertErrorMessagesMatch(t, test.expectedErrors, actualErrors)
})
}
}
Expand Down
14 changes: 8 additions & 6 deletions internal/cmd/skupper/link/kube/link_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ package kube

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/skupperproject/skupper/internal/cmd/skupper/utils"
"github.com/skupperproject/skupper/internal/kube/client"
"github.com/skupperproject/skupper/pkg/apis/skupper/v1alpha1"
Expand All @@ -16,9 +21,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"strconv"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -60,7 +62,7 @@ func NewCmdLinkGenerate() *CmdLinkGenerate {
Long: linkGenerateLong,
PreRun: skupperCmd.NewClient,
Run: func(cmd *cobra.Command, args []string) {
utils.HandleErrorList(skupperCmd.ValidateInput(args))
utils.HandleError(skupperCmd.ValidateInput(args))
skupperCmd.InputToOptions()
utils.HandleError(skupperCmd.Run())
},
Expand Down Expand Up @@ -92,7 +94,7 @@ func (cmd *CmdLinkGenerate) AddFlags() {
cmd.CobraCmd.Flags().StringVar(&cmd.flags.timeout, "timeout", "60", "raise an error if the operation does not complete in the given period of time (expressed in seconds).")
}

func (cmd *CmdLinkGenerate) ValidateInput(args []string) []error {
func (cmd *CmdLinkGenerate) ValidateInput(args []string) error {

var validationErrors []error
resourceStringValidator := validator.NewResourceStringValidator()
Expand Down Expand Up @@ -155,7 +157,7 @@ func (cmd *CmdLinkGenerate) ValidateInput(args []string) []error {
}
}

return validationErrors
return errors.Join(validationErrors...)
}

func (cmd *CmdLinkGenerate) InputToOptions() {
Expand Down
Loading

0 comments on commit 74e87da

Please sign in to comment.