Skip to content

Commit

Permalink
Fixed the registry info issue (goharbor#237)
Browse files Browse the repository at this point in the history
* fixed the issue#224

Signed-off-by: ALTHAF <[email protected]>

* fixed the issue#224

Signed-off-by: ALTHAF <[email protected]>

* some modification in registry

Signed-off-by: ALTHAF <[email protected]>

---------

Signed-off-by: ALTHAF <[email protected]>
  • Loading branch information
Althaf66 authored Nov 1, 2024
1 parent de2fb47 commit 82a1435
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
8 changes: 4 additions & 4 deletions cmd/harbor/root/registry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
14 changes: 6 additions & 8 deletions cmd/harbor/root/registry/delete.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
13 changes: 6 additions & 7 deletions cmd/harbor/root/registry/info.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions cmd/harbor/root/registry/update.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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()
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/api/registry_handler.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 82a1435

Please sign in to comment.