mirror of https://github.com/fluxcd/cli-utils.git
Avoid referencing cmd line flags in error strings
This commit is contained in:
parent
56b08e9176
commit
d727d4bf2f
|
|
@ -32,6 +32,18 @@ func (e *UnknownTypesError) Error() string {
|
|||
return fmt.Sprintf("unknown resource types: %s", strings.Join(gks, ","))
|
||||
}
|
||||
|
||||
// NamespaceMismatchError is returned if all resources must be in a specific
|
||||
// namespace, and resources are found using other namespaces.
|
||||
type NamespaceMismatchError struct {
|
||||
RequiredNamespace string
|
||||
Namespace string
|
||||
}
|
||||
|
||||
func (e *NamespaceMismatchError) Error() string {
|
||||
return fmt.Sprintf("found namespace %q, but all resources must be in namespace %q",
|
||||
e.Namespace, e.RequiredNamespace)
|
||||
}
|
||||
|
||||
// SetNamespaces verifies that every namespaced resource has the namespace
|
||||
// set, and if one does not, it will set the namespace to the provided
|
||||
// defaultNamespace.
|
||||
|
|
@ -82,9 +94,10 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
|
|||
} else {
|
||||
ns := obj.GetNamespace()
|
||||
if enforceNamespace && ns != defaultNamespace {
|
||||
return fmt.Errorf("the namespace from the provided object %q "+
|
||||
"does not match the namespace %q. You must pass '--namespace=%s' to perform this operation",
|
||||
ns, defaultNamespace, ns)
|
||||
return &NamespaceMismatchError{
|
||||
Namespace: ns,
|
||||
RequiredNamespace: defaultNamespace,
|
||||
}
|
||||
}
|
||||
}
|
||||
case meta.RESTScopeRoot:
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
|
@ -30,7 +31,7 @@ func TestSetNamespaces(t *testing.T) {
|
|||
enforceNamespace bool
|
||||
|
||||
expectedNamespaces []string
|
||||
expectedErrText string
|
||||
expectedErr error
|
||||
}{
|
||||
"resources already have namespace": {
|
||||
objs: []*unstructured.Unstructured{
|
||||
|
|
@ -86,7 +87,10 @@ func TestSetNamespaces(t *testing.T) {
|
|||
},
|
||||
defaultNamspace: "bar",
|
||||
enforceNamespace: true,
|
||||
expectedErrText: "does not match the namespace",
|
||||
expectedErr: &NamespaceMismatchError{
|
||||
RequiredNamespace: "bar",
|
||||
Namespace: "foo",
|
||||
},
|
||||
},
|
||||
"cluster-scoped CR with CRD": {
|
||||
objs: []*unstructured.Unstructured{
|
||||
|
|
@ -141,7 +145,18 @@ func TestSetNamespaces(t *testing.T) {
|
|||
Kind: "AnotherCustom",
|
||||
}, ""),
|
||||
},
|
||||
expectedErrText: "unknown resource types: Custom.custom.io,AnotherCustom.custom.io",
|
||||
expectedErr: &UnknownTypesError{
|
||||
GroupKinds: []schema.GroupKind{
|
||||
{
|
||||
Group: "custom.io",
|
||||
Kind: "Custom",
|
||||
},
|
||||
{
|
||||
Group: "custom.io",
|
||||
Kind: "AnotherCustom",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -157,11 +172,9 @@ func TestSetNamespaces(t *testing.T) {
|
|||
|
||||
err = SetNamespaces(mapper, tc.objs, tc.defaultNamspace, tc.enforceNamespace)
|
||||
|
||||
if tc.expectedErrText != "" {
|
||||
if err == nil {
|
||||
t.Errorf("expected error %s, but not nil", tc.expectedErrText)
|
||||
}
|
||||
assert.Contains(t, err.Error(), tc.expectedErrText)
|
||||
if tc.expectedErr != nil {
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, tc.expectedErr, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue