diff --git a/cmd/harbor/root/registry/delete.go b/cmd/harbor/root/registry/delete.go index 597846e5d..243988067 100644 --- a/cmd/harbor/root/registry/delete.go +++ b/cmd/harbor/root/registry/delete.go @@ -33,7 +33,12 @@ func DeleteRegistryCommand() *cobra.Command { errChan := make(chan error, len(args)) if len(args) > 0 { for _, arg := range args { - registryID, _ := api.GetRegistryIdByName(arg) + registryID, err := api.GetRegistryIdByName(arg) + if err != nil { + errChan <- fmt.Errorf("failed to get registry id for %q: %w", arg, err) + continue + } + wg.Add(1) go func(registryID int64) { defer wg.Done() diff --git a/cmd/harbor/root/registry/delete_test.go b/cmd/harbor/root/registry/delete_test.go new file mode 100644 index 000000000..bb2920a76 --- /dev/null +++ b/cmd/harbor/root/registry/delete_test.go @@ -0,0 +1,30 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package registry + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDeleteRegistryCommand(t *testing.T) { + cmd := DeleteRegistryCommand() + + assert.Equal(t, "delete", cmd.Use) + assert.Equal(t, "delete registry by name or id", cmd.Short) + assert.Equal(t, "harbor registry delete [registryname]", cmd.Example) + assert.NotNil(t, cmd.RunE) + assert.NotNil(t, cmd.Args) +} diff --git a/pkg/api/registry_handler.go b/pkg/api/registry_handler.go index 61658fdbe..9b725c615 100644 --- a/pkg/api/registry_handler.go +++ b/pkg/api/registry_handler.go @@ -175,7 +175,7 @@ func GetRegistryProviders() ([]string, error) { } func GetRegistryIdByName(registryName string) (int64, error) { - var opts ListFlags + opts := ListFlags{Name: registryName} r, err := ListRegistries(opts) if err != nil { @@ -188,5 +188,5 @@ func GetRegistryIdByName(registryName string) (int64, error) { } } - return 0, err + return 0, fmt.Errorf("registry '%s' not found", registryName) }