Skip to content

Commit

Permalink
Add registry get command
Browse files Browse the repository at this point in the history
This commit adds the registry get command
for the user to get the registry by ID.

Signed-off-by: Akshat <akshat25iiit@gmail.com>
  • Loading branch information
akshatdalton committed May 4, 2023
1 parent f9412b8 commit 0957d83
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
56 changes: 56 additions & 0 deletions cmd/registry/get_registry.go
@@ -0,0 +1,56 @@
package registry

import (
"context"
"fmt"
"strconv"

"github.com/akshatdalton/harbor-cli/cmd/constants"
"github.com/akshatdalton/harbor-cli/cmd/utils"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/spf13/cobra"
)

type getRegistryOptions struct {
id int64
}

// NewGetRegistryCommand creates a new `harbor get registry` command
func NewGetRegistryCommand() *cobra.Command {
var opts getRegistryOptions

cmd := &cobra.Command{
Use: "registry [ID]",
Short: "get registry by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("Invalid argument: %s. Expected an integer.\n", args[0])
return err
}
opts.id = int64(id)

credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return runGetRegistry(opts, credentialName)
},
}

return cmd
}

func runGetRegistry(opts getRegistryOptions, credentialName string) error {
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.GetRegistry(ctx, &registry.GetRegistryParams{ID: opts.id})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response.GetPayload())
return nil
}
2 changes: 2 additions & 0 deletions cmd/root.go
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/akshatdalton/harbor-cli/cmd/constants"
"github.com/akshatdalton/harbor-cli/cmd/login"
"github.com/akshatdalton/harbor-cli/cmd/project"
"github.com/akshatdalton/harbor-cli/cmd/registry"
"github.com/spf13/cobra"
)

Expand All @@ -17,6 +18,7 @@ func newGetCommand() *cobra.Command {

cmd.PersistentFlags().String(constants.CredentialNameOption, "", "Name of the credential to use for authentication")
cmd.AddCommand(project.NewGetProjectCommand())
cmd.AddCommand(registry.NewGetRegistryCommand())
return cmd
}

Expand Down

0 comments on commit 0957d83

Please sign in to comment.