-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
f9412b8
commit 0957d83
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ®istry.GetRegistryParams{ID: opts.id}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
utils.PrintPayloadInJSONFormat(response.GetPayload()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters