mirror of https://gitlab.com/gitlab-org/cli.git
feat(set): add raw flag for set variable command
This commit is contained in:
parent
a3b4e0ad26
commit
e2805bcae5
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/MakeNowJust/heredoc"
|
"github.com/MakeNowJust/heredoc"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/xanzy/go-gitlab"
|
"github.com/xanzy/go-gitlab"
|
||||||
|
|
||||||
"gitlab.com/gitlab-org/cli/api"
|
"gitlab.com/gitlab-org/cli/api"
|
||||||
"gitlab.com/gitlab-org/cli/commands/cmdutils"
|
"gitlab.com/gitlab-org/cli/commands/cmdutils"
|
||||||
"gitlab.com/gitlab-org/cli/commands/flag"
|
"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",
|
Short: "List project or group variables",
|
||||||
Aliases: []string{"ls"},
|
Aliases: []string{"ls"},
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(
|
||||||
|
`
|
||||||
glab variable list
|
glab variable list
|
||||||
`),
|
`,
|
||||||
|
),
|
||||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||||
// Supports repo override
|
// Supports repo override
|
||||||
opts.HTTPClient = f.HttpClient
|
opts.HTTPClient = f.HttpClient
|
||||||
|
@ -55,7 +58,12 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *ListOpts) error) *cobra.Comm
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdutils.EnableRepoOverride(cmd, f)
|
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
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -73,7 +81,7 @@ func listRun(opts *ListOpts) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
table := tableprinter.NewTablePrinter()
|
table := tableprinter.NewTablePrinter()
|
||||||
table.AddRow("KEY", "PROTECTED", "MASKED", "SCOPE")
|
table.AddRow("KEY", "PROTECTED", "MASKED", "EXPANDED", "SCOPE")
|
||||||
|
|
||||||
if opts.Group != "" {
|
if opts.Group != "" {
|
||||||
opts.IO.Logf("Listing variables for the %s group:\n\n", color.Bold(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
|
return err
|
||||||
}
|
}
|
||||||
for _, variable := range variables {
|
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 {
|
} else {
|
||||||
opts.IO.Logf("Listing variables for the %s project:\n\n", color.Bold(repo.FullName()))
|
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
|
return err
|
||||||
}
|
}
|
||||||
for _, variable := range variables {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ type SetOpts struct {
|
||||||
Scope string
|
Scope string
|
||||||
Protected bool
|
Protected bool
|
||||||
Masked bool
|
Masked bool
|
||||||
|
Raw bool
|
||||||
Group string
|
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.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().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.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")
|
cmd.Flags().BoolVarP(&opts.Protected, "protected", "p", false, "Whether the variable is protected")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -111,6 +113,7 @@ func setRun(opts *SetOpts) error {
|
||||||
Masked: gitlab.Bool(opts.Masked),
|
Masked: gitlab.Bool(opts.Masked),
|
||||||
Protected: gitlab.Bool(opts.Protected),
|
Protected: gitlab.Bool(opts.Protected),
|
||||||
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
||||||
|
Raw: gitlab.Bool(opts.Raw),
|
||||||
}
|
}
|
||||||
_, err = api.CreateGroupVariable(httpClient, opts.Group, createVarOpts)
|
_, err = api.CreateGroupVariable(httpClient, opts.Group, createVarOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -133,6 +136,7 @@ func setRun(opts *SetOpts) error {
|
||||||
Masked: gitlab.Bool(opts.Masked),
|
Masked: gitlab.Bool(opts.Masked),
|
||||||
Protected: gitlab.Bool(opts.Protected),
|
Protected: gitlab.Bool(opts.Protected),
|
||||||
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
||||||
|
Raw: gitlab.Bool(opts.Raw),
|
||||||
}
|
}
|
||||||
_, err = api.CreateProjectVariable(httpClient, baseRepo.FullName(), createVarOpts)
|
_, err = api.CreateProjectVariable(httpClient, baseRepo.FullName(), createVarOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -90,6 +90,36 @@ func Test_NewCmdSet(t *testing.T) {
|
||||||
Group: "coolGroup",
|
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 {
|
for _, tt := range tests {
|
||||||
|
@ -140,6 +170,7 @@ func Test_setRun_project(t *testing.T) {
|
||||||
"variable_type": "env_var",
|
"variable_type": "env_var",
|
||||||
"protected": false,
|
"protected": false,
|
||||||
"masked": false,
|
"masked": false,
|
||||||
|
"raw": false,
|
||||||
"scope": "production"
|
"scope": "production"
|
||||||
}
|
}
|
||||||
`),
|
`),
|
||||||
|
@ -179,6 +210,7 @@ func Test_setRun_group(t *testing.T) {
|
||||||
"variable_type": "env_var",
|
"variable_type": "env_var",
|
||||||
"protected": false,
|
"protected": false,
|
||||||
"masked": false,
|
"masked": false,
|
||||||
|
"raw": false,
|
||||||
"scope": "production"
|
"scope": "production"
|
||||||
}
|
}
|
||||||
`),
|
`),
|
||||||
|
|
|
@ -26,6 +26,7 @@ type UpdateOpts struct {
|
||||||
Scope string
|
Scope string
|
||||||
Protected bool
|
Protected bool
|
||||||
Masked bool
|
Masked bool
|
||||||
|
Raw bool
|
||||||
Group string
|
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.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().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.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")
|
cmd.Flags().BoolVarP(&opts.Protected, "protected", "p", false, "Whether the variable is protected")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -118,6 +120,7 @@ func updateRun(opts *UpdateOpts) error {
|
||||||
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
||||||
Masked: gitlab.Bool(opts.Masked),
|
Masked: gitlab.Bool(opts.Masked),
|
||||||
Protected: gitlab.Bool(opts.Protected),
|
Protected: gitlab.Bool(opts.Protected),
|
||||||
|
Raw: gitlab.Bool(opts.Raw),
|
||||||
EnvironmentScope: gitlab.String(opts.Scope),
|
EnvironmentScope: gitlab.String(opts.Scope),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +143,7 @@ func updateRun(opts *UpdateOpts) error {
|
||||||
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
VariableType: gitlab.VariableType(gitlab.VariableTypeValue(opts.Type)),
|
||||||
Masked: gitlab.Bool(opts.Masked),
|
Masked: gitlab.Bool(opts.Masked),
|
||||||
Protected: gitlab.Bool(opts.Protected),
|
Protected: gitlab.Bool(opts.Protected),
|
||||||
|
Raw: gitlab.Bool(opts.Raw),
|
||||||
EnvironmentScope: gitlab.String(opts.Scope),
|
EnvironmentScope: gitlab.String(opts.Scope),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,6 +65,37 @@ func Test_NewCmdSet(t *testing.T) {
|
||||||
Group: "coolGroup",
|
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",
|
name: "leading numbers in name",
|
||||||
cli: `123_TOKEN -v"cool"`,
|
cli: `123_TOKEN -v"cool"`,
|
||||||
|
|
|
@ -43,6 +43,7 @@ cat token.txt | glab variable set GROUP_TOKEN -g mygroup --scope=prod
|
||||||
-g, --group string Set variable for a group
|
-g, --group string Set variable for a group
|
||||||
-m, --masked Whether the variable is masked
|
-m, --masked Whether the variable is masked
|
||||||
-p, --protected Whether the variable is protected
|
-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 "*")
|
-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")
|
-t, --type string The type of a variable: {env_var|file} (default "env_var")
|
||||||
-v, --value string The value of a variable
|
-v, --value string The value of a variable
|
||||||
|
|
|
@ -36,6 +36,7 @@ cat token.txt | glab variable update GROUP_TOKEN -g mygroup --scope=prod
|
||||||
-g, --group string Set variable for a group
|
-g, --group string Set variable for a group
|
||||||
-m, --masked Whether the variable is masked
|
-m, --masked Whether the variable is masked
|
||||||
-p, --protected Whether the variable is protected
|
-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 "*")
|
-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")
|
-t, --type string The type of a variable: {env_var|file} (default "env_var")
|
||||||
-v, --value string The value of a variable
|
-v, --value string The value of a variable
|
||||||
|
|
Loading…
Reference in New Issue