From 82a143517e10506541a86ba3c5af238aeaf8eacc Mon Sep 17 00:00:00 2001 From: ALTHAF <114910365+Althaf66@users.noreply.github.com> Date: Sat, 2 Nov 2024 02:31:40 +0530 Subject: [PATCH] Fixed the registry info issue (#237) * fixed the issue#224 Signed-off-by: ALTHAF * fixed the issue#224 Signed-off-by: ALTHAF * some modification in registry Signed-off-by: ALTHAF --------- Signed-off-by: ALTHAF --- cmd/harbor/root/registry/create.go | 8 ++++---- cmd/harbor/root/registry/delete.go | 14 ++++++-------- cmd/harbor/root/registry/info.go | 13 ++++++------- cmd/harbor/root/registry/update.go | 11 +++++------ pkg/api/registry_handler.go | 22 ++++++++++++++++++++++ 5 files changed, 43 insertions(+), 25 deletions(-) diff --git a/cmd/harbor/root/registry/create.go b/cmd/harbor/root/registry/create.go index 30efec3d..bf4d93ac 100644 --- a/cmd/harbor/root/registry/create.go +++ b/cmd/harbor/root/registry/create.go @@ -7,14 +7,14 @@ import ( "github.com/spf13/cobra" ) -// NewCreateRegistryCommand creates a new `harbor create registry` command func CreateRegistryCommand() *cobra.Command { var opts api.CreateRegView cmd := &cobra.Command{ - Use: "create", - Short: "create registry", - Args: cobra.NoArgs, + Use: "create", + Short: "create registry", + Example: "harbor registry create", + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { var err error createView := &api.CreateRegView{ diff --git a/cmd/harbor/root/registry/delete.go b/cmd/harbor/root/registry/delete.go index ed9d426b..92687744 100644 --- a/cmd/harbor/root/registry/delete.go +++ b/cmd/harbor/root/registry/delete.go @@ -1,27 +1,25 @@ package registry import ( - "strconv" - "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -// NewDeleteRegistryCommand creates a new `harbor delete registry` command func DeleteRegistryCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "delete", - Short: "delete registry by id", - Args: cobra.MaximumNArgs(1), + Use: "delete", + Short: "delete registry by id", + Example: "harbor registry delete [registryname]", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var err error if len(args) > 0 { - registryId, _ := strconv.ParseInt(args[0], 10, 64) - err = api.DeleteRegistry(registryId) + registryName, _ := api.GetRegistryIdByName(args[0]) + err = api.DeleteRegistry(registryName) } else { registryId := prompt.GetRegistryNameFromUser() err = api.DeleteRegistry(registryId) diff --git a/cmd/harbor/root/registry/info.go b/cmd/harbor/root/registry/info.go index 17da984c..97a16799 100644 --- a/cmd/harbor/root/registry/info.go +++ b/cmd/harbor/root/registry/info.go @@ -1,8 +1,6 @@ package registry import ( - "strconv" - "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" @@ -11,15 +9,16 @@ import ( func InfoRegistryCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "info", - Short: "get registry info", - Args: cobra.MaximumNArgs(1), + Use: "info", + Short: "get registry info", + Example: "harbor registry info [registryname]", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var err error if len(args) > 0 { - registryId, _ := strconv.ParseInt(args[0], 10, 64) - err = api.InfoRegistry(registryId) + registryName, _ := api.GetRegistryIdByName(args[0]) + err = api.InfoRegistry(registryName) } else { registryId := prompt.GetRegistryNameFromUser() err = api.InfoRegistry(registryId) diff --git a/cmd/harbor/root/registry/update.go b/cmd/harbor/root/registry/update.go index fde3cb4c..0db5db45 100644 --- a/cmd/harbor/root/registry/update.go +++ b/cmd/harbor/root/registry/update.go @@ -1,8 +1,6 @@ package registry import ( - "strconv" - "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/views/registry/create" @@ -15,9 +13,10 @@ func UpdateRegistryCommand() *cobra.Command { var opts api.CreateRegView cmd := &cobra.Command{ - Use: "update", - Short: "update registry", - Args: cobra.MaximumNArgs(1), + Use: "update", + Short: "update registry", + Example: "harbor registry update [registryname]", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var err error var registryId int64 @@ -36,7 +35,7 @@ func UpdateRegistryCommand() *cobra.Command { } if len(args) > 0 { - registryId, err = strconv.ParseInt(args[0], 10, 64) + registryId, err = api.GetRegistryIdByName(args[0]) } else { registryId = prompt.GetRegistryNameFromUser() } diff --git a/pkg/api/registry_handler.go b/pkg/api/registry_handler.go index aafcc147..e3a341cb 100644 --- a/pkg/api/registry_handler.go +++ b/pkg/api/registry_handler.go @@ -1,6 +1,8 @@ package api import ( + "fmt" + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" "github.com/goharbor/go-client/pkg/sdk/v2.0/models" "github.com/goharbor/harbor-cli/pkg/utils" @@ -89,6 +91,9 @@ func InfoRegistry(registryId int64) error { if err != nil { return err } + if response.Payload.ID == 0 { + return fmt.Errorf("registry is not found") + } utils.PrintPayloadInJSONFormat(response.Payload) return nil @@ -138,3 +143,20 @@ func GetRegistryProviders() ([]string, error) { return response.Payload, nil } + +func GetRegistryIdByName(registryName string) (int64, error) { + var opts ListFlags + + r, err := ListRegistries(opts) + if err != nil { + return 0, err + } + + for _, registry := range r.Payload { + if registry.Name == registryName { + return registry.ID, nil + } + } + + return 0, err +}