Skip to content

Commit

Permalink
update: add reg providers selection
Browse files Browse the repository at this point in the history
Signed-off-by: bupd <bupdprasanth@gmail.com>
  • Loading branch information
bupd committed May 31, 2024
1 parent a2fa749 commit d3cbcb1
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 126 deletions.
12 changes: 6 additions & 6 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 Down Expand Up @@ -52,7 +52,7 @@ func CreateRegistryCommand() *cobra.Command {
"insecure",
"",
true,
"Whether or not the certificate will be verified when Harbor tries to access the server",
"Whether Harbor will verify the server certificate",
)
flags.StringVarP(
&opts.Credential.AccessKey,
Expand All @@ -79,9 +79,9 @@ func CreateRegistryCommand() *cobra.Command {
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"`
}
24 changes: 0 additions & 24 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package utils

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
rview "github.com/goharbor/harbor-cli/pkg/views/registry/select"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

// Returns Harbor v2 client for given clientConfig
Expand All @@ -27,26 +23,6 @@ func PrintPayloadInJSONFormat(payload any) {
fmt.Println(string(jsonStr))
}

func GetRegistryTypeFromUser() string {
registryType := make(chan string)
go func() {
credentialName := viper.GetString("current-credential-name")
client := GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.ListRegistryProviderTypes(
ctx,
&registry.ListRegistryProviderTypesParams{},
)
if err != nil {
log.Fatal(err)
}

rview.RegistryListTypes(response.Payload, registryType)
}()

return <-registryType
}

func ParseProjectRepo(projectRepo string) (string, string) {
split := strings.Split(projectRepo, "/")
if len(split) != 2 {
Expand Down
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 d3cbcb1

Please sign in to comment.