Skip to content

Commit

Permalink
Merge pull request #364 from DopplerHQ/pagination
Browse files Browse the repository at this point in the history
Support pagination when listing projects, environments, and configs
  • Loading branch information
Piccirello authored Feb 2, 2023
2 parents 61d9282 + 87d0163 commit 3b7e867
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 18 deletions.
8 changes: 6 additions & 2 deletions pkg/cmd/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ var configsCloneCmd = &cobra.Command{
func configs(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
environment := cmd.Flag("environment").Value.String()
number := utils.GetIntFlag(cmd, "number", 16)
page := utils.GetIntFlag(cmd, "page", 16)
localConfig := configuration.LocalConfig(cmd)

utils.RequireValue("token", localConfig.Token.Value)

configs, err := http.GetConfigs(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, environment)
configs, err := http.GetConfigs(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, environment, page, number)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}
Expand Down Expand Up @@ -182,7 +184,7 @@ func deleteConfigs(cmd *cobra.Command, args []string) {
}

if !utils.Silent {
configs, err := http.GetConfigs(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, "")
configs, err := http.GetConfigs(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, "", 1, 100)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}
Expand Down Expand Up @@ -354,6 +356,8 @@ func unlockedConfigNamesValidArgs(cmd *cobra.Command, args []string, toComplete
func init() {
configsCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsCmd.Flags().StringP("environment", "e", "", "config environment")
configsCmd.Flags().IntP("number", "n", 100, "max number of configs to display")
configsCmd.Flags().Int("page", 1, "page to display")

configsGetCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsGetCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/enclave_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ var enclaveConfigsUnlockCmd = &cobra.Command{
func init() {
enclaveConfigsCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsCmd.Flags().StringP("environment", "e", "", "config environment")
enclaveConfigsCmd.Flags().IntP("number", "n", 100, "max number of configs to display")
enclaveConfigsCmd.Flags().Int("page", 1, "page to display")

enclaveConfigsGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsGetCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/enclave_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ func init() {
enclaveEnvironmentsCmd.AddCommand(enclaveEnvironmentsGetCmd)

enclaveEnvironmentsCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveEnvironmentsCmd.Flags().IntP("number", "n", 100, "max number of environments to display")
enclaveEnvironmentsCmd.Flags().Int("page", 1, "page to display")
enclaveCmd.AddCommand(enclaveEnvironmentsCmd)
}
3 changes: 3 additions & 0 deletions pkg/cmd/enclave_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ var enclaveProjectsUpdateCmd = &cobra.Command{
}

func init() {
enclaveProjectsCmd.Flags().IntP("number", "n", 100, "max number of projects to display")
enclaveProjectsCmd.Flags().Int("page", 1, "page to display")

enclaveProjectsGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveProjectsCmd.AddCommand(enclaveProjectsGetCmd)

Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ var environmentsRenameCmd = &cobra.Command{
func environments(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
localConfig := configuration.LocalConfig(cmd)
number := utils.GetIntFlag(cmd, "number", 16)
page := utils.GetIntFlag(cmd, "page", 16)

utils.RequireValue("token", localConfig.Token.Value)

info, err := http.GetEnvironments(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value)
info, err := http.GetEnvironments(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, page, number)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}
Expand Down Expand Up @@ -146,7 +148,7 @@ func deleteEnvironment(cmd *cobra.Command, args []string) {
}

if !utils.Silent {
info, err := http.GetEnvironments(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value)
info, err := http.GetEnvironments(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, 1, 100)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}
Expand Down Expand Up @@ -213,5 +215,7 @@ func init() {
environmentsCmd.AddCommand(environmentsRenameCmd)

environmentsCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
environmentsCmd.Flags().IntP("number", "n", 100, "max number of environments to display")
environmentsCmd.Flags().Int("page", 1, "page to display")
rootCmd.AddCommand(environmentsCmd)
}
9 changes: 7 additions & 2 deletions pkg/cmd/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ var projectsUpdateCmd = &cobra.Command{
func projects(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
localConfig := configuration.LocalConfig(cmd)
number := utils.GetIntFlag(cmd, "number", 16)
page := utils.GetIntFlag(cmd, "page", 16)

utils.RequireValue("token", localConfig.Token.Value)

info, err := http.GetProjects(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value)
info, err := http.GetProjects(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, page, number)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}
Expand Down Expand Up @@ -145,7 +147,7 @@ func deleteProjects(cmd *cobra.Command, args []string) {
}

if !utils.Silent {
info, err := http.GetProjects(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value)
info, err := http.GetProjects(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, 1, 100)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}
Expand Down Expand Up @@ -206,6 +208,9 @@ func projectIDsValidArgs(cmd *cobra.Command, args []string, toComplete string) (
}

func init() {
projectsCmd.Flags().IntP("number", "n", 100, "max number of projects to display")
projectsCmd.Flags().Int("page", 1, "page to display")

projectsGetCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
projectsCmd.AddCommand(projectsGetCmd)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func setup(cmd *cobra.Command, args []string) {
break
}

projects, httpErr := http.GetProjects(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value)
projects, httpErr := http.GetProjects(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, 1, 100)
if !httpErr.IsNil() {
utils.HandleError(httpErr.Unwrap(), httpErr.Message)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func setup(cmd *cobra.Command, args []string) {
break
}

configs, apiError := http.GetConfigs(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, selectedProject, "")
configs, apiError := http.GetConfigs(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, selectedProject, "", 1, 100)
if !apiError.IsNil() {
utils.HandleError(apiError.Unwrap(), apiError.Message)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func GetConfigs(config models.ScopedOptions) ([]models.ConfigInfo, Error) {
utils.RequireValue("token", config.Token.Value)

configs, err := http.GetConfigs(config.APIHost.Value, utils.GetBool(config.VerifyTLS.Value, true), config.Token.Value, config.EnclaveProject.Value, "")
configs, err := http.GetConfigs(config.APIHost.Value, utils.GetBool(config.VerifyTLS.Value, true), config.Token.Value, config.EnclaveProject.Value, "", 1, 100)
if !err.IsNil() {
return nil, Error{Err: err.Unwrap(), Message: err.Message}
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func GetConfigTokenSlugs(config models.ScopedOptions) ([]string, Error) {
func GetEnvironmentIDs(config models.ScopedOptions) ([]string, Error) {
utils.RequireValue("token", config.Token.Value)

environments, err := http.GetEnvironments(config.APIHost.Value, utils.GetBool(config.VerifyTLS.Value, true), config.Token.Value, config.EnclaveProject.Value)
environments, err := http.GetEnvironments(config.APIHost.Value, utils.GetBool(config.VerifyTLS.Value, true), config.Token.Value, config.EnclaveProject.Value, 1, 100)
if !err.IsNil() {
return nil, Error{Err: err.Unwrap(), Message: err.Message}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func GetProjectIDs(config models.ScopedOptions) ([]string, Error) {
utils.RequireValue("token", config.Token.Value)

info, err := http.GetProjects(config.APIHost.Value, utils.GetBool(config.VerifyTLS.Value, true), config.Token.Value)
info, err := http.GetProjects(config.APIHost.Value, utils.GetBool(config.VerifyTLS.Value, true), config.Token.Value, 1, 100)
if !err.IsNil() {
return nil, Error{Err: err.Unwrap(), Message: err.Message}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func SecretsToBytes(secrets map[string]string, format string, templateBody strin
if format == models.JSONMountFormat {
envStr, err := json.Marshal(secrets)
if err != nil {
return nil, Error{Err: err, Message: "Unable to marshall secrets to json"}
return nil, Error{Err: err, Message: "Unable to marshal secrets to json"}
}
return envStr, Error{}
}

if format == models.DotNETJSONMountFormat {
envStr, err := json.Marshal(utils.MapToDotNETJSONFormat(secrets))
if err != nil {
return nil, Error{Err: err, Message: "Unable to marshall .NET formatted secrets to json"}
return nil, Error{Err: err, Message: "Unable to marshal .NET formatted secrets to json"}
}
return envStr, Error{}
}
Expand Down
14 changes: 9 additions & 5 deletions pkg/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,10 @@ func SetWorkplaceSettings(host string, verifyTLS bool, apiKey string, values mod
}

// GetProjects get projects
func GetProjects(host string, verifyTLS bool, apiKey string) ([]models.ProjectInfo, Error) {
func GetProjects(host string, verifyTLS bool, apiKey string, page int, number int) ([]models.ProjectInfo, Error) {
var params []queryParam
params = append(params, queryParam{Key: "per_page", Value: "100"})
params = append(params, queryParam{Key: "page", Value: strconv.Itoa(page)})
params = append(params, queryParam{Key: "per_page", Value: strconv.Itoa(number)})

url, err := generateURL(host, "/v3/projects", params)
if err != nil {
Expand Down Expand Up @@ -580,9 +581,11 @@ func DeleteProject(host string, verifyTLS bool, apiKey string, project string) E
}

// GetEnvironments get environments
func GetEnvironments(host string, verifyTLS bool, apiKey string, project string) ([]models.EnvironmentInfo, Error) {
func GetEnvironments(host string, verifyTLS bool, apiKey string, project string, page int, number int) ([]models.EnvironmentInfo, Error) {
var params []queryParam
params = append(params, queryParam{Key: "project", Value: project})
params = append(params, queryParam{Key: "page", Value: strconv.Itoa(page)})
params = append(params, queryParam{Key: "per_page", Value: strconv.Itoa(number)})

url, err := generateURL(host, "/v3/environments", params)
if err != nil {
Expand Down Expand Up @@ -741,10 +744,11 @@ func RenameEnvironment(host string, verifyTLS bool, apiKey string, project strin
}

// GetConfigs get configs
func GetConfigs(host string, verifyTLS bool, apiKey string, project string, environment string) ([]models.ConfigInfo, Error) {
func GetConfigs(host string, verifyTLS bool, apiKey string, project string, environment string, page int, number int) ([]models.ConfigInfo, Error) {
var params []queryParam
params = append(params, queryParam{Key: "project", Value: project})
params = append(params, queryParam{Key: "per_page", Value: "100"})
params = append(params, queryParam{Key: "per_page", Value: strconv.Itoa(number)})
params = append(params, queryParam{Key: "page", Value: strconv.Itoa(page)})
if environment != "" {
params = append(params, queryParam{Key: "environment", Value: environment})
}
Expand Down

0 comments on commit 3b7e867

Please sign in to comment.