Skip to content

Commit a1c20f8

Browse files
committed
feat: alias env set and env unset as primary commands
1 parent 24c1d2c commit a1c20f8

File tree

9 files changed

+83
-49
lines changed

9 files changed

+83
-49
lines changed

cmd/env/env.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
3535
cmd := &cobra.Command{
3636
Use: "env <subcommand>",
3737
Aliases: []string{"var", "vars", "variable", "variables"},
38-
Short: "Add, remove, or list environment variables",
38+
Short: "Set, unset, or list environment variables",
3939
Long: strings.Join([]string{
40-
"Add, remove, or list environment variables for the app.",
40+
"Set, unset, or list environment variables for the app.",
4141
"",
4242
"Commands that run in the context of a project source environment variables from",
4343
"the \".env\" file. This includes the \"run\" command.",
@@ -49,16 +49,16 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
4949
}, "\n"),
5050
Example: style.ExampleCommandsf([]style.ExampleCommand{
5151
{
52-
Meaning: "Add an environment variable",
53-
Command: "env add MAGIC_PASSWORD abracadbra",
52+
Meaning: "Set an environment variable",
53+
Command: "env set MAGIC_PASSWORD abracadbra",
5454
},
5555
{
5656
Meaning: "List all environment variables",
5757
Command: "env list",
5858
},
5959
{
60-
Meaning: "Remove an environment variable",
61-
Command: "env remove MAGIC_PASSWORD",
60+
Meaning: "Unset an environment variable",
61+
Command: "env unset MAGIC_PASSWORD",
6262
},
6363
}),
6464
RunE: func(cmd *cobra.Command, args []string) error {
@@ -67,9 +67,9 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
6767
}
6868

6969
// Add child commands
70-
cmd.AddCommand(NewEnvAddCommand(clients))
70+
cmd.AddCommand(NewEnvSetCommand(clients))
7171
cmd.AddCommand(NewEnvListCommand(clients))
72-
cmd.AddCommand(NewEnvRemoveCommand(clients))
72+
cmd.AddCommand(NewEnvUnsetCommand(clients))
7373

7474
return cmd
7575
}

cmd/env/env_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ func Test_Env_Command(t *testing.T) {
3232
testutil.TableTestCommand(t, testutil.CommandTests{
3333
"shows the help page without commands or arguments or flags": {
3434
ExpectedStdoutOutputs: []string{
35-
"Add an environment variable",
35+
"Set an environment variable",
3636
"List all environment variables",
37-
"Remove an environment variable",
37+
"Unset an environment variable",
3838
},
3939
},
4040
}, func(clients *shared.ClientFactory) *cobra.Command {

cmd/env/add.go renamed to cmd/env/set.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ import (
3030
"github.com/spf13/cobra"
3131
)
3232

33-
func NewEnvAddCommand(clients *shared.ClientFactory) *cobra.Command {
33+
func NewEnvSetCommand(clients *shared.ClientFactory) *cobra.Command {
3434
cmd := &cobra.Command{
35-
Use: "add <name> [value] [flags]",
36-
Short: "Add an environment variable to the project",
35+
Use: "set <name> [value] [flags]",
36+
Aliases: []string{"add"},
37+
Short: "Set an environment variable for the project",
3738
Long: strings.Join([]string{
38-
"Add an environment variable to the project.",
39+
"Set an environment variable for the project.",
3940
"",
4041
"If a name or value is not provided, you will be prompted to provide these.",
4142
"",
@@ -48,24 +49,24 @@ func NewEnvAddCommand(clients *shared.ClientFactory) *cobra.Command {
4849
Example: style.ExampleCommandsf([]style.ExampleCommand{
4950
{
5051
Meaning: "Prompt for an environment variable",
51-
Command: "env add",
52+
Command: "env set",
5253
},
5354
{
54-
Meaning: "Add an environment variable",
55-
Command: "env add MAGIC_PASSWORD abracadbra",
55+
Meaning: "Set an environment variable",
56+
Command: "env set MAGIC_PASSWORD abracadbra",
5657
},
5758
{
5859
Meaning: "Prompt for an environment variable value",
59-
Command: "env add SECRET_PASSWORD",
60+
Command: "env set SECRET_PASSWORD",
6061
},
6162
}),
6263
Args: cobra.MaximumNArgs(2),
6364
PreRunE: func(cmd *cobra.Command, args []string) error {
6465
ctx := cmd.Context()
65-
return preRunEnvAddCommandFunc(ctx, clients, cmd)
66+
return preRunEnvSetCommandFunc(ctx, clients, cmd)
6667
},
6768
RunE: func(cmd *cobra.Command, args []string) error {
68-
return runEnvAddCommandFunc(clients, cmd, args)
69+
return runEnvSetCommandFunc(clients, cmd, args)
6970
},
7071
}
7172

@@ -74,15 +75,15 @@ func NewEnvAddCommand(clients *shared.ClientFactory) *cobra.Command {
7475
return cmd
7576
}
7677

77-
// preRunEnvAddCommandFunc determines if the command is run in a valid project
78+
// preRunEnvSetCommandFunc determines if the command is run in a valid project
7879
// and configures flags
79-
func preRunEnvAddCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
80+
func preRunEnvSetCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
8081
clients.Config.SetFlags(cmd)
8182
return cmdutil.IsValidProjectDirectory(clients)
8283
}
8384

84-
// runEnvAddCommandFunc sets an app environment variable to given values
85-
func runEnvAddCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error {
85+
// runEnvSetCommandFunc sets an app environment variable to given values
86+
func runEnvSetCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error {
8687
ctx := cmd.Context()
8788

8889
// Hosted apps require selecting an app before gathering variable inputs.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func Test_Env_AddCommandPreRun(t *testing.T) {
6565
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(cf *shared.ClientFactory) {
6666
cf.SDKConfig.WorkingDirectory = tc.mockWorkingDirectory
6767
})
68-
cmd := NewEnvAddCommand(clients)
68+
cmd := NewEnvSetCommand(clients)
6969
err := cmd.PreRunE(cmd, nil)
7070
if tc.expectedError != nil {
7171
assert.Equal(t, slackerror.ToSlackError(tc.expectedError).Code, slackerror.ToSlackError(err).Code)
@@ -258,7 +258,7 @@ func Test_Env_AddCommand(t *testing.T) {
258258
},
259259
},
260260
}, func(cf *shared.ClientFactory) *cobra.Command {
261-
cmd := NewEnvAddCommand(cf)
261+
cmd := NewEnvSetCommand(cf)
262262
cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return nil }
263263
return cmd
264264
})
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ import (
2828
"github.com/spf13/cobra"
2929
)
3030

31-
func NewEnvRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
31+
func NewEnvUnsetCommand(clients *shared.ClientFactory) *cobra.Command {
3232
cmd := &cobra.Command{
33-
Use: "remove <name> [flags]",
34-
Short: "Remove an environment variable from the app",
33+
Use: "unset <name> [flags]",
34+
Aliases: []string{"remove"},
35+
Short: "Unset an environment variable from the app",
3536
Long: strings.Join([]string{
36-
"Remove an environment variable from an app deployed to Slack managed",
37+
"Unset an environment variable from an app deployed to Slack managed",
3738
"infrastructure.",
3839
"",
3940
"If no variable name is provided, you will be prompted to select one.",
@@ -43,21 +44,21 @@ func NewEnvRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
4344
}, "\n"),
4445
Example: style.ExampleCommandsf([]style.ExampleCommand{
4546
{
46-
Meaning: "Select an environment variable to remove",
47-
Command: "env remove",
47+
Meaning: "Select an environment variable to unset",
48+
Command: "env unset",
4849
},
4950
{
50-
Meaning: "Remove an environment variable",
51-
Command: "env remove MAGIC_PASSWORD",
51+
Meaning: "Unset an environment variable",
52+
Command: "env unset MAGIC_PASSWORD",
5253
},
5354
}),
5455
Args: cobra.MaximumNArgs(1),
5556
PreRunE: func(cmd *cobra.Command, args []string) error {
5657
ctx := cmd.Context()
57-
return preRunEnvRemoveCommandFunc(ctx, clients, cmd)
58+
return preRunEnvUnsetCommandFunc(ctx, clients, cmd)
5859
},
5960
RunE: func(cmd *cobra.Command, args []string) error {
60-
return runEnvRemoveCommandFunc(clients, cmd, args)
61+
return runEnvUnsetCommandFunc(clients, cmd, args)
6162
},
6263
}
6364

@@ -66,9 +67,9 @@ func NewEnvRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
6667
return cmd
6768
}
6869

69-
// preRunEnvRemoveCommandFunc determines if the command is supported for a project
70+
// preRunEnvUnsetCommandFunc determines if the command is supported for a project
7071
// and configures flags
71-
func preRunEnvRemoveCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
72+
func preRunEnvUnsetCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
7273
clients.Config.SetFlags(cmd)
7374
err := cmdutil.IsValidProjectDirectory(clients)
7475
if err != nil {
@@ -80,8 +81,8 @@ func preRunEnvRemoveCommandFunc(ctx context.Context, clients *shared.ClientFacto
8081
return cmdutil.IsSlackHostedProject(ctx, clients)
8182
}
8283

83-
// runEnvRemoveCommandFunc removes an environment variable from an app
84-
func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error {
84+
// runEnvUnsetCommandFunc removes an environment variable from an app
85+
func runEnvUnsetCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error {
8586
var ctx = cmd.Context()
8687

8788
// Get the workspace from the flag or prompt
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func Test_Env_RemoveCommandPreRun(t *testing.T) {
118118
cf.Config.ForceFlag = tc.mockFlagForce
119119
cf.SDKConfig.WorkingDirectory = tc.mockWorkingDirectory
120120
})
121-
cmd := NewEnvRemoveCommand(clients)
121+
cmd := NewEnvUnsetCommand(clients)
122122
err := cmd.PreRunE(cmd, nil)
123123
if tc.expectedError != nil {
124124
assert.Equal(t, slackerror.ToSlackError(tc.expectedError).Code, slackerror.ToSlackError(err).Code)
@@ -270,7 +270,7 @@ func Test_Env_RemoveCommand(t *testing.T) {
270270
},
271271
},
272272
}, func(cf *shared.ClientFactory) *cobra.Command {
273-
cmd := NewEnvRemoveCommand(cf)
273+
cmd := NewEnvUnsetCommand(cf)
274274
cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return nil }
275275
return cmd
276276
})

docs/guides/using-environment-variables-with-the-slack-cli.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ While the `.env` file should **never** be committed to source control for securi
2626

2727
### Storing deployed environment variables {#deployed-env-vars}
2828

29-
When your app is [deployed](/tools/deno-slack-sdk/guides/deploying-to-slack), it will no longer use the `.env` file. Instead, you will have to add the environment variables using the [`env add`](/tools/slack-cli/reference/commands/slack_env_add) command. Environment variables added with `env add` will be made available to your deployed app's [custom functions](/tools/deno-slack-sdk/guides/creating-custom-functions) just as they are locally; see examples in the next section.
29+
When your app is [deployed](/tools/deno-slack-sdk/guides/deploying-to-slack), it will no longer use the `.env` file. Instead, you will have to add the environment variables using the [`env set`](/tools/slack-cli/reference/commands/slack_env_set) command. Environment variables added with `env set` will be made available to your deployed app's [custom functions](/tools/deno-slack-sdk/guides/creating-custom-functions) just as they are locally; see examples in the next section.
3030

3131
For the above example, we could run the following command before deploying our app:
3232

3333
```zsh
34-
slack env add MY_ENV_VAR asdf1234
34+
slack env set MY_ENV_VAR asdf1234
3535
```
3636

3737
If your token contains non-alphanumeric characters, wrap it in quotes like this:
3838

3939
```zsh
40-
slack env add SLACK_API_URL "https://dev<yournumber>.slack.com/api/"
40+
slack env set SLACK_API_URL "https://dev<yournumber>.slack.com/api/"
4141
```
4242

4343
Your environment variables are always encrypted before being stored on our servers and will be automatically decrypted when you use them&mdash;including when listing environment variables with `slack env list`.
@@ -145,14 +145,14 @@ SLACK_DEBUG=true
145145
For deployed apps, run the following command before deployment:
146146

147147
```
148-
slack env add SLACK_DEBUG true
148+
slack env set SLACK_DEBUG true
149149
```
150150

151151
## Included local and deployed variables {#included}
152152

153153
Slack provides two environment variables by default, `SLACK_WORKSPACE` and `SLACK_ENV`. The workspace name is specified by `SLACK_WORKSPACE` and `SLACK_ENV` provides a distinction between the `local` and `deployed` app. Use these values if you want to have different values based on the workspace or environment that the app is installed in.
154154

155-
These variables are automatically included when generating the manifest or triggers only. For access from within a custom function, these variables can be set from the `.env` file or with the [`env add`](/tools/slack-cli/reference/commands/slack_env_add) command.
155+
These variables are automatically included when generating the manifest or triggers only. For access from within a custom function, these variables can be set from the `.env` file or with the [`env set`](/tools/slack-cli/reference/commands/slack_env_set) command.
156156

157157
A custom `WorkspaceMapSchema` can be created and used with these variables to decide which values to use for certain instances of an app. This can be used as an alternative to a local `.env` file or in conjunction with it. The following snippet works well for inclusion in your app manifest, or for triggers (for example, to change event trigger channel IDs):
158158

internal/goutils/strings.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func RedactPII(s string) string {
9494
regexp.MustCompile(`(?P<keys>(?:\w)*client_id(?:\=s*))(?P<values>(([\w\s.-]*)))`),
9595
// Escape variables for 3p auth add client secrets
9696
regexp.MustCompile(`(?P<keys>(?:\w)*secret(?:\ s*))(?P<values>(([\w\s.-]*)))`),
97-
// Escape variables for env and it's alias for add/remove command
98-
regexp.MustCompile(`(?P<keys>(?:\w)*(env|var|vars|variable|variables|auth) (add|remove)(?:\ s*))(?P<values>(([\w\s.-]*)))`),
97+
// Escape variables for env and its aliases for set/unset/add/remove commands
98+
regexp.MustCompile(`(?P<keys>(?:\w)*(env|var|vars|variable|variables|auth) (set|unset|add|remove)(?:\ s*))(?P<values>(([\w\s.-]*)))`),
9999
// Add more regex here
100100
}
101101
// regexListWithQuotes will find sensitive data within quotes and later we escape with "..."

internal/goutils/strings_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ func Test_RedactPII(t *testing.T) {
292292
text: `slack var remove topsecret 123`,
293293
expected: `slack var remove ...`,
294294
},
295+
"Escape from `Command` for var set": {
296+
text: `slack var set topsecret 123`,
297+
expected: `slack var set ...`,
298+
},
299+
"Escape from `Command` for var unset": {
300+
text: `slack var unset topsecret 123`,
301+
expected: `slack var unset ...`,
302+
},
295303
"Escape from `Command` for env add": {
296304
text: `slack env add topsecret 123`,
297305
expected: `slack env add ...`,
@@ -300,6 +308,14 @@ func Test_RedactPII(t *testing.T) {
300308
text: `slack env remove topsecret 123`,
301309
expected: `slack env remove ...`,
302310
},
311+
"Escape from `Command` for env set": {
312+
text: `slack env set topsecret 123`,
313+
expected: `slack env set ...`,
314+
},
315+
"Escape from `Command` for env unset": {
316+
text: `slack env unset topsecret 123`,
317+
expected: `slack env unset ...`,
318+
},
303319
"Escape from `Command` for vars add": {
304320
text: `slack vars add topsecret 123`,
305321
expected: `slack vars add ...`,
@@ -308,6 +324,14 @@ func Test_RedactPII(t *testing.T) {
308324
text: `slack vars remove topsecret 123`,
309325
expected: `slack vars remove ...`,
310326
},
327+
"Escape from `Command` for vars set": {
328+
text: `slack vars set topsecret 123`,
329+
expected: `slack vars set ...`,
330+
},
331+
"Escape from `Command` for vars unset": {
332+
text: `slack vars unset topsecret 123`,
333+
expected: `slack vars unset ...`,
334+
},
311335
"Escape from `Command` for variables add": {
312336
text: `slack variables add topsecret 123`,
313337
expected: `slack variables add ...`,
@@ -316,6 +340,14 @@ func Test_RedactPII(t *testing.T) {
316340
text: `slack variables remove topsecret 123`,
317341
expected: `slack variables remove ...`,
318342
},
343+
"Escape from `Command` for variables set": {
344+
text: `slack variables set topsecret 123`,
345+
expected: `slack variables set ...`,
346+
},
347+
"Escape from `Command` for variables unset": {
348+
text: `slack variables unset topsecret 123`,
349+
expected: `slack variables unset ...`,
350+
},
319351
}
320352
for name, tc := range tests {
321353
t.Run(name, func(t *testing.T) {

0 commit comments

Comments
 (0)