@@ -17,12 +17,14 @@ package env
1717import (
1818 "context"
1919 "fmt"
20+ "sort"
2021 "strings"
2122
2223 "github.com/slackapi/slack-cli/internal/cmdutil"
2324 "github.com/slackapi/slack-cli/internal/iostreams"
2425 "github.com/slackapi/slack-cli/internal/prompts"
2526 "github.com/slackapi/slack-cli/internal/shared"
27+ "github.com/slackapi/slack-cli/internal/slackdotenv"
2628 "github.com/slackapi/slack-cli/internal/slacktrace"
2729 "github.com/slackapi/slack-cli/internal/style"
2830 "github.com/spf13/cobra"
@@ -31,15 +33,17 @@ import (
3133func NewEnvRemoveCommand (clients * shared.ClientFactory ) * cobra.Command {
3234 cmd := & cobra.Command {
3335 Use : "remove <name> [flags]" ,
34- Short : "Remove an environment variable from the app " ,
36+ Short : "Remove an environment variable from the project " ,
3537 Long : strings .Join ([]string {
36- "Remove an environment variable from an app deployed to Slack managed" ,
37- "infrastructure." ,
38+ "Remove an environment variable from the project." ,
3839 "" ,
3940 "If no variable name is provided, you will be prompted to select one." ,
4041 "" ,
41- "This command is supported for apps deployed to Slack managed infrastructure but" ,
42- "other apps can attempt to run the command with the --force flag." ,
42+ "Commands that run in the context of a project source environment variables from" ,
43+ "the \" .env\" file. This includes the \" run\" command." ,
44+ "" ,
45+ "The \" deploy\" command gathers environment variables from the \" .env\" file as well" ,
46+ "unless the app is using ROSI features." ,
4347 }, "\n " ),
4448 Example : style .ExampleCommandsf ([]style.ExampleCommand {
4549 {
@@ -66,35 +70,33 @@ func NewEnvRemoveCommand(clients *shared.ClientFactory) *cobra.Command {
6670 return cmd
6771}
6872
69- // preRunEnvRemoveCommandFunc determines if the command is supported for a project
73+ // preRunEnvRemoveCommandFunc determines if the command is run in a valid project
7074// and configures flags
7175func preRunEnvRemoveCommandFunc (ctx context.Context , clients * shared.ClientFactory , cmd * cobra.Command ) error {
7276 clients .Config .SetFlags (cmd )
73- err := cmdutil .IsValidProjectDirectory (clients )
74- if err != nil {
75- return err
76- }
77- if clients .Config .ForceFlag {
78- return nil
79- }
80- return cmdutil .IsSlackHostedProject (ctx , clients )
77+ return cmdutil .IsValidProjectDirectory (clients )
8178}
8279
8380// runEnvRemoveCommandFunc removes an environment variable from an app
8481func runEnvRemoveCommandFunc (clients * shared.ClientFactory , cmd * cobra.Command , args []string ) error {
85- var ctx = cmd .Context ()
82+ ctx : = cmd .Context ()
8683
87- // Get the workspace from the flag or prompt
88- selection , err := appSelectPromptFunc (ctx , clients , prompts .ShowHostedOnly , prompts .ShowInstalledAppsOnly )
89- if err != nil {
90- return err
84+ // Hosted apps require selecting an app before gathering variable inputs.
85+ hosted := isHostedRuntime (ctx , clients )
86+ var selection prompts.SelectedApp
87+ if hosted {
88+ s , err := appSelectPromptFunc (ctx , clients , prompts .ShowAllEnvironments , prompts .ShowInstalledAppsOnly )
89+ if err != nil {
90+ return err
91+ }
92+ selection = s
9193 }
9294
93- // Get the variable name from the flags, args, or select from the environment
95+ // Get the variable name from args, or prompt from the appropriate source.
9496 var variableName string
9597 if len (args ) > 0 {
9698 variableName = args [0 ]
97- } else {
99+ } else if hosted && ! selection . App . IsDev {
98100 variables , err := clients .API ().ListVariables (
99101 ctx ,
100102 selection .Auth .Token ,
@@ -114,7 +116,41 @@ func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command,
114116 }))
115117 return nil
116118 }
117- selection , err := clients .IO .SelectPrompt (
119+ selected , err := clients .IO .SelectPrompt (
120+ ctx ,
121+ "Select a variable to remove" ,
122+ variables ,
123+ iostreams.SelectPromptConfig {
124+ Flag : clients .Config .Flags .Lookup ("name" ),
125+ Required : true ,
126+ },
127+ )
128+ if err != nil {
129+ return err
130+ }
131+ variableName = selected .Option
132+ } else {
133+ dotEnv , err := slackdotenv .Read (clients .Fs )
134+ if err != nil {
135+ return err
136+ }
137+ if len (dotEnv ) <= 0 {
138+ clients .IO .PrintTrace (ctx , slacktrace .EnvRemoveSuccess )
139+ clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
140+ Emoji : "evergreen_tree" ,
141+ Text : "App Environment" ,
142+ Secondary : []string {
143+ "The project has no environment variables to remove" ,
144+ },
145+ }))
146+ return nil
147+ }
148+ variables := make ([]string , 0 , len (dotEnv ))
149+ for k := range dotEnv {
150+ variables = append (variables , k )
151+ }
152+ sort .Strings (variables )
153+ selected , err := clients .IO .SelectPrompt (
118154 ctx ,
119155 "Select a variable to remove" ,
120156 variables ,
@@ -125,32 +161,43 @@ func runEnvRemoveCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command,
125161 )
126162 if err != nil {
127163 return err
128- } else {
129- variableName = selection .Option
130164 }
165+ variableName = selected .Option
131166 }
132167
133- err = clients .API ().RemoveVariable (
134- ctx ,
135- selection .Auth .Token ,
136- selection .App .AppID ,
137- variableName ,
138- )
139- if err != nil {
140- return err
168+ // Remove the environment variable using either the Slack API method or the
169+ // project ".env" file depending on the app hosting.
170+ if hosted && ! selection .App .IsDev {
171+ err := clients .API ().RemoveVariable (
172+ ctx ,
173+ selection .Auth .Token ,
174+ selection .App .AppID ,
175+ variableName ,
176+ )
177+ if err != nil {
178+ return err
179+ }
180+ clients .IO .PrintTrace (ctx , slacktrace .EnvRemoveSuccess )
181+ clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
182+ Emoji : "evergreen_tree" ,
183+ Text : "App Environment" ,
184+ Secondary : []string {
185+ fmt .Sprintf ("Successfully removed \" %s\" as an app environment variable" , variableName ),
186+ },
187+ }))
188+ } else {
189+ err := slackdotenv .Unset (clients .Fs , variableName )
190+ if err != nil {
191+ return err
192+ }
193+ clients .IO .PrintTrace (ctx , slacktrace .EnvRemoveSuccess )
194+ clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
195+ Emoji : "evergreen_tree" ,
196+ Text : "App Environment" ,
197+ Secondary : []string {
198+ fmt .Sprintf ("Successfully removed \" %s\" as a project environment variable" , variableName ),
199+ },
200+ }))
141201 }
142-
143- clients .IO .PrintTrace (ctx , slacktrace .EnvRemoveSuccess )
144- clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
145- Emoji : "evergreen_tree" ,
146- Text : "App Environment" ,
147- Secondary : []string {
148- fmt .Sprintf (
149- "Successfully removed \" %s\" from the app's environment variables" ,
150- variableName ,
151- ),
152- },
153- }))
154-
155202 return nil
156203}
0 commit comments