diff --git a/commands/variable/list/list.go b/commands/variable/list/list.go index d0ae4b2e..4ae0a348 100644 --- a/commands/variable/list/list.go +++ b/commands/variable/list/list.go @@ -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) } } diff --git a/commands/variable/set/set.go b/commands/variable/set/set.go index be7a6a8f..d576f74f 100644 --- a/commands/variable/set/set.go +++ b/commands/variable/set/set.go @@ -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 { diff --git a/commands/variable/set/set_test.go b/commands/variable/set/set_test.go index d97493ca..12fa0237 100644 --- a/commands/variable/set/set_test.go +++ b/commands/variable/set/set_test.go @@ -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" } `), diff --git a/commands/variable/update/update.go b/commands/variable/update/update.go index 670af28a..bc6c1f14 100644 --- a/commands/variable/update/update.go +++ b/commands/variable/update/update.go @@ -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), } diff --git a/commands/variable/update/update_test.go b/commands/variable/update/update_test.go index 258a2a36..053076f0 100644 --- a/commands/variable/update/update_test.go +++ b/commands/variable/update/update_test.go @@ -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"`, diff --git a/docs/source/variable/set.md b/docs/source/variable/set.md index 6f402181..788c1705 100644 --- a/docs/source/variable/set.md +++ b/docs/source/variable/set.md @@ -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 diff --git a/docs/source/variable/update.md b/docs/source/variable/update.md index 23ab584c..d45fc5ec 100644 --- a/docs/source/variable/update.md +++ b/docs/source/variable/update.md @@ -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