diff --git a/cmd/harbor/root/registry/create.go b/cmd/harbor/root/registry/create.go index 0a049442..babcdba5 100644 --- a/cmd/harbor/root/registry/create.go +++ b/cmd/harbor/root/registry/create.go @@ -6,58 +6,80 @@ 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" "github.com/spf13/cobra" "github.com/spf13/viper" ) -type createRegistrytOptions struct { - name string - _type string - url string - description string - insecure bool - credential struct { - accessKey string - accessSecret string - _type string - } -} - // NewCreateRegistryCommand creates a new `harbor create registry` command func CreateRegistryCommand() *cobra.Command { - var opts createRegistrytOptions + var opts create.CreateView cmd := &cobra.Command{ Use: "create", Short: "create registry", - RunE: func(cmd *cobra.Command, args []string) error { - return runCreateRegistry(opts) + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + var err error + createView := &create.CreateView{ + Name: opts.Name, + Type: opts.Type, + Description: opts.Description, + URL: opts.URL, + Credential: create.RegistryCredential{ + AccessKey: opts.Credential.AccessKey, + Type: opts.Credential.Type, + AccessSecret: opts.Credential.AccessSecret, + }, + Insecure: opts.Insecure, + } + + if opts.Name != "" && opts.Type != "" && opts.URL != "" { + err = runCreateRegistry(opts) + } else { + err = createRegistryView(createView) + } + + 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.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.accessKey, "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.StringVarP(&opts.Name, "name", "", "", "Name of the registry") + flags.StringVarP(&opts.Type, "type", "", "harbor", "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'") return cmd } -func runCreateRegistry(opts createRegistrytOptions) error { +func createRegistryView(createView *create.CreateView) error { + if createView == nil { + createView = &create.CreateView{} + } + + create.CreateRegistryView(createView) + return runCreateRegistry(*createView) +} + +func runCreateRegistry(opts create.CreateView) error { credentialName := viper.GetString("current-credential-name") client := utils.GetClientByCredentialName(credentialName) ctx := context.Background() - response, err := client.Registry.CreateRegistry(ctx, ®istry.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, ®istry.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 } - utils.PrintPayloadInJSONFormat(response) + log.Infof("Registry %s created", opts.Name) return nil } diff --git a/pkg/views/registry/create/view.go b/pkg/views/registry/create/view.go new file mode 100644 index 00000000..1f3c6f1b --- /dev/null +++ b/pkg/views/registry/create/view.go @@ -0,0 +1,76 @@ +package create + +import ( + "errors" + + "github.com/charmbracelet/huh" + log "github.com/sirupsen/logrus" +) + +type CreateView struct { + Name string + Type string + Description string + URL string + Credential RegistryCredential + Insecure bool +} + +type RegistryCredential struct { + AccessKey string `json:"access_key,omitempty"` + Type string `json:"type,omitempty"` + AccessSecret string `json:"access_secret,omitempty"` +} + +func CreateRegistryView(createView *CreateView) { + theme := huh.ThemeCharm() + err := huh.NewForm( + huh.NewGroup( + huh.NewInput(). + Title("Provider"). + Value(&createView.Type). + Validate(func(str string) error { + if str == "" { + return errors.New("provider cannot be empty") + } + return nil + }), + huh.NewInput(). + Title("Name"). + Value(&createView.Name). + Validate(func(str string) error { + if str == "" { + return errors.New("name cannot be empty") + } + return nil + }), + huh.NewInput(). + Title("Description"). + Value(&createView.Description), + huh.NewInput(). + Title("URL"). + Value(&createView.URL). + Validate(func(str string) error { + if str == "" { + return errors.New("url cannot be empty") + } + return nil + }), + huh.NewInput(). + Title("Access Key"). + Value(&createView.Credential.AccessKey), + huh.NewInput(). + Title("Access Secret"). + Value(&createView.Credential.AccessSecret), + huh.NewConfirm(). + Title("Verify Cert"). + Value(&createView.Insecure). + Affirmative("yes"). + Negative("no"), + ), + ).WithTheme(theme).Run() + + if err != nil { + log.Fatal(err) + } +}