Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the registry info issue #237

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/harbor/root/registry/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func Registry() *cobra.Command {
DeleteRegistryCommand(),
ListRegistryCommand(),
UpdateRegistryCommand(),
ViewCommand(),
)

return cmd
Expand Down
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
42 changes: 0 additions & 42 deletions cmd/harbor/root/registry/view.go

This file was deleted.

36 changes: 22 additions & 14 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,22 +91,11 @@ func InfoRegistry(registryId int64) error {
if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response.Payload)
return nil
}

func GetRegistry(registryId int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}
response, err := client.Registry.GetRegistry(ctx, &registry.GetRegistryParams{ID: registryId})
if err != nil {
return err
if response.Payload.ID == 0 {
return fmt.Errorf("registry is not found")
}

utils.PrintPayloadInJSONFormat(response.GetPayload())
utils.PrintPayloadInJSONFormat(response.Payload)
return nil
}

Expand Down Expand Up @@ -152,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
}