feat(variable get): Add scope parameter to variable get subcommand

This commit is contained in:
Jeroen Huinink 2023-01-18 18:18:24 +00:00 committed by Gary Holtz
parent d6c59371b2
commit 4b1948fe3f
3 changed files with 18 additions and 3 deletions

View File

@ -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

View File

@ -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
}

View File

@ -22,6 +22,7 @@ glab variable get <key> [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