-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathadd.go
More file actions
217 lines (196 loc) · 7.87 KB
/
add.go
File metadata and controls
217 lines (196 loc) · 7.87 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2022-2025 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 app
import (
"context"
"fmt"
"github.com/slackapi/slack-cli/internal/app"
"github.com/slackapi/slack-cli/internal/cmdutil"
"github.com/slackapi/slack-cli/internal/config"
"github.com/slackapi/slack-cli/internal/experiment"
"github.com/slackapi/slack-cli/internal/logger"
"github.com/slackapi/slack-cli/internal/pkg/apps"
"github.com/slackapi/slack-cli/internal/prompts"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/slackapi/slack-cli/internal/style"
"github.com/spf13/cobra"
)
// Handle to client's function used for testing
var runAddCommandFunc = RunAddCommand
var appInstallProdAppFunc = apps.Add
var appInstallDevAppFunc = apps.InstallLocalApp
var appSelectPromptFunc = prompts.AppSelectPrompt
// Flags
type addCmdFlags struct {
orgGrantWorkspaceID string
}
var addFlags addCmdFlags
// NewAddCommand returns a new Cobra command
func NewAddCommand(clients *shared.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "install [flags]",
Aliases: []string{"add"},
Short: "Install the app to a team",
Long: "Install the app to a team",
Example: style.ExampleCommandsf([]style.ExampleCommand{
{Command: "app install", Meaning: "Install a production app to a team"},
{Command: "app install --team T0123456", Meaning: "Install a production app to a specific team"},
}),
PreRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
return preRunAddCommand(ctx, clients)
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
_, _, appInstance, err := runAddCommandFunc(ctx, clients, nil, addFlags.orgGrantWorkspaceID)
if err != nil {
return err
}
return printAddSuccess(clients, cmd, appInstance)
},
}
cmd.Flags().StringVar(&addFlags.orgGrantWorkspaceID, cmdutil.OrgGrantWorkspaceFlag, "", cmdutil.OrgGrantWorkspaceDescription())
return cmd
}
// preRunAddCommand confirms an app is available for installation
func preRunAddCommand(ctx context.Context, clients *shared.ClientFactory) error {
err := cmdutil.IsValidProjectDirectory(clients)
if err != nil {
return err
}
if !clients.Config.WithExperimentOn(experiment.BoltFrameworks) {
return nil
}
manifestSource, err := clients.Config.ProjectConfig.GetManifestSource(ctx)
if err != nil {
return err
}
if manifestSource.Equals(config.ManifestSourceRemote) {
return slackerror.New(slackerror.ErrAppInstall).
WithMessage("Apps cannot be installed due to project configurations").
WithRemediation(
"Install an app on app settings: %s\nLink an app to this project with %s\nList apps saved with this project using %s",
style.LinkText("https://api.slack.com/apps"),
style.Commandf("app link", false),
style.Commandf("app list", false),
).
WithDetails(slackerror.ErrorDetails{
slackerror.ErrorDetail{
Code: slackerror.ErrProjectConfigManifestSource,
Message: "Cannot install apps with manifests sourced from app settings",
},
})
}
return nil
}
// RunAddCommand executes the workspace install command, prints output, and returns any errors.
func RunAddCommand(ctx context.Context, clients *shared.ClientFactory, selection *prompts.SelectedApp, orgGrantWorkspaceID string) (context.Context, types.InstallState, types.App, error) {
if selection == nil {
selected, err := appSelectPromptFunc(ctx, clients, prompts.ShowHostedOnly, prompts.ShowAllApps)
if err != nil {
return ctx, "", types.App{}, err
}
selection = &selected
}
if selection.Auth.TeamDomain == "" {
return ctx, "", types.App{}, slackerror.New(slackerror.ErrCredentialsNotFound)
}
var err error
orgGrantWorkspaceID, err = prompts.ValidateGetOrgWorkspaceGrant(ctx, clients, selection, orgGrantWorkspaceID, true /* top prompt option should be 'all workspaces' */)
if err != nil {
return ctx, "", types.App{}, err
}
clients.Config.ManifestEnv = app.SetManifestEnvTeamVars(clients.Config.ManifestEnv, selection.App.TeamDomain, selection.App.IsDev)
// Set up event logger
log := newAddLogger(clients, selection.Auth.TeamDomain)
// Install dev app or prod app to a workspace
installedApp, installState, err := appInstall(ctx, clients, log, selection, orgGrantWorkspaceID)
if err != nil {
return ctx, installState, types.App{}, err // pass the installState because some callers may use it to handle the error
}
// Update the context with the token
ctx = config.SetContextToken(ctx, selection.Auth.Token)
return ctx, installState, installedApp, nil
}
// newAddLogger creates a logger instance to receive event notifications
func newAddLogger(clients *shared.ClientFactory, envName string) *logger.Logger {
return logger.New(
// OnEvent
func(event *logger.LogEvent) {
teamName := event.DataToString("teamName")
appName := event.DataToString("appName")
switch event.Name {
case "app_install_manifest":
// Ignore this event and format manifest outputs in create/update events
case "app_install_manifest_create":
_, _ = clients.IO.WriteOut().Write([]byte(style.Sectionf(style.TextSection{
Emoji: "books",
Text: "App Manifest",
Secondary: []string{
fmt.Sprintf(`Creating app manifest for "%s" in "%s"`, appName, teamName),
},
})))
case "app_install_manifest_update":
_, _ = clients.IO.WriteOut().Write([]byte("\n" + style.Sectionf(style.TextSection{
Emoji: "books",
Text: "App Manifest",
Secondary: []string{
fmt.Sprintf(`Updated app manifest for "%s" in "%s"`, appName, teamName),
},
})))
case "app_install_start":
_, _ = clients.IO.WriteOut().Write([]byte("\n" + style.Sectionf(style.TextSection{
Emoji: "house",
Text: "App Install",
Secondary: []string{
fmt.Sprintf(`Installing "%s" app to "%s"`, appName, teamName),
},
})))
case "app_install_icon_success":
iconPath := event.DataToString("iconPath")
_, _ = clients.IO.WriteOut().Write([]byte(
style.SectionSecondaryf("Updated app icon: %s", iconPath),
))
case "app_install_icon_error":
iconError := event.DataToString("iconError")
_, _ = clients.IO.WriteOut().Write([]byte(
style.SectionSecondaryf("Error updating app icon: %s", iconError),
))
case "app_install_complete":
_, _ = clients.IO.WriteOut().Write([]byte(
style.SectionSecondaryf("Finished in %s", event.DataToString("installTime")),
))
default:
// Ignore the event
}
},
)
}
// printAddSuccess will print a list of the environments
func printAddSuccess(clients *shared.ClientFactory, cmd *cobra.Command, appInstance types.App) error {
return runListCommand(cmd, clients)
}
// appInstall will install an app to a team. It supports both local and deployed app types.
func appInstall(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, selection *prompts.SelectedApp, orgGrantWorkspaceID string) (types.App, types.InstallState, error) {
if selection != nil && selection.App.IsDev {
// Install local dev app to a team
installedApp, _, installState, err := appInstallDevAppFunc(ctx, clients, "", log, selection.Auth, selection.App)
return installedApp, installState, err
} else {
installState, installedApp, err := appInstallProdAppFunc(ctx, clients, log, selection.Auth, selection.App, orgGrantWorkspaceID)
return installedApp, installState, err
}
}