diff --git a/cmd/harbor/root/context/list.go b/cmd/harbor/root/context/list.go index 24da31ce..0565c861 100644 --- a/cmd/harbor/root/context/list.go +++ b/cmd/harbor/root/context/list.go @@ -17,8 +17,8 @@ package context import ( "fmt" - "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" + contextview "github.com/goharbor/harbor-cli/pkg/views/context" "github.com/goharbor/harbor-cli/pkg/views/context/list" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -47,9 +47,9 @@ func ListContextCommand() *cobra.Command { return } } else { - var cxlist []api.ContextListView + var cxlist []contextview.ContextListView for _, cred := range config.Credentials { - cx := api.ContextListView{Name: cred.Name, Username: cred.Username, Server: cred.ServerAddress} + cx := contextview.ContextListView{Name: cred.Name, Username: cred.Username, Server: cred.ServerAddress} cxlist = append(cxlist, cx) } currentCredential := config.CurrentCredentialName diff --git a/cmd/harbor/root/registry/create.go b/cmd/harbor/root/registry/create.go index df1c1952..e3831d39 100644 --- a/cmd/harbor/root/registry/create.go +++ b/cmd/harbor/root/registry/create.go @@ -14,6 +14,7 @@ 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/views/registry/create" log "github.com/sirupsen/logrus" @@ -21,7 +22,7 @@ import ( ) func CreateRegistryCommand() *cobra.Command { - var opts api.CreateRegView + var opts create.CreateView cmd := &cobra.Command{ Use: "create", @@ -30,12 +31,12 @@ func CreateRegistryCommand() *cobra.Command { Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { var err error - createView := &api.CreateRegView{ + createView := &create.CreateView{ Name: opts.Name, Type: opts.Type, Description: opts.Description, URL: opts.URL, - Credential: api.RegistryCredential{ + Credential: create.RegistryCredential{ AccessKey: opts.Credential.AccessKey, Type: opts.Credential.Type, AccessSecret: opts.Credential.AccessSecret, @@ -44,7 +45,19 @@ func CreateRegistryCommand() *cobra.Command { } if opts.Name != "" && opts.Type != "" && opts.URL != "" { - err = api.CreateRegistry(opts) + registryModel := &models.Registry{ + Name: opts.Name, + Type: opts.Type, + Description: opts.Description, + URL: opts.URL, + Credential: &models.RegistryCredential{ + AccessKey: opts.Credential.AccessKey, + AccessSecret: opts.Credential.AccessSecret, + Type: opts.Credential.Type, + }, + Insecure: opts.Insecure, + } + err = api.CreateRegistry(registryModel) } else { err = createRegistryView(createView) } @@ -92,11 +105,24 @@ func CreateRegistryCommand() *cobra.Command { return cmd } -func createRegistryView(createView *api.CreateRegView) error { +func createRegistryView(createView *create.CreateView) error { if createView == nil { - createView = &api.CreateRegView{} + createView = &create.CreateView{} } create.CreateRegistryView(createView) - return api.CreateRegistry(*createView) + + registryModel := &models.Registry{ + Name: createView.Name, + Type: createView.Type, + Description: createView.Description, + URL: createView.URL, + Credential: &models.RegistryCredential{ + AccessKey: createView.Credential.AccessKey, + AccessSecret: createView.Credential.AccessSecret, + Type: createView.Credential.Type, + }, + Insecure: createView.Insecure, + } + return api.CreateRegistry(registryModel) } diff --git a/harbor-dev b/harbor-dev new file mode 100755 index 00000000..4c04438b Binary files /dev/null and b/harbor-dev differ diff --git a/pkg/api/common_types.go b/pkg/api/common_types.go new file mode 100644 index 00000000..f7b1ced7 --- /dev/null +++ b/pkg/api/common_types.go @@ -0,0 +1,26 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package api + +// ListFlags represents common list parameters used by multiple API handlers. +type ListFlags struct { + ProjectID int64 + Scope string + Name string + Page int64 + PageSize int64 + Q string + Sort string + Public bool +} diff --git a/pkg/api/types.go b/pkg/api/member_types.go similarity index 54% rename from pkg/api/types.go rename to pkg/api/member_types.go index 27cf95e8..1223fcc3 100644 --- a/pkg/api/types.go +++ b/pkg/api/member_types.go @@ -15,49 +15,7 @@ package api import "github.com/goharbor/go-client/pkg/sdk/v2.0/models" -type ListFlags struct { - ProjectID int64 - Scope string - Name string - Page int64 - PageSize int64 - Q string - Sort string - Public bool -} - -// CreateView for Registry -type CreateRegView struct { - Name string - Type string - Description string - URL string - Credential RegistryCredential - Insecure bool -} - -type ContextListView struct { - Name string - Username string - Server string -} - -// Credential for Registry -type RegistryCredential struct { - AccessKey string `json:"access_key,omitempty"` - Type string `json:"type,omitempty"` - AccessSecret string `json:"access_secret,omitempty"` -} - -type ListQuotaFlags struct { - PageSize int64 - Page int64 - Sort string - Reference string - ReferenceID string -} - -// Provides type for List member +// ListMemberOptions provides options for listing project members. type ListMemberOptions struct { XIsResourceName bool ProjectNameOrID string @@ -67,7 +25,7 @@ type ListMemberOptions struct { WithDetail bool } -// Provides type for Update member +// UpdateMemberOptions provides options for updating a project member. type UpdateMemberOptions struct { XIsResourceName bool ID int64 @@ -75,7 +33,7 @@ type UpdateMemberOptions struct { RoleID *models.RoleRequest } -// Provides Params for getting the Member +// GetMemberOptions provides parameters for getting a specific project member. type GetMemberOptions struct { XIsResourceName bool ID int64 diff --git a/pkg/api/quota_types.go b/pkg/api/quota_types.go new file mode 100644 index 00000000..37b9ee37 --- /dev/null +++ b/pkg/api/quota_types.go @@ -0,0 +1,22 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package api + +type ListQuotaFlags struct { + PageSize int64 + Page int64 + Sort string + Reference string + ReferenceID string +} diff --git a/pkg/api/registry_handler.go b/pkg/api/registry_handler.go index 61658fdb..279a5ea9 100644 --- a/pkg/api/registry_handler.go +++ b/pkg/api/registry_handler.go @@ -48,7 +48,7 @@ func ListRegistries(opts ...ListFlags) (*registry.ListRegistriesOK, error) { return response, nil } -func CreateRegistry(opts CreateRegView) error { +func CreateRegistry(opts *models.Registry) error { ctx, client, err := utils.ContextWithClient() if err != nil { return err @@ -57,18 +57,7 @@ func CreateRegistry(opts CreateRegView) error { _, 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, - }, + Registry: opts, }, ) if err != nil { diff --git a/pkg/prompt/prompt.go b/pkg/prompt/prompt.go index e33e4ac2..98e7e56b 100644 --- a/pkg/prompt/prompt.go +++ b/pkg/prompt/prompt.go @@ -26,6 +26,7 @@ import ( "github.com/goharbor/harbor-cli/pkg/constants" aview "github.com/goharbor/harbor-cli/pkg/views/artifact/select" tview "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/select" + contextview "github.com/goharbor/harbor-cli/pkg/views/context" immview "github.com/goharbor/harbor-cli/pkg/views/immutable/select" instview "github.com/goharbor/harbor-cli/pkg/views/instance/select" lview "github.com/goharbor/harbor-cli/pkg/views/label/select" @@ -287,7 +288,7 @@ func GetQuotaIDFromUser() int64 { QuotaID := make(chan int64) go func() { - response, err := api.ListQuota(*&api.ListQuotaFlags{}) + response, err := api.ListQuota(api.ListQuotaFlags{}) if err != nil { log.Errorf("failed to list quota: %v", err) } @@ -302,9 +303,9 @@ func GetActiveContextFromUser() (string, error) { if err != nil { return "", err } - var cxlist []api.ContextListView + var cxlist []contextview.ContextListView for _, cred := range config.Credentials { - cx := api.ContextListView{Name: cred.Name, Username: cred.Username, Server: cred.ServerAddress} + cx := contextview.ContextListView{Name: cred.Name, Username: cred.Username, Server: cred.ServerAddress} cxlist = append(cxlist, cx) } diff --git a/pkg/views/context/list/view.go b/pkg/views/context/list/view.go index 38d2c84d..4256cbc7 100644 --- a/pkg/views/context/list/view.go +++ b/pkg/views/context/list/view.go @@ -22,8 +22,8 @@ import ( "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" - "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/views/base/tablelist" + contextview "github.com/goharbor/harbor-cli/pkg/views/context" ) var columns = []table.Column{ @@ -32,7 +32,7 @@ var columns = []table.Column{ {Title: "Server Address", Width: tablelist.WidthXXL}, } -func ListContexts(contexts []api.ContextListView, currentCredential string) { +func ListContexts(contexts []contextview.ContextListView, currentCredential string) { rows := selectActiveContext(contexts, currentCredential) var opts []tea.ProgramOption @@ -48,7 +48,7 @@ func ListContexts(contexts []api.ContextListView, currentCredential string) { } } -func selectActiveContext(contexts []api.ContextListView, currentCredential string) []table.Row { +func selectActiveContext(contexts []contextview.ContextListView, currentCredential string) []table.Row { var rows []table.Row for _, ctx := range contexts { diff --git a/pkg/views/context/switch/view.go b/pkg/views/context/switch/view.go index 7f9417db..ad9fada8 100644 --- a/pkg/views/context/switch/view.go +++ b/pkg/views/context/switch/view.go @@ -21,11 +21,11 @@ import ( "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" - "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/views/base/selection" + contextview "github.com/goharbor/harbor-cli/pkg/views/context" ) -func ContextList(contexts []api.ContextListView, activeContext string) (string, error) { +func ContextList(contexts []contextview.ContextListView, activeContext string) (string, error) { itemsList := make([]list.Item, len(contexts)) for i, ctx := range contexts { diff --git a/pkg/views/context/types.go b/pkg/views/context/types.go new file mode 100644 index 00000000..560943b7 --- /dev/null +++ b/pkg/views/context/types.go @@ -0,0 +1,23 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package contextview holds shared types for context-related views. +package contextview + +// ContextListView represents a context entry used in list/switch views. +type ContextListView struct { + Name string + Username string + Server string +} diff --git a/pkg/views/registry/create/types.go b/pkg/views/registry/create/types.go new file mode 100644 index 00000000..24d904b5 --- /dev/null +++ b/pkg/views/registry/create/types.go @@ -0,0 +1,31 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package create + +// RegistryCredential holds credentials for a registry provider in the view layer. +type RegistryCredential struct { + AccessKey string `json:"access_key,omitempty"` + Type string `json:"type,omitempty"` + AccessSecret string `json:"access_secret,omitempty"` +} + +// CreateView is the interactive view model to create a registry. +type CreateView struct { + Name string + Type string + Description string + URL string + Credential RegistryCredential + Insecure bool +} diff --git a/pkg/views/registry/create/view.go b/pkg/views/registry/create/view.go index 979688c6..a7dfd6c8 100644 --- a/pkg/views/registry/create/view.go +++ b/pkg/views/registry/create/view.go @@ -31,7 +31,7 @@ type RegistryOption struct { Name string } -func CreateRegistryView(createView *api.CreateRegView) { +func CreateRegistryView(createView *CreateView) { registries, _ := api.GetRegistryProviders() // Initialize a slice to hold registry options