Skip to content

Commit

Permalink
feat: modified registy update command (goharbor#88)
Browse files Browse the repository at this point in the history
* modified registy update command

Signed-off-by: Althaf66 <althafasharaf02@gmail.com>

* modified registry cmd

Signed-off-by: ALTHAF <althafasharaf02@gmail.com>

* modified registry cmd

Signed-off-by: ALTHAF <althafasharaf02@gmail.com>

---------

Signed-off-by: Althaf66 <althafasharaf02@gmail.com>
Signed-off-by: ALTHAF <114910365+Althaf66@users.noreply.github.com>
Signed-off-by: ALTHAF <althafasharaf02@gmail.com>
  • Loading branch information
Althaf66 authored Dec 10, 2024
1 parent 2e06912 commit 3c16b0c
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 68 deletions.
131 changes: 64 additions & 67 deletions cmd/harbor/root/registry/update.go
Original file line number Diff line number Diff line change
@@ -1,103 +1,100 @@
package registry

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/registry/create"
"github.com/goharbor/harbor-cli/pkg/views/registry/update"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// NewUpdateRegistryCommand creates a new `harbor update registry` command
func UpdateRegistryCommand() *cobra.Command {
var opts api.CreateRegView
opts := &models.Registry{
Credential: &models.RegistryCredential{},
}

cmd := &cobra.Command{
Use: "update",
Short: "update registry",
Example: "harbor registry update [registryname]",
Args: cobra.MaximumNArgs(1),
Use: "update [registry_name]",
Short: "update registry",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var registryId int64

updateView := &api.CreateRegView{
Name: opts.Name,
Type: opts.Type,
Description: opts.Description,
URL: opts.URL,
Credential: api.RegistryCredential{
AccessKey: opts.Credential.AccessKey,
Type: opts.Credential.Type,
AccessSecret: opts.Credential.AccessSecret,
},
Insecure: opts.Insecure,
}

if len(args) > 0 {
registryId, err = api.GetRegistryIdByName(args[0])
if err != nil {
log.Errorf("failed to get registry id: %v", err)
return
}
} else {
registryId = prompt.GetRegistryNameFromUser()
}

if err != nil {
log.Errorf("failed to parse registry id: %v", err)
existingRegistry := api.GetRegistryResponse(registryId)
if existingRegistry == nil {
log.Errorf("registry is not found")
return
}

if opts.Name != "" && opts.Type != "" && opts.URL != "" {
err = api.UpdateRegistry(updateView, registryId)
} else {
err = updateRegistryView(updateView, registryId)
updateView := &models.Registry{
Name: existingRegistry.Name,
Type: existingRegistry.Type,
Description: existingRegistry.Description,
URL: existingRegistry.URL,
Insecure: existingRegistry.Insecure,
Credential: &models.RegistryCredential{
AccessKey: existingRegistry.Credential.AccessKey,
AccessSecret: existingRegistry.Credential.AccessSecret,
Type: existingRegistry.Credential.Type,
},
}

flags := cmd.Flags()
if flags.Changed("name") {
updateView.Name = opts.Name
}
if flags.Changed("type") {
updateView.Type = opts.Type
}
if flags.Changed("description") {
updateView.Description = opts.Description
}
if flags.Changed("url") {
updateView.URL = opts.URL
}
if flags.Changed("insecure") {
updateView.Insecure = opts.Insecure
}
if flags.Changed("credential-access-key") {
updateView.Credential.AccessKey = opts.Credential.AccessKey
}
if flags.Changed("credential-access-secret") {
updateView.Credential.AccessSecret = opts.Credential.AccessSecret
}
if flags.Changed("credential-type") {
updateView.Credential.Type = opts.Credential.Type
}

update.UpdateRegistryView(updateView)
err = api.UpdateRegistry(updateView, registryId)
if err != nil {
log.Errorf("failed to update registry: %v", err)
return
}
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "", "", "Name 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",
"",
"",
"Credential type, such as 'basic', 'oauth'",
)
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the registry")
flags.StringVarP(&opts.Type, "type", "t", "", "Type of the registry")
flags.StringVarP(&opts.URL, "url", "u", "", "Registry endpoint URL")
flags.StringVarP(&opts.Description, "description", "d", "", "Description of the registry")
flags.BoolVarP(&opts.Insecure, "insecure", "i", false, "Whether or not the certificate will be verified when Harbor tries to access the server")
flags.StringVarP(&opts.Credential.AccessKey, "credential-access-key", "k", "", "Access key, e.g. user name when credential type is 'basic'")
flags.StringVarP(&opts.Credential.AccessSecret, "credential-access-secret", "s", "", "Access secret, e.g. password when credential type is 'basic'")
flags.StringVarP(&opts.Credential.Type, "credential-type", "c", "", "Credential type, such as 'basic', 'oauth'")

return cmd
}

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

create.CreateRegistryView(updateView)
return api.UpdateRegistry(updateView, projectID)
}
18 changes: 17 additions & 1 deletion pkg/api/registry_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,23 @@ func ViewRegistry(registryId int64) (*registry.GetRegistryOK, error) {
return response, nil
}

func UpdateRegistry(updateView *CreateRegView, projectID int64) error {
func GetRegistryResponse(registryId int64) *models.Registry {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil
}
response, err := client.Registry.GetRegistry(ctx, &registry.GetRegistryParams{ID: registryId})
if err != nil {
return nil
}
if response.Payload.ID == 0 {
return nil
}

return response.GetPayload()
}

func UpdateRegistry(updateView *models.Registry, projectID int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
Expand Down
64 changes: 64 additions & 0 deletions pkg/views/registry/update/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package update

import (
"errors"

"github.com/charmbracelet/huh"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
log "github.com/sirupsen/logrus"
)

func UpdateRegistryView(updateView *models.Registry) {
theme := huh.ThemeCharm()
err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Provider").
Value(&updateView.Type).
Validate(func(str string) error {
if str == "" {
return errors.New("provider cannot be empty")
}
return nil
}),
huh.NewInput().
Title("Name").
Value(&updateView.Name).
Validate(func(str string) error {
if str == "" {
return errors.New("name cannot be empty")
}
return nil
}),
huh.NewInput().
Title("Description").
Value(&updateView.Description),
huh.NewInput().
Title("URL").
Value(&updateView.URL).
Validate(func(str string) error {
if str == "" {
return errors.New("url cannot be empty")
}
return nil
}),
huh.NewInput().
Title("Access ID").
Value(&updateView.Credential.AccessKey),
huh.NewInput().
Title("Access Secret").
EchoMode(huh.EchoModePassword).
Description("Replace the Access Secret to the real one").
Value(&updateView.Credential.AccessSecret),
huh.NewConfirm().
Title("Verify Cert").
Value(&updateView.Insecure).
Affirmative("yes").
Negative("no"),
),
).WithTheme(theme).Run()

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

0 comments on commit 3c16b0c

Please sign in to comment.