-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathadd.go
More file actions
164 lines (151 loc) · 4.97 KB
/
add.go
File metadata and controls
164 lines (151 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2022-2026 Salesforce, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package env
import (
"context"
"fmt"
"strings"
"github.com/slackapi/slack-cli/internal/cmdutil"
"github.com/slackapi/slack-cli/internal/iostreams"
"github.com/slackapi/slack-cli/internal/prompts"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/slackdotenv"
"github.com/slackapi/slack-cli/internal/slacktrace"
"github.com/slackapi/slack-cli/internal/style"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
func NewEnvAddCommand(clients *shared.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "add <name> [value] [flags]",
Short: "Add an environment variable to the project",
Long: strings.Join([]string{
"Add an environment variable to the project.",
"",
"If a name or value is not provided, you will be prompted to provide these.",
"",
"Commands that run in the context of a project source environment variables from",
`the ".env" file. This includes the "run" command.`,
"",
`The "deploy" command gathers environment variables from the ".env" file as well`,
"unless the app is using ROSI features.",
}, "\n"),
Example: style.ExampleCommandsf([]style.ExampleCommand{
{
Meaning: "Prompt for an environment variable",
Command: "env add",
},
{
Meaning: "Add an environment variable",
Command: "env add MAGIC_PASSWORD abracadbra",
},
{
Meaning: "Prompt for an environment variable value",
Command: "env add SECRET_PASSWORD",
},
}),
Args: cobra.MaximumNArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
return preRunEnvAddCommandFunc(ctx, clients, cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runEnvAddCommandFunc(clients, cmd, args)
},
}
cmd.Flags().StringVar(&variableValueFlag, "value", "", "set the environment variable value")
return cmd
}
// preRunEnvAddCommandFunc 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 {
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 {
ctx := cmd.Context()
// Hosted apps require selecting an app before gathering variable inputs.
hosted := isHostedRuntime(ctx, clients)
var selection prompts.SelectedApp
if hosted {
s, err := appSelectPromptFunc(ctx, clients, prompts.ShowAllEnvironments, prompts.ShowInstalledAppsOnly)
if err != nil {
return err
}
selection = s
}
// Get the variable name from the args or prompt
variableName := ""
if len(args) < 1 {
name, err := clients.IO.InputPrompt(ctx, "Variable name", iostreams.InputPromptConfig{
Required: false,
})
if err != nil {
return err
}
variableName = name
} else {
variableName = args[0]
}
// Get the variable value from the args or prompt
variableValue := ""
if len(args) < 2 {
response, err := clients.IO.PasswordPrompt(ctx, "Variable value", iostreams.PasswordPromptConfig{
Flag: clients.Config.Flags.Lookup("value"),
})
if err != nil {
return err
}
variableValue = response.Value
} else {
variableValue = args[1]
}
// Add the environment variable using either the Slack API method or the
// project ".env" file depending on the app hosting.
var details []string
if hosted && !selection.App.IsDev {
err := clients.API().AddVariable(
ctx,
selection.Auth.Token,
selection.App.AppID,
variableName,
variableValue,
)
if err != nil {
return err
}
details = append(details, fmt.Sprintf("Successfully added \"%s\" as an app environment variable", variableName))
} else {
exists, err := afero.Exists(clients.Fs, ".env")
if err != nil {
return err
}
err = slackdotenv.Set(clients.Fs, variableName, variableValue)
if err != nil {
return err
}
if !exists {
details = append(details, "Created a project .env file that shouldn't be added to version control")
}
details = append(details, fmt.Sprintf("Successfully added \"%s\" as a project environment variable", variableName))
}
clients.IO.PrintTrace(ctx, slacktrace.EnvAddSuccess)
clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
Emoji: "evergreen_tree",
Text: "Environment Add",
Secondary: details,
}))
return nil
}