diff --git a/cmd/harbor/root/registry/update.go b/cmd/harbor/root/registry/update.go index 0db5db45..84b17aec 100644 --- a/cmd/harbor/root/registry/update.go +++ b/cmd/harbor/root/registry/update.go @@ -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) -} diff --git a/pkg/api/registry_handler.go b/pkg/api/registry_handler.go index 0df427fc..490fe08a 100644 --- a/pkg/api/registry_handler.go +++ b/pkg/api/registry_handler.go @@ -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, ®istry.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 diff --git a/pkg/views/registry/update/view.go b/pkg/views/registry/update/view.go new file mode 100644 index 00000000..c073c400 --- /dev/null +++ b/pkg/views/registry/update/view.go @@ -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) + } +}