diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index 09c13cd13308c86a1eb7362d120d91b424dfdb89..008fb711a4cca7ad65030c629975d95904c52387 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -12,9 +12,10 @@ import ( ) var ( - ErrCheckCollections = errors.New("collections check error") - ErrInstallCollections = errors.New("failed to install collections") - ErrUninstallCollections = errors.New("failed to uninstall collections") + ErrCheckCollections = errors.New("collections check error") + ErrInstallCollections = errors.New("failed to install collections") + ErrUninstallCollections = errors.New("failed to uninstall collections") + ErrCollectionAlreadyExists = errors.New("failed to uninstall collections") ) type CollectionsOption func(c *CollectionConfig) @@ -81,9 +82,14 @@ func UpdateExistingCollection() CollectionsOption { } } -func SetSchemaMetadata(md map[string]string) CollectionsOption { +func SetSchemaMetadata(kv ...string) CollectionsOption { return func(c *CollectionConfig) { - c.metadata = md + if c.metadata == nil { + c.metadata = make(map[string]string) + } + for i := 0; i < len(kv); i += 2 { + c.metadata[kv[i]] = kv[i+1] + } } } @@ -152,7 +158,7 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS } } else { if collection.Schema.Metadata != nil && exist.Schema.Metadata["extension"] != collection.Schema.Metadata["extension"] && !s.IsForce() { - return false, errors.New("collection already exists") + return false, ErrCollectionAlreadyExists } var upd bool diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go index 027c13b9ea51c566076cc8c5fa9a3f31814f4273..a3a6bafa3cfa0e874e3374c33f0e6fe94cfd8ca2 100644 --- a/pkg/setup/collection_test.go +++ b/pkg/setup/collection_test.go @@ -107,7 +107,7 @@ func TestSetup_InstallCollections(t *testing.T) { wantErr: func(t *testing.T, err error) { assert.NoError(t, err) }, - co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}), + co: SetSchemaMetadata("extension", "test-extension"), }, { name: "Fail to update collection with the same id", @@ -118,7 +118,7 @@ func TestSetup_InstallCollections(t *testing.T) { wantErr: func(t *testing.T, err error) { assert.Error(t, err) }, - co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}), + co: SetSchemaMetadata("extension", "test-extension"), }, { name: "Update collection with the same id, with force", @@ -134,7 +134,7 @@ func TestSetup_InstallCollections(t *testing.T) { wantErr: func(t *testing.T, err error) { assert.NoError(t, err) }, - co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}), + co: SetSchemaMetadata("extension", "test-extension"), force: true, }, }