feat(set): add raw flag for set variable command

This commit is contained in:
Mat Gautron 2023-11-01 17:24:43 +00:00 committed by Gary Holtz
parent a3b4e0ad26
commit e2805bcae5
7 changed files with 87 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/commands/flag"
@ -31,9 +32,11 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *ListOpts) error) *cobra.Comm
Short: "List project or group variables",
Aliases: []string{"ls"},
Args: cobra.ExactArgs(0),
Example: heredoc.Doc(`
Example: heredoc.Doc(
`
glab variable list
`),
`,
),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Supports repo override
opts.HTTPClient = f.HttpClient
@ -55,7 +58,12 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *ListOpts) error) *cobra.Comm
}
cmdutils.EnableRepoOverride(cmd, f)
cmd.PersistentFlags().StringP("group", "g", "", "Select a group/subgroup. This option is ignored if a repo argument is set.")
cmd.PersistentFlags().StringP(
"group",
"g",
"",
"Select a group/subgroup. This option is ignored if a repo argument is set.",
)
return cmd
}
@ -73,7 +81,7 @@ func listRun(opts *ListOpts) error {
}
table := tableprinter.NewTablePrinter()
table.AddRow("KEY", "PROTECTED", "MASKED", "SCOPE")
table.AddRow("KEY", "PROTECTED", "MASKED", "EXPANDED", "SCOPE")
if opts.Group != "" {
opts.IO.Logf("Listing variables for the %s group:\n\n", color.Bold(opts.Group))
@ -83,7 +91,7 @@ func listRun(opts *ListOpts) error {
return err
}
for _, variable := range variables {
table.AddRow(variable.Key, variable.Protected, variable.Masked, variable.EnvironmentScope)
table.AddRow(variable.Key, variable.Protected, variable.Masked, !variable.Raw, variable.EnvironmentScope)
}
} else {
opts.IO.Logf("Listing variables for the %s project:\n\n", color.Bold(repo.FullName()))
@ -93,7 +101,7 @@ func listRun(opts *ListOpts) error {
return err
}
for _, variable := range variables {
table.AddRow(variable.Key, variable.Protected, variable.Masked, variable.EnvironmentScope)
table.AddRow(variable.Key, variable.Protected, variable.Masked, !variable.Raw, variable.EnvironmentScope)
}
}

View File

@ -26,6 +26,7 @@ type SetOpts struct {
Scope string
Protected bool
Masked bool
Raw bool
Group string
}
@ -91,6 +92,7 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *SetOpts) error) *cobra.Comma
cmd.Flags().StringVarP(&opts.Scope, "scope", "s", "*", "The environment_scope of the variable. All (*), or specific environments")
cmd.Flags().StringVarP(&opts.Group, "group", "g", "", "Set variable for a group")
cmd.Flags().BoolVarP(&opts.Masked, "masked", "m", false, "Whether the variable is masked")
cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "Whether the variable is treated as a raw string")
cmd.Flags().BoolVarP(&opts.Protected, "protected", "p", false, "Whether the variable is protected")
return cmd
}
@ -111,6 +113,7 @@ func setRun(opts *SetOpts) error {
Masked: gitlab.Bool(opts.Masked),
Protected: gitlab.Bool(opts.Protected),
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
Raw: gitlab.Bool(opts.Raw),
}
_, err = api.CreateGroupVariable(httpClient, opts.Group, createVarOpts)
if err != nil {
@ -133,6 +136,7 @@ func setRun(opts *SetOpts) error {
Masked: gitlab.Bool(opts.Masked),
Protected: gitlab.Bool(opts.Protected),
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
Raw: gitlab.Bool(opts.Raw),
}
_, err = api.CreateProjectVariable(httpClient, baseRepo.FullName(), createVarOpts)
if err != nil {

View File

@ -90,6 +90,36 @@ func Test_NewCmdSet(t *testing.T) {
Group: "coolGroup",
},
},
{
name: "raw variable with flag",
cli: `cool_secret -r -v"$variable_name"`,
wants: SetOpts{
Key: "cool_secret",
Value: "$variable_name",
Raw: true,
Group: "",
},
},
{
name: "raw variable with flag in group",
cli: `cool_secret -r --group coolGroup -v"$variable_name"`,
wants: SetOpts{
Key: "cool_secret",
Value: "$variable_name",
Raw: true,
Group: "coolGroup",
},
},
{
name: "raw is false by default",
cli: `cool_secret -v"$variable_name"`,
wants: SetOpts{
Key: "cool_secret",
Value: "$variable_name",
Raw: false,
Group: "",
},
},
}
for _, tt := range tests {
@ -140,6 +170,7 @@ func Test_setRun_project(t *testing.T) {
"variable_type": "env_var",
"protected": false,
"masked": false,
"raw": false,
"scope": "production"
}
`),
@ -179,6 +210,7 @@ func Test_setRun_group(t *testing.T) {
"variable_type": "env_var",
"protected": false,
"masked": false,
"raw": false,
"scope": "production"
}
`),

View File

@ -26,6 +26,7 @@ type UpdateOpts struct {
Scope string
Protected bool
Masked bool
Raw bool
Group string
}
@ -95,6 +96,7 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *UpdateOpts) error) *cobra.Co
cmd.Flags().StringVarP(&opts.Scope, "scope", "s", "*", "The environment_scope of the variable. All (*), or specific environments")
cmd.Flags().StringVarP(&opts.Group, "group", "g", "", "Set variable for a group")
cmd.Flags().BoolVarP(&opts.Masked, "masked", "m", false, "Whether the variable is masked")
cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "Whether the variable is treated as a raw string")
cmd.Flags().BoolVarP(&opts.Protected, "protected", "p", false, "Whether the variable is protected")
return cmd
}
@ -118,6 +120,7 @@ func updateRun(opts *UpdateOpts) error {
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
Masked: gitlab.Bool(opts.Masked),
Protected: gitlab.Bool(opts.Protected),
Raw: gitlab.Bool(opts.Raw),
EnvironmentScope: gitlab.String(opts.Scope),
}
@ -140,6 +143,7 @@ func updateRun(opts *UpdateOpts) error {
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
Masked: gitlab.Bool(opts.Masked),
Protected: gitlab.Bool(opts.Protected),
Raw: gitlab.Bool(opts.Raw),
EnvironmentScope: gitlab.String(opts.Scope),
}

View File

@ -65,6 +65,37 @@ func Test_NewCmdSet(t *testing.T) {
Group: "coolGroup",
},
},
{
name: "raw variable with flag",
cli: `cool_secret -r -v"$variable_name"`,
wants: UpdateOpts{
Key: "cool_secret",
Value: "$variable_name",
Raw: true,
Group: "",
},
},
{
name: "raw variable with flag in group",
cli: `cool_secret -r --group coolGroup -v"$variable_name"`,
wants: UpdateOpts{
Key: "cool_secret",
Value: "$variable_name",
Raw: true,
Group: "coolGroup",
},
},
{
name: "raw is false by default",
cli: `cool_secret -v"$variable_name"`,
wants: UpdateOpts{
Key: "cool_secret",
Value: "$variable_name",
Raw: false,
Group: "",
},
},
{
name: "leading numbers in name",
cli: `123_TOKEN -v"cool"`,

View File

@ -43,6 +43,7 @@ cat token.txt | glab variable set GROUP_TOKEN -g mygroup --scope=prod
-g, --group string Set variable for a group
-m, --masked Whether the variable is masked
-p, --protected Whether the variable is protected
-r, --raw Whether the variable is treated as a raw string
-s, --scope string The environment_scope of the variable. All (*), or specific environments (default "*")
-t, --type string The type of a variable: {env_var|file} (default "env_var")
-v, --value string The value of a variable

View File

@ -36,6 +36,7 @@ cat token.txt | glab variable update GROUP_TOKEN -g mygroup --scope=prod
-g, --group string Set variable for a group
-m, --masked Whether the variable is masked
-p, --protected Whether the variable is protected
-r, --raw Whether the variable is treated as a raw string
-s, --scope string The environment_scope of the variable. All (*), or specific environments (default "*")
-t, --type string The type of a variable: {env_var|file} (default "env_var")
-v, --value string The value of a variable