Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cmd/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "env <subcommand>",
Aliases: []string{"var", "vars", "variable", "variables"},
Short: "Add, remove, or list environment variables",
Short: "Set, unset, or list environment variables",
Long: strings.Join([]string{
"Add, remove, or list environment variables for the app.",
"Set, unset, or list environment variables for the project.",
"",
"Commands that run in the context of a project source environment variables from",
"the \".env\" file. This includes the \"run\" command.",
Expand All @@ -49,16 +49,16 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
}, "\n"),
Example: style.ExampleCommandsf([]style.ExampleCommand{
{
Meaning: "Add an environment variable",
Command: "env add MAGIC_PASSWORD abracadbra",
Meaning: "Set an environment variable",
Command: "env set MAGIC_PASSWORD abracadbra",
},
{
Meaning: "List all environment variables",
Command: "env list",
},
{
Meaning: "Remove an environment variable",
Command: "env remove MAGIC_PASSWORD",
Meaning: "Unset an environment variable",
Command: "env unset MAGIC_PASSWORD",
},
}),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -67,9 +67,9 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
}

// Add child commands
cmd.AddCommand(NewEnvAddCommand(clients))
cmd.AddCommand(NewEnvSetCommand(clients))
cmd.AddCommand(NewEnvListCommand(clients))
cmd.AddCommand(NewEnvRemoveCommand(clients))
cmd.AddCommand(NewEnvUnsetCommand(clients))

return cmd
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func Test_Env_Command(t *testing.T) {
testutil.TableTestCommand(t, testutil.CommandTests{
"shows the help page without commands or arguments or flags": {
ExpectedStdoutOutputs: []string{
"Add an environment variable",
"Set an environment variable",
"List all environment variables",
"Remove an environment variable",
"Unset an environment variable",
},
},
}, func(clients *shared.ClientFactory) *cobra.Command {
Expand Down
4 changes: 2 additions & 2 deletions cmd/env/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
func NewEnvListCommand(clients *shared.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "list [flags]",
Short: "List all environment variables for the app",
Short: "List all environment variables of the project",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Nice catch!

Long: strings.Join([]string{
"List environment variables available to the app at runtime.",
"List the environment variables available to the project.",
"",
"Commands that run in the context of a project source environment variables from",
"the \".env\" file. This includes the \"run\" command.",
Expand Down
33 changes: 17 additions & 16 deletions cmd/env/add.go → cmd/env/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import (
"github.com/spf13/cobra"
)

func NewEnvAddCommand(clients *shared.ClientFactory) *cobra.Command {
func NewEnvSetCommand(clients *shared.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "add <name> [value] [flags]",
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔭 note: While this command is changed we also prefer the square brackets with follow up in #459 since the "name" argument has a form fallback-

Short: "Add an environment variable to the project",
Use: "set [name] [value] [flags]",
Aliases: []string{"add"},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Nice to see add remains an alias for backwards compatibility.

Short: "Set an environment variable for the project",
Long: strings.Join([]string{
"Add an environment variable to the project.",
"Set an environment variable for the project.",
"",
"If a name or value is not provided, you will be prompted to provide these.",
"",
Expand All @@ -48,24 +49,24 @@ func NewEnvAddCommand(clients *shared.ClientFactory) *cobra.Command {
Example: style.ExampleCommandsf([]style.ExampleCommand{
{
Meaning: "Prompt for an environment variable",
Command: "env add",
Command: "env set",
},
{
Meaning: "Add an environment variable",
Command: "env add MAGIC_PASSWORD abracadbra",
Meaning: "Set an environment variable",
Command: "env set MAGIC_PASSWORD abracadbra",
},
{
Meaning: "Prompt for an environment variable value",
Command: "env add SECRET_PASSWORD",
Command: "env set SECRET_PASSWORD",
},
}),
Args: cobra.MaximumNArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
return preRunEnvAddCommandFunc(ctx, clients, cmd)
return preRunEnvSetCommandFunc(ctx, clients, cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runEnvAddCommandFunc(clients, cmd, args)
return runEnvSetCommandFunc(clients, cmd, args)
},
}

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

// preRunEnvAddCommandFunc determines if the command is run in a valid project
// preRunEnvSetCommandFunc determines if the command is run in a valid project
// and configures flags
func preRunEnvAddCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
func preRunEnvSetCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
clients.Config.SetFlags(cmd)
return cmdutil.IsValidProjectDirectory(clients)
}

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

// Hosted apps require selecting an app before gathering variable inputs.
Expand Down Expand Up @@ -137,7 +138,7 @@ func runEnvAddCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, arg
if err != nil {
return err
}
clients.IO.PrintTrace(ctx, slacktrace.EnvAddSuccess)
clients.IO.PrintTrace(ctx, slacktrace.EnvSetSuccess)
clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
Emoji: "evergreen_tree",
Text: "App Environment",
Expand All @@ -154,7 +155,7 @@ func runEnvAddCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, arg
if err != nil {
return err
}
clients.IO.PrintTrace(ctx, slacktrace.EnvAddSuccess)
clients.IO.PrintTrace(ctx, slacktrace.EnvSetSuccess)
var details []string
if !exists {
details = append(details, "Created a project .env file that shouldn't be added to version control")
Expand Down
6 changes: 3 additions & 3 deletions cmd/env/add_test.go → cmd/env/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Test_Env_AddCommandPreRun(t *testing.T) {
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(cf *shared.ClientFactory) {
cf.SDKConfig.WorkingDirectory = tc.mockWorkingDirectory
})
cmd := NewEnvAddCommand(clients)
cmd := NewEnvSetCommand(clients)
err := cmd.PreRunE(cmd, nil)
if tc.expectedError != nil {
assert.Equal(t, slackerror.ToSlackError(tc.expectedError).Code, slackerror.ToSlackError(err).Code)
Expand Down Expand Up @@ -97,7 +97,7 @@ func Test_Env_AddCommand(t *testing.T) {
t,
"PrintTrace",
mock.Anything,
slacktrace.EnvAddSuccess,
slacktrace.EnvSetSuccess,
mock.Anything,
)
},
Expand Down Expand Up @@ -258,7 +258,7 @@ func Test_Env_AddCommand(t *testing.T) {
},
},
}, func(cf *shared.ClientFactory) *cobra.Command {
cmd := NewEnvAddCommand(cf)
cmd := NewEnvSetCommand(cf)
cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return nil }
return cmd
})
Expand Down
37 changes: 19 additions & 18 deletions cmd/env/remove.go → cmd/env/unset.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking great!! i noticed there are still a few references to removing in the file 👁️‍🗨️
line 120 "Select a variable to remove"
line 150 "Successfully removed \"%s\" from the app's environment variables"
might be nice to update these too 🚀

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srtaalej Thanks for catching this! I saw a few comments you left similar in #461 and will save these changes for such 🎁

Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import (
"github.com/spf13/cobra"
)

func NewEnvRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
func NewEnvUnsetCommand(clients *shared.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "remove <name> [flags]",
Short: "Remove an environment variable from the project",
Use: "unset [name] [flags]",
Aliases: []string{"remove"},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Nice to see remove remains an alias for backwards compatibility.

Short: "Unset an environment variable from the project",
Long: strings.Join([]string{
"Remove an environment variable from the project.",
"Unset an environment variable from the project.",
"",
"If no variable name is provided, you will be prompted to select one.",
"",
Expand All @@ -47,21 +48,21 @@ func NewEnvRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
}, "\n"),
Example: style.ExampleCommandsf([]style.ExampleCommand{
{
Meaning: "Select an environment variable to remove",
Command: "env remove",
Meaning: "Select an environment variable to unset",
Command: "env unset",
},
{
Meaning: "Remove an environment variable",
Command: "env remove MAGIC_PASSWORD",
Meaning: "Unset an environment variable",
Command: "env unset MAGIC_PASSWORD",
},
}),
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
return preRunEnvRemoveCommandFunc(ctx, clients, cmd)
return preRunEnvUnsetCommandFunc(ctx, clients, cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runEnvRemoveCommandFunc(clients, cmd, args)
return runEnvUnsetCommandFunc(clients, cmd, args)
},
}

Expand All @@ -70,15 +71,15 @@ func NewEnvRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
return cmd
}

// preRunEnvRemoveCommandFunc determines if the command is run in a valid project
// preRunEnvUnsetCommandFunc determines if the command is run in a valid project
// and configures flags
func preRunEnvRemoveCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
func preRunEnvUnsetCommandFunc(ctx context.Context, clients *shared.ClientFactory, cmd *cobra.Command) error {
clients.Config.SetFlags(cmd)
return cmdutil.IsValidProjectDirectory(clients)
}

// runEnvRemoveCommandFunc removes an environment variable from an app
func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error {
// runEnvUnsetCommandFunc removes an environment variable from an app
func runEnvUnsetCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

// Hosted apps require selecting an app before gathering variable inputs.
Expand Down Expand Up @@ -106,7 +107,7 @@ func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command,
return err
}
if len(variables) <= 0 {
clients.IO.PrintTrace(ctx, slacktrace.EnvRemoveSuccess)
clients.IO.PrintTrace(ctx, slacktrace.EnvUnsetSuccess)
clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
Emoji: "evergreen_tree",
Text: "App Environment",
Expand Down Expand Up @@ -135,7 +136,7 @@ func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command,
return err
}
if len(dotEnv) <= 0 {
clients.IO.PrintTrace(ctx, slacktrace.EnvRemoveSuccess)
clients.IO.PrintTrace(ctx, slacktrace.EnvUnsetSuccess)
clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
Emoji: "evergreen_tree",
Text: "App Environment",
Expand Down Expand Up @@ -177,7 +178,7 @@ func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command,
if err != nil {
return err
}
clients.IO.PrintTrace(ctx, slacktrace.EnvRemoveSuccess)
clients.IO.PrintTrace(ctx, slacktrace.EnvUnsetSuccess)
clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
Emoji: "evergreen_tree",
Text: "App Environment",
Expand All @@ -190,7 +191,7 @@ func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command,
if err != nil {
return err
}
clients.IO.PrintTrace(ctx, slacktrace.EnvRemoveSuccess)
clients.IO.PrintTrace(ctx, slacktrace.EnvUnsetSuccess)
clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
Emoji: "evergreen_tree",
Text: "App Environment",
Expand Down
8 changes: 4 additions & 4 deletions cmd/env/remove_test.go → cmd/env/unset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Test_Env_RemoveCommandPreRun(t *testing.T) {
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(cf *shared.ClientFactory) {
cf.SDKConfig.WorkingDirectory = tc.mockWorkingDirectory
})
cmd := NewEnvRemoveCommand(clients)
cmd := NewEnvUnsetCommand(clients)
err := cmd.PreRunE(cmd, nil)
if tc.expectedError != nil {
assert.Equal(t, slackerror.ToSlackError(tc.expectedError).Code, slackerror.ToSlackError(err).Code)
Expand Down Expand Up @@ -115,7 +115,7 @@ func Test_Env_RemoveCommand(t *testing.T) {
t,
"PrintTrace",
mock.Anything,
slacktrace.EnvRemoveSuccess,
slacktrace.EnvUnsetSuccess,
mock.Anything,
)
},
Expand Down Expand Up @@ -172,7 +172,7 @@ func Test_Env_RemoveCommand(t *testing.T) {
t,
"PrintTrace",
mock.Anything,
slacktrace.EnvRemoveSuccess,
slacktrace.EnvUnsetSuccess,
mock.Anything,
)
content, err := afero.ReadFile(cm.Fs, ".env")
Expand Down Expand Up @@ -241,7 +241,7 @@ func Test_Env_RemoveCommand(t *testing.T) {
},
},
}, func(cf *shared.ClientFactory) *cobra.Command {
cmd := NewEnvRemoveCommand(cf)
cmd := NewEnvUnsetCommand(cf)
cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return nil }
return cmd
})
Expand Down
10 changes: 5 additions & 5 deletions docs/guides/using-environment-variables-with-the-slack-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ While the `.env` file should **never** be committed to source control for securi

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

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.
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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: The power of the docs being part of the repo. Thanks for making this sweeping change so smooth!


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

```zsh
slack env add MY_ENV_VAR asdf1234
slack env set MY_ENV_VAR asdf1234
```

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

```zsh
slack env add SLACK_API_URL "https://dev<yournumber>.slack.com/api/"
slack env set SLACK_API_URL "https://dev<yournumber>.slack.com/api/"
```

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`.
Expand Down Expand Up @@ -145,14 +145,14 @@ SLACK_DEBUG=true
For deployed apps, run the following command before deployment:

```
slack env add SLACK_DEBUG true
slack env set SLACK_DEBUG true
```

## Included local and deployed variables {#included}

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.

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

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):

Expand Down
4 changes: 2 additions & 2 deletions internal/goutils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func RedactPII(s string) string {
regexp.MustCompile(`(?P<keys>(?:\w)*client_id(?:\=s*))(?P<values>(([\w\s.-]*)))`),
// Escape variables for 3p auth add client secrets
regexp.MustCompile(`(?P<keys>(?:\w)*secret(?:\ s*))(?P<values>(([\w\s.-]*)))`),
// Escape variables for env and it's alias for add/remove command
regexp.MustCompile(`(?P<keys>(?:\w)*(env|var|vars|variable|variables|auth) (add|remove)(?:\ s*))(?P<values>(([\w\s.-]*)))`),
// Escape variables for env and its aliases for set/unset/add/remove commands
regexp.MustCompile(`(?P<keys>(?:\w)*(env|var|vars|variable|variables|auth) (set|unset|add|remove)(?:\ s*))(?P<values>(([\w\s.-]*)))`),
// Add more regex here
}
// regexListWithQuotes will find sensitive data within quotes and later we escape with "..."
Expand Down
Loading
Loading