Skip to content

Commit

Permalink
Add create registry cmd
Browse files Browse the repository at this point in the history
This adds support for the create
registry cmd. User can also pass
different options while creating
the registry.

Signed-off-by: Akshat <akshat25iiit@gmail.com>
  • Loading branch information
akshatdalton committed May 15, 2023
1 parent d285cdf commit 3bf5c61
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cmd/registry/create_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package registry

import (
"context"

"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/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/spf13/cobra"
)

type createRegistrytOptions struct {
name string
_type string
url string
description string
insecure bool
credential struct {
accessKey string
accessSecret string
_type string
}
}

// NewCreateRegistryCommand creates a new `harbor create registry` command
func NewCreateRegistryCommand() *cobra.Command {
var opts createRegistrytOptions

cmd := &cobra.Command{
Use: "registry",
Short: "create registry",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return runCreateRegistry(opts, credentialName)
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.name, "name", "", "", "Name of the registry")
flags.StringVarP(&opts._type, "type", "", "harbor", "Type of the registry")
flags.StringVarP(&opts.url, "url", "", "", "Registry endpoint URL")
flags.StringVarP(&opts.description, "description", "", "", "Description of the registry")
flags.BoolVarP(&opts.insecure, "insecure", "", true, "Whether or not the certificate will be verified when Harbor tries to access the server")
flags.StringVarP(&opts.credential.accessKey, "credential-access-key", "", "", "Access key, e.g. user name when credential type is 'basic'")
flags.StringVarP(&opts.credential.accessKey, "credential-access-secret", "", "", "Access secret, e.g. password when credential type is 'basic'")
flags.StringVarP(&opts.credential._type, "credential-type", "", "basic", "Credential type, such as 'basic', 'oauth'")

return cmd
}

func runCreateRegistry(opts createRegistrytOptions, credentialName string) error {
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.CreateRegistry(ctx, &registry.CreateRegistryParams{Registry: &models.Registry{Credential: &models.RegistryCredential{AccessKey: opts.credential.accessKey, AccessSecret: opts.credential.accessSecret, Type: opts.credential._type}, Description: opts.description, Insecure: opts.insecure, Name: opts.name, Type: opts._type, URL: opts.url}})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response)
return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func newCreateCommand() *cobra.Command {

cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.NewCreateProjectCommand())
cmd.AddCommand(registry.NewCreateRegistryCommand())
return cmd
}

Expand Down

0 comments on commit 3bf5c61

Please sign in to comment.