From 4b1948fe3fa76a079fd67062dd663f1c561c8c9f Mon Sep 17 00:00:00 2001 From: Jeroen Huinink Date: Wed, 18 Jan 2023 18:18:24 +0000 Subject: [PATCH] feat(variable get): Add scope parameter to variable get subcommand --- api/variable.go | 8 ++++++-- commands/variable/get/get.go | 11 ++++++++++- docs/source/variable/get.md | 2 ++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/api/variable.go b/api/variable.go index 31e1bac8..a73b48c0 100644 --- a/api/variable.go +++ b/api/variable.go @@ -30,12 +30,16 @@ var ListProjectVariables = func(client *gitlab.Client, projectID interface{}, op return vars, nil } -var GetProjectVariable = func(client *gitlab.Client, projectID interface{}, key string, opts *gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, error) { +var GetProjectVariable = func(client *gitlab.Client, projectID interface{}, key string, scope string) (*gitlab.ProjectVariable, error) { if client == nil { client = apiClient.Lab() } - reqOpts := &gitlab.GetProjectVariableOptions{} + reqOpts := &gitlab.GetProjectVariableOptions{ + Filter: &gitlab.VariableFilter{ + EnvironmentScope: scope, + }, + } vars, _, err := client.ProjectVariables.GetVariable(projectID, key, reqOpts) if err != nil { return nil, err diff --git a/commands/variable/get/get.go b/commands/variable/get/get.go index 5a00e111..47d6e563 100644 --- a/commands/variable/get/get.go +++ b/commands/variable/get/get.go @@ -1,6 +1,7 @@ package get import ( + "errors" "fmt" "github.com/MakeNowJust/heredoc" @@ -20,6 +21,7 @@ type GetOps struct { Key string Group string + Scope string } func NewCmdSet(f *cmdutils.Factory, runE func(opts *GetOps) error) *cobra.Command { @@ -34,6 +36,7 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *GetOps) error) *cobra.Comman Example: heredoc.Doc(` glab variable get VAR_KEY glab variable get -g GROUP VAR_KEY + glab variable get -s SCOPE VAR_KEY `), RunE: func(cmd *cobra.Command, args []string) (err error) { opts.HTTPClient = f.HttpClient @@ -46,6 +49,11 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *GetOps) error) *cobra.Comman return } + if cmd.Flags().Changed("scope") && opts.Group != "" { + err = cmdutils.FlagError{Err: errors.New("Scope is not required for group variables.")} + return + } + if runE != nil { err = runE(opts) return @@ -55,6 +63,7 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *GetOps) error) *cobra.Comman }, } + cmd.Flags().StringVarP(&opts.Scope, "scope", "s", "*", "The environment_scope of the variable. All (*), or specific environments.") cmd.Flags().StringVarP(&opts.Group, "group", "g", "", "Get variable for a group") return cmd } @@ -79,7 +88,7 @@ func getRun(opts *GetOps) error { return err } - variable, err := api.GetProjectVariable(httpClient, baseRepo.FullName(), opts.Key, nil) + variable, err := api.GetProjectVariable(httpClient, baseRepo.FullName(), opts.Key, opts.Scope) if err != nil { return err } diff --git a/docs/source/variable/get.md b/docs/source/variable/get.md index 17bd0114..b8704a43 100644 --- a/docs/source/variable/get.md +++ b/docs/source/variable/get.md @@ -22,6 +22,7 @@ glab variable get [flags] ```plaintext glab variable get VAR_KEY glab variable get -g GROUP VAR_KEY +glab variable get -s SCOPE VAR_KEY ``` @@ -29,6 +30,7 @@ glab variable get VAR_KEY ```plaintext -g, --group string Get variable for a group + -s, --scope string The environment_scope of the variable. All (*), or specific environments. (default "*") ``` ## Options inherited from parent commands