forked from goharbor/harbor-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modified registy update command (goharbor#88)
* 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
Showing
3 changed files
with
145 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |