@@ -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"
@@ -32,15 +34,17 @@ func NewEnvUnsetCommand(clients *shared.ClientFactory) *cobra.Command {
3234 cmd := & cobra.Command {
3335 Use : "unset [name] [flags]" ,
3436 Aliases : []string {"remove" },
35- Short : "Unset an environment variable from the app " ,
37+ Short : "Unset an environment variable from the project " ,
3638 Long : strings .Join ([]string {
37- "Unset an environment variable from an app deployed to Slack managed" ,
38- "infrastructure." ,
39+ "Unset an environment variable from the project." ,
3940 "" ,
4041 "If no variable name is provided, you will be prompted to select one." ,
4142 "" ,
42- "This command is supported for apps deployed to Slack managed infrastructure but" ,
43- "other apps can attempt to run the command with the --force flag." ,
43+ "Commands that run in the context of a project source environment variables from" ,
44+ `the ".env" file. This includes the "run" command.` ,
45+ "" ,
46+ `The "deploy" command gathers environment variables from the ".env" file as well` ,
47+ "unless the app is using ROSI features." ,
4448 }, "\n " ),
4549 Example : style .ExampleCommandsf ([]style.ExampleCommand {
4650 {
@@ -67,35 +71,33 @@ func NewEnvUnsetCommand(clients *shared.ClientFactory) *cobra.Command {
6771 return cmd
6872}
6973
70- // preRunEnvUnsetCommandFunc determines if the command is supported for a project
74+ // preRunEnvUnsetCommandFunc determines if the command is run in a valid project
7175// and configures flags
7276func preRunEnvUnsetCommandFunc (ctx context.Context , clients * shared.ClientFactory , cmd * cobra.Command ) error {
7377 clients .Config .SetFlags (cmd )
74- err := cmdutil .IsValidProjectDirectory (clients )
75- if err != nil {
76- return err
77- }
78- if clients .Config .ForceFlag {
79- return nil
80- }
81- return cmdutil .IsSlackHostedProject (ctx , clients )
78+ return cmdutil .IsValidProjectDirectory (clients )
8279}
8380
8481// runEnvUnsetCommandFunc removes an environment variable from an app
8582func runEnvUnsetCommandFunc (clients * shared.ClientFactory , cmd * cobra.Command , args []string ) error {
86- var ctx = cmd .Context ()
83+ ctx : = cmd .Context ()
8784
88- // Get the workspace from the flag or prompt
89- selection , err := appSelectPromptFunc (ctx , clients , prompts .ShowHostedOnly , prompts .ShowInstalledAppsOnly )
90- if err != nil {
91- return err
85+ // Hosted apps require selecting an app before gathering variable inputs.
86+ hosted := isHostedRuntime (ctx , clients )
87+ var selection prompts.SelectedApp
88+ if hosted {
89+ s , err := appSelectPromptFunc (ctx , clients , prompts .ShowAllEnvironments , prompts .ShowInstalledAppsOnly )
90+ if err != nil {
91+ return err
92+ }
93+ selection = s
9294 }
9395
94- // Get the variable name from the flags, args, or select from the environment
96+ // Get the variable name from args, or prompt from the appropriate source.
9597 var variableName string
9698 if len (args ) > 0 {
9799 variableName = args [0 ]
98- } else {
100+ } else if hosted && ! selection . App . IsDev {
99101 variables , err := clients .API ().ListVariables (
100102 ctx ,
101103 selection .Auth .Token ,
@@ -115,7 +117,41 @@ func runEnvUnsetCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, a
115117 }))
116118 return nil
117119 }
118- selection , err := clients .IO .SelectPrompt (
120+ selected , err := clients .IO .SelectPrompt (
121+ ctx ,
122+ "Select a variable to remove" ,
123+ variables ,
124+ iostreams.SelectPromptConfig {
125+ Flag : clients .Config .Flags .Lookup ("name" ),
126+ Required : true ,
127+ },
128+ )
129+ if err != nil {
130+ return err
131+ }
132+ variableName = selected .Option
133+ } else {
134+ dotEnv , err := slackdotenv .Read (clients .Fs )
135+ if err != nil {
136+ return err
137+ }
138+ if len (dotEnv ) <= 0 {
139+ clients .IO .PrintTrace (ctx , slacktrace .EnvUnsetSuccess )
140+ clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
141+ Emoji : "evergreen_tree" ,
142+ Text : "App Environment" ,
143+ Secondary : []string {
144+ "The project has no environment variables to remove" ,
145+ },
146+ }))
147+ return nil
148+ }
149+ variables := make ([]string , 0 , len (dotEnv ))
150+ for k := range dotEnv {
151+ variables = append (variables , k )
152+ }
153+ sort .Strings (variables )
154+ selected , err := clients .IO .SelectPrompt (
119155 ctx ,
120156 "Select a variable to remove" ,
121157 variables ,
@@ -126,32 +162,43 @@ func runEnvUnsetCommandFunc(clients *shared.ClientFactory, cmd *cobra.Command, a
126162 )
127163 if err != nil {
128164 return err
129- } else {
130- variableName = selection .Option
131165 }
166+ variableName = selected .Option
132167 }
133168
134- err = clients .API ().RemoveVariable (
135- ctx ,
136- selection .Auth .Token ,
137- selection .App .AppID ,
138- variableName ,
139- )
140- if err != nil {
141- return err
169+ // Remove the environment variable using either the Slack API method or the
170+ // project ".env" file depending on the app hosting.
171+ if hosted && ! selection .App .IsDev {
172+ err := clients .API ().RemoveVariable (
173+ ctx ,
174+ selection .Auth .Token ,
175+ selection .App .AppID ,
176+ variableName ,
177+ )
178+ if err != nil {
179+ return err
180+ }
181+ clients .IO .PrintTrace (ctx , slacktrace .EnvUnsetSuccess )
182+ clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
183+ Emoji : "evergreen_tree" ,
184+ Text : "App Environment" ,
185+ Secondary : []string {
186+ fmt .Sprintf ("Successfully removed \" %s\" as an app environment variable" , variableName ),
187+ },
188+ }))
189+ } else {
190+ err := slackdotenv .Unset (clients .Fs , variableName )
191+ if err != nil {
192+ return err
193+ }
194+ clients .IO .PrintTrace (ctx , slacktrace .EnvUnsetSuccess )
195+ clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
196+ Emoji : "evergreen_tree" ,
197+ Text : "App Environment" ,
198+ Secondary : []string {
199+ fmt .Sprintf ("Successfully removed \" %s\" as a project environment variable" , variableName ),
200+ },
201+ }))
142202 }
143-
144- clients .IO .PrintTrace (ctx , slacktrace .EnvUnsetSuccess )
145- clients .IO .PrintInfo (ctx , false , "\n %s" , style .Sectionf (style.TextSection {
146- Emoji : "evergreen_tree" ,
147- Text : "App Environment" ,
148- Secondary : []string {
149- fmt .Sprintf (
150- "Successfully removed \" %s\" from the app's environment variables" ,
151- variableName ,
152- ),
153- },
154- }))
155-
156203 return nil
157204}
0 commit comments