Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Page size #290

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/harbor/root/labels/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func ListLabelCommand() *cobra.Command {
Use: "list",
Short: "list labels",
Run: func(cmd *cobra.Command, args []string) {
if !cmd.Flags().Changed("page-size") {
opts.PageSize = utils.ResolvePageSize(20)
}
label, err := api.ListLabel(opts)
if err != nil {
log.Fatalf("failed to get label list: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions cmd/harbor/root/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func ListProjectCommand() *cobra.Command {
Use: "list",
Short: "list project",
Run: func(cmd *cobra.Command, args []string) {
if !cmd.Flags().Changed("page-size") {
opts.PageSize = utils.ResolvePageSize(10)
}
if private && public {
log.Fatal("Cannot specify both --private and --public flags")
} else if private {
Expand Down
3 changes: 3 additions & 0 deletions cmd/harbor/root/registry/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func ListRegistryCommand() *cobra.Command {
Use: "list",
Short: "list registry",
Run: func(cmd *cobra.Command, args []string) {
if !cmd.Flags().Changed("page-size") {
opts.PageSize = utils.ResolvePageSize(10)
}
registry, err := api.ListRegistries(opts)

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/harbor/root/schedule/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func ListScheduleCommand() *cobra.Command {
Use: "list",
Short: "show all schedule jobs in Harbor",
Run: func(cmd *cobra.Command, args []string) {
if !cmd.Flags().Changed("page-size") {
opts.PageSize = utils.ResolvePageSize(10)
}
schedule, err := api.ListSchedule(opts)
if err != nil {
log.Fatalf("failed to get schedule list: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions cmd/harbor/root/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func UserListCmd() *cobra.Command {
Args: cobra.NoArgs,
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
if !cmd.Flags().Changed("page-size") {
opts.PageSize = utils.ResolvePageSize(10)
}
response, err := api.ListUsers(opts)
if err != nil {
log.Errorf("failed to list users: %v", err)
Expand Down
25 changes: 25 additions & 0 deletions pkg/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"

log "github.com/sirupsen/logrus"
Expand All @@ -20,6 +21,7 @@ type Credential struct {
}

type HarborConfig struct {
PageSize int `mapstructure:"page-size" yaml:"page-size"`
CurrentCredentialName string `mapstructure:"current-credential-name" yaml:"current-credential-name"`
Credentials []Credential `mapstructure:"credentials" yaml:"credentials"`
}
Expand Down Expand Up @@ -330,11 +332,13 @@ func CreateConfigFile(configPath string) error {

defaultConfig := HarborConfig{
CurrentCredentialName: "",
PageSize: 10,
Credentials: []Credential{},
}

v.Set("current-credential-name", defaultConfig.CurrentCredentialName)
v.Set("credentials", defaultConfig.Credentials)
v.Set("page-size", defaultConfig.PageSize)

if err := v.WriteConfigAs(configPath); err != nil {
log.Fatalf("failed to write config file: %v", err)
Expand Down Expand Up @@ -392,6 +396,7 @@ func AddCredentialsToConfigFile(credential Credential, configPath string) error

v.Set("current-credential-name", c.CurrentCredentialName)
v.Set("credentials", c.Credentials)
v.Set("page-size", c.PageSize)

if err := v.WriteConfig(); err != nil {
log.Fatalf("failed to write updated config file: %v", err)
Expand Down Expand Up @@ -436,6 +441,7 @@ func UpdateCredentialsInConfigFile(updatedCredential Credential, configPath stri

v.Set("current-credential-name", c.CurrentCredentialName)
v.Set("credentials", c.Credentials)
v.Set("page-size", c.PageSize)

if err := v.WriteConfig(); err != nil {
log.Fatalf("failed to write updated config file: %v", err)
Expand All @@ -444,3 +450,22 @@ func UpdateCredentialsInConfigFile(updatedCredential Credential, configPath stri
log.Infof("Updated credential '%s' in config file at %s", updatedCredential.Name, configPath)
return nil
}

// obtaining the defaault page-size from the config/environment
func ResolvePageSize(x int64) int64 {
config, err := GetCurrentHarborConfig()
if err != nil {
return x
}
if config != nil && config.PageSize > 0 {
return int64(config.PageSize)
}
envPageSize := os.Getenv("HARBOR_CLI_DEFAULT_PAGE_SIZE")
if envPageSize != "" {
if val, err := strconv.ParseInt(envPageSize, 10, 64); err == nil && val > 0 {
return val
}
}

return x
}
Loading