Skip to content

Commit

Permalink
Merge pull request #86 from bupd/registry-create
Browse files Browse the repository at this point in the history
Enhance `registry create` Command
  • Loading branch information
Vad1mo authored Jul 26, 2024
2 parents 533879b + d3cbcb1 commit fa6234f
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 70 deletions.
45 changes: 34 additions & 11 deletions cmd/harbor/root/registry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (

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

cmd := &cobra.Command{
Use: "create",
Short: "create registry",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var err error
createView := &create.CreateView{
createView := &api.CreateRegView{
Name: opts.Name,
Type: opts.Type,
Description: opts.Description,
URL: opts.URL,
Credential: create.RegistryCredential{
Credential: api.RegistryCredential{
AccessKey: opts.Credential.AccessKey,
Type: opts.Credential.Type,
AccessSecret: opts.Credential.AccessSecret,
Expand All @@ -39,26 +39,49 @@ func CreateRegistryCommand() *cobra.Command {
if err != nil {
log.Errorf("failed to create registry: %v", err)
}

},
}

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.Type, "type", "", "", "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.AccessSecret, "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'")
flags.BoolVarP(
&opts.Insecure,
"insecure",
"",
true,
"Whether Harbor will verify the server certificate",
)
flags.StringVarP(
&opts.Credential.AccessKey,
"credential-access-key",
"",
"",
"Access key, e.g. user name when credential type is 'basic'",
)
flags.StringVarP(
&opts.Credential.AccessSecret,
"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 createRegistryView(createView *create.CreateView) error {
func createRegistryView(createView *api.CreateRegView) error {
if createView == nil {
createView = &create.CreateView{}
createView = &api.CreateRegView{}
}

create.CreateRegistryView(createView)
Expand Down
42 changes: 33 additions & 9 deletions cmd/harbor/root/registry/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// NewUpdateRegistryCommand creates a new `harbor update registry` command
func UpdateRegistryCommand() *cobra.Command {
var opts create.CreateView
var opts api.CreateRegView

cmd := &cobra.Command{
Use: "update",
Expand All @@ -22,12 +22,12 @@ func UpdateRegistryCommand() *cobra.Command {
var err error
var registryId int64

updateView := &create.CreateView{
updateView := &api.CreateRegView{
Name: opts.Name,
Type: opts.Type,
Description: opts.Description,
URL: opts.URL,
Credential: create.RegistryCredential{
Credential: api.RegistryCredential{
AccessKey: opts.Credential.AccessKey,
Type: opts.Credential.Type,
AccessSecret: opts.Credential.AccessSecret,
Expand Down Expand Up @@ -62,17 +62,41 @@ func UpdateRegistryCommand() *cobra.Command {
flags.StringVarP(&opts.Type, "type", "", "", "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.AccessSecret, "credential-access-secret", "", "", "Access secret, e.g. password when credential type is 'basic'")
flags.StringVarP(&opts.Credential.Type, "credential-type", "", "", "Credential type, such as 'basic', 'oauth'")
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.AccessSecret,
"credential-access-secret",
"",
"",
"Access secret, e.g. password when credential type is 'basic'",
)
flags.StringVarP(
&opts.Credential.Type,
"credential-type",
"",
"",
"Credential type, such as 'basic', 'oauth'",
)

return cmd
}

func updateRegistryView(updateView *create.CreateView, projectID int64) error {
func updateRegistryView(updateView *api.CreateRegView, projectID int64) error {
if updateView == nil {
updateView = &create.CreateView{}
updateView = &api.CreateRegView{}
}

create.CreateRegistryView(updateView)
Expand Down
51 changes: 40 additions & 11 deletions pkg/api/registry_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"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"
"github.com/goharbor/harbor-cli/pkg/views/registry/create"
log "github.com/sirupsen/logrus"
)

Expand All @@ -27,22 +26,36 @@ func ListRegistries(opts ...ListFlags) (*registry.ListRegistriesOK, error) {
Name: &listFlags.Name,
Sort: &listFlags.Sort,
})

if err != nil {
return nil, err
}

return response, nil
}

func CreateRegistry(opts create.CreateView) error {
func CreateRegistry(opts CreateRegView) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}

_, 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}})

_, 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
}
Expand All @@ -57,7 +70,6 @@ func DeleteRegistry(registryName int64) error {
return err
}
_, err = client.Registry.DeleteRegistry(ctx, &registry.DeleteRegistryParams{ID: registryName})

if err != nil {
return err
}
Expand All @@ -74,7 +86,6 @@ func InfoRegistry(registryId int64) error {
}

response, err := client.Registry.GetRegistry(ctx, &registry.GetRegistryParams{ID: registryId})

if err != nil {
return err
}
Expand All @@ -89,7 +100,6 @@ func GetRegistry(registryId int64) error {
return err
}
response, err := client.Registry.GetRegistry(ctx, &registry.GetRegistryParams{ID: registryId})

if err != nil {
return err
}
Expand All @@ -98,7 +108,7 @@ func GetRegistry(registryId int64) error {
return nil
}

func UpdateRegistry(updateView *create.CreateView, projectID int64) error {
func UpdateRegistry(updateView *CreateRegView, projectID int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
Expand All @@ -113,8 +123,10 @@ func UpdateRegistry(updateView *create.CreateView, projectID int64) error {
Insecure: &updateView.Insecure,
}

_, err = client.Registry.UpdateRegistry(ctx, &registry.UpdateRegistryParams{ID: projectID, Registry: registryUpdate})

_, err = client.Registry.UpdateRegistry(
ctx,
&registry.UpdateRegistryParams{ID: projectID, Registry: registryUpdate},
)
if err != nil {
return err
}
Expand All @@ -123,3 +135,20 @@ func UpdateRegistry(updateView *create.CreateView, projectID int64) error {

return nil
}

// Get List of Registry Providers
func GetRegistryProviders() ([]string, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
}
response, err := client.Registry.ListRegistryProviderTypes(
ctx,
&registry.ListRegistryProviderTypesParams{},
)
if err != nil {
return nil, err
}

return response.Payload, nil
}
17 changes: 17 additions & 0 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,20 @@ type ListFlags struct {
Sort string
Public bool
}

// CreateView for Registry
type CreateRegView struct {
Name string
Type string
Description string
URL string
Credential RegistryCredential
Insecure bool
}

// Credential for Registry
type RegistryCredential struct {
AccessKey string `json:"access_key,omitempty"`
Type string `json:"type,omitempty"`
AccessSecret string `json:"access_secret,omitempty"`
}
10 changes: 10 additions & 0 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"strings"
"time"
)

Expand All @@ -25,3 +26,12 @@ func FormatCreatedTime(timestamp string) (string, error) {
return fmt.Sprintf("%d day ago", days), nil
}
}

func FormatUrl(url string) string {
// Check if URL starts with "http://" or "https://"
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
// If not, prepend "https://"
url = "https://" + url
}
return url
}
53 changes: 36 additions & 17 deletions pkg/views/registry/create/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,59 @@ package create

import (
"errors"
"fmt"
"strconv"

"github.com/charmbracelet/huh"
"github.com/goharbor/harbor-cli/pkg/api"
log "github.com/sirupsen/logrus"
)

type CreateView struct {
Name string
Type string
Description string
URL string
Credential RegistryCredential
Insecure bool
// struct to hold registry options
type RegistryOption struct {
ID string
Name string
}

type RegistryCredential struct {
AccessKey string `json:"access_key,omitempty"`
Type string `json:"type,omitempty"`
AccessSecret string `json:"access_secret,omitempty"`
}
func CreateRegistryView(createView *api.CreateRegView) {
registries, _ := api.GetRegistryProviders()

// Initialize a slice to hold registry options
var registryOptions []RegistryOption

// Iterate over registries to populate registryOptions
for i, registry := range registries {
registryOptions = append(registryOptions, RegistryOption{
ID: strconv.FormatInt(int64(i), 10),
Name: fmt.Sprintf("%s", registry),
})
}

// Initialize a slice to hold select options
var registrySelectOptions []huh.Option[string]

// Iterate over registryOptions to populate registrySelectOptions
for _, option := range registryOptions {
registrySelectOptions = append(
registrySelectOptions,
huh.NewOption(option.Name, option.Name),
)
}

func CreateRegistryView(createView *CreateView) {
theme := huh.ThemeCharm()
err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Provider").
huh.NewSelect[string]().
Title("Select a Registry Provider").
Value(&createView.Type).
Options(registrySelectOptions...).
Validate(func(str string) error {
if str == "" {
return errors.New("provider cannot be empty")
return errors.New("registry provider cannot be empty")
}
return nil
}),

huh.NewInput().
Title("Name").
Value(&createView.Name).
Expand Down Expand Up @@ -69,7 +89,6 @@ func CreateRegistryView(createView *CreateView) {
Negative("no"),
),
).WithTheme(theme).Run()

if err != nil {
log.Fatal(err)
}
Expand Down
Loading

0 comments on commit fa6234f

Please sign in to comment.