Skip to content

Commit

Permalink
Merge branch 'main' into dagger-ci
Browse files Browse the repository at this point in the history
Signed-off-by: Bishal Das  <70086051+bishal7679@users.noreply.github.com>
  • Loading branch information
bishal7679 authored Jul 28, 2024
2 parents e77af6a + fa6234f commit 837a1fa
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 74 deletions.
7 changes: 4 additions & 3 deletions cmd/harbor/root/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ func CreateProjectCommand() *cobra.Command {
var opts create.CreateView

cmd := &cobra.Command{
Use: "create",
Use: "create [project name]",
Short: "create project",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
opts.ProjectName = args[0]
var err error
createView := &create.CreateView{
ProjectName: opts.ProjectName,
Expand All @@ -40,8 +42,7 @@ func CreateProjectCommand() *cobra.Command {
}

flags := cmd.Flags()
flags.StringVarP(&opts.ProjectName, "name", "", "", "Name of the project")
flags.BoolVarP(&opts.Public, "public", "", false, "Project is public or private. Private by default")
flags.BoolVarP(&opts.Public, "public", "", false, "Project is public or private")
flags.StringVarP(&opts.RegistryID, "registry-id", "", "", "ID of referenced registry when creating the proxy cache project")
flags.StringVarP(&opts.StorageLimit, "storage-limit", "", "-1", "Storage quota of the project")
flags.BoolVarP(&opts.ProxyCache, "proxy-cache", "", false, "Whether the project is a proxy cache project")
Expand Down
45 changes: 34 additions & 11 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 All @@ -39,26 +39,49 @@ func CreateRegistryCommand() *cobra.Command {
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.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", "", "basic", "Credential type, such as 'basic', 'oauth'")
flags.BoolVarP(
&opts.Insecure,
"insecure",
"",
true,
"Whether Harbor will verify the server certificate",
)
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 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
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.6
github.com/charmbracelet/huh v0.3.0
github.com/charmbracelet/lipgloss v0.10.0
github.com/charmbracelet/lipgloss v0.12.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand All @@ -20,6 +20,7 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.1.2 // indirect

github.com/charmbracelet/x/input v0.1.0 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.0 // indirect
Expand Down Expand Up @@ -49,6 +50,10 @@ require (
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect

golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.7.0 // indirect

gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down Expand Up @@ -106,5 +111,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.1 // indirect

gopkg.in/yaml.v2 v2.4.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ github.com/charmbracelet/bubbletea v0.26.6 h1:zTCWSuST+3yZYZnVSvbXwKOPRSNZceVeqp
github.com/charmbracelet/bubbletea v0.26.6/go.mod h1:dz8CWPlfCCGLFbBlTY4N7bjLiyOGDJEnd2Muu7pOWhk=
github.com/charmbracelet/huh v0.3.0 h1:CxPplWkgW2yUTDDG0Z4S5HH8SJOosWHd4LxCvi0XsKE=
github.com/charmbracelet/huh v0.3.0/go.mod h1:fujUdKX8tC45CCSaRQdw789O6uaCRwx8l2NDyKfC4jA=

github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY=
github.com/charmbracelet/x/ansi v0.1.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=

github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=
github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=
github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=
Expand Down Expand Up @@ -418,8 +420,10 @@ golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=

golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=

golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
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"`
}
10 changes: 10 additions & 0 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"strings"
"time"
)

Expand All @@ -25,3 +26,12 @@ func FormatCreatedTime(timestamp string) (string, error) {
return fmt.Sprintf("%d day ago", days), nil
}
}

func FormatUrl(url string) string {
// Check if URL starts with "http://" or "https://"
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
// If not, prepend "https://"
url = "https://" + url
}
return url
}
Loading

0 comments on commit 837a1fa

Please sign in to comment.