-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathclients.go
More file actions
350 lines (309 loc) · 13 KB
/
clients.go
File metadata and controls
350 lines (309 loc) · 13 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// 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 shared
import (
"context"
"encoding/json"
"net/url"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"
"github.com/slackapi/slack-cli/internal/api"
"github.com/slackapi/slack-cli/internal/app"
"github.com/slackapi/slack-cli/internal/auth"
"github.com/slackapi/slack-cli/internal/config"
"github.com/slackapi/slack-cli/internal/goutils"
"github.com/slackapi/slack-cli/internal/hooks"
"github.com/slackapi/slack-cli/internal/iostreams"
"github.com/slackapi/slack-cli/internal/runtime"
"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slackdeps"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/slackapi/slack-cli/internal/tracking"
"github.com/spf13/afero"
)
// ClientFactory are shared clients and configurations for use across the CLI commands (cmd) and handlers (pkg).
type ClientFactory struct {
API func() api.APIInterface
AppClient func() *app.Client
Auth func() auth.AuthInterface
CLIVersion string
Config *config.Config
EventTracker tracking.TrackingManager
HookExecutor hooks.HookExecutor
IO iostreams.IOStreamer
Runtime runtime.Runtime
SDKConfig hooks.SDKCLIConfig
// Browser can display or open URLs as webpages on the internet
Browser func() slackdeps.Browser
// Fs is the file system module that's shared by all packages and enables testing & mock of the file system
Fs afero.Fs
// Os are a group of operating system functions following the `os` interface that are shared by all packages and enables testing & mocking
Os types.Os
// CleanupWaitGroup is a group of wait groups shared by all packages and allow functions to cleanup before the process terminates
CleanupWaitGroup sync.WaitGroup
}
const sdkSlackDevDomainFlag = "sdk-slack-dev-domain"
const sdkUnsafelyIgnoreCertErrorsFlag = "sdk-unsafely-ignore-certificate-errors"
// NewClientFactory creates a new ClientFactory type with the default function handlers
func NewClientFactory(options ...func(*ClientFactory)) *ClientFactory {
clients := &ClientFactory{}
// TODO: is there a better place to put this?
clients.CleanupWaitGroup = sync.WaitGroup{}
// External dependencies that belong to clients for testing and mocking
clients.Os = slackdeps.NewOs()
clients.Fs = slackdeps.NewFs()
// Default values
clients.Config = config.NewConfig(clients.Fs, clients.Os)
clients.IO = iostreams.NewIOStreams(clients.Config, clients.Fs, clients.Os)
clients.HookExecutor = &hooks.HookExecutorDefaultProtocol{
IO: clients.IO,
}
clients.EventTracker = tracking.NewEventTracker()
clients.API = clients.defaultAPIFunc
clients.AppClient = clients.defaultAppClientFunc
clients.Auth = clients.defaultAuthFunc
clients.Browser = clients.defaultBrowserFunc
// TODO: Temporary hack to get around circular dependency in internal/api/client.go since that imports version
// Follows pattern demonstrated by the GitHub CLI here https://github.com/cli/cli/blob/5a46c1cab601a3394caa8de85adb14f909b811e9/pkg/cmd/factory/default.go#L29
// Used by the APIClient for its userAgent
// Currently needed because trying to get the version of the CLI from pkg/version/version.go would cause a circular dependency
// We can get rid of this once we refactor the code relationship between pkg/ and internal/
// userAgent can get Slack CLI version from context which is defined in main.go, this approach bypass circular dependency. The clients.CLIVersion is retained for future code refactor purpose and serve SetVersion function
clients.CLIVersion = ""
// Custom values set by functional options
// Learn more: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
for _, option := range options {
option(clients)
}
return clients
}
// defaultAPIClientFunc return a new API Client using the ConfigAPIHost
func (c *ClientFactory) defaultAPIClientFunc() *api.Client {
return api.NewClient(nil, c.Config.APIHostResolved, c.IO)
}
// defaultAPIFunc return a new API Client using the ConfigAPIHost
func (c *ClientFactory) defaultAPIFunc() api.APIInterface {
return c.defaultAPIClientFunc()
}
// defaultAppClientFunc return a new App Client
func (c *ClientFactory) defaultAppClientFunc() *app.Client {
return app.NewClient(c.API(), c.Config, c.Fs, c.Os)
}
// defaultAuthClientFunc return a new Auth Client
func (c *ClientFactory) defaultAuthClientFunc() *auth.Client {
return auth.NewClient(c.API(), c.AppClient(), c.Config, c.IO, c.Fs)
}
// defaultAuthFunc return a new Auth Interface
func (c *ClientFactory) defaultAuthFunc() auth.AuthInterface {
return c.defaultAuthClientFunc()
}
// defaultBrowserFunc returns a new Browser
func (c *ClientFactory) defaultBrowserFunc() slackdeps.Browser {
return slackdeps.NewBrowser(c.IO.WriteOut())
}
// InitRuntime initializes a new Runtime instance from the runtime flag or the
// SDK config or the directory structure
func (c *ClientFactory) InitRuntime(ctx context.Context, dirPath string) error {
var err error
var method string
switch {
case len(strings.TrimSpace(c.Config.RuntimeFlag)) > 0:
method = "flag"
c.Runtime, err = runtime.New(c.Config.RuntimeFlag)
case len(strings.TrimSpace(c.SDKConfig.Runtime)) > 0:
method = "hooks.json"
c.Runtime, err = runtime.New(c.SDKConfig.Runtime)
default:
method = "auto-detect"
c.Runtime, err = runtime.NewDetectProject(ctx, c.Fs, dirPath, c.SDKConfig)
}
if err != nil {
c.IO.PrintDebug(ctx, "failed to initialize the project runtime: %s", err)
return err
}
c.IO.PrintDebug(ctx, "initialize runtime from %s: %s (%s)",
method, c.Runtime.Name(), c.Runtime.Version())
return nil
}
// InitSDKConfig finds and loads hook configurations for a project
func (c *ClientFactory) InitSDKConfig(ctx context.Context, dirPath string) error {
// Read the project Slack hooks file (.slack/hooks.json)
// A command can be run any the project's root directory or subdirectory, so
// we begin in the current directory and loop moving up the directory tree
hooksJSONFilePath := "hooks.json"
homeDir, err := c.Os.UserHomeDir()
if err != nil {
return err
}
for {
// First, check if the hooks file exists in the current working directory
hooksJSONFilePath = filepath.Join(dirPath, ".slack", "hooks.json")
info, err := c.Fs.Stat(hooksJSONFilePath)
if err == nil && !info.IsDir() {
break
}
// Then, fallback to hooks in the deprecated project slack.json file
// DEPRECATED(semver:major) - Drop support on the next major
hooksJSONFilePath = filepath.Join(dirPath, "slack.json")
info, err = c.Fs.Stat(hooksJSONFilePath)
if err == nil && !info.IsDir() {
c.IO.PrintDebug(ctx, "%s", slackerror.New(slackerror.ErrSlackJSONLocation))
break
}
// Next, search for the hooks files in the outdated path
// .slack/slack.json and display an error that this path
// is deprecated
// DEPRECATED(semver:major) - Drop support on the next major
hooksJSONFilePath = filepath.Join(dirPath, ".slack", "slack.json")
info, err = c.Fs.Stat(hooksJSONFilePath)
if err == nil && !info.IsDir() {
c.IO.PrintWarning(ctx, "%s", slackerror.New(slackerror.ErrSlackSlackJSONLocation))
break
}
// Next, search for the hooks files in the outdated path
// .slack/cli.json and display an error that this path
// is deprecated
// DEPRECATED(semver:major) - Drop support on the next major
hooksJSONFilePath = filepath.Join(dirPath, ".slack", "cli.json")
info, err = c.Fs.Stat(hooksJSONFilePath)
if err == nil && !info.IsDir() {
return slackerror.New(slackerror.ErrCLIConfigLocationError)
}
// Return an error if the current path is the project root, identified by the
// .slack directory, because no hooks file was found
slackConfigDirPath := filepath.Join(dirPath, ".slack")
info, err = c.Fs.Stat(slackConfigDirPath)
if err == nil && info.IsDir() {
return slackerror.New(slackerror.ErrHooksJSONLocation)
}
// Return an error if we have reached the user home directory, root directory,
// system root volume, or "." (returned by Dir when all path elements are removed)
switch dirPath {
case homeDir, "/", filepath.VolumeName(os.Getenv("SYSTEMROOT")) + "\\", ".":
return slackerror.New(slackerror.ErrHooksJSONLocation)
}
// Move upward one directory level
parentDir := filepath.Dir(dirPath)
if parentDir == dirPath {
// Reached a filesystem root not covered above (e.g. D:\ when SYSTEMROOT is on C:\)
return slackerror.New(slackerror.ErrHooksJSONLocation)
}
dirPath = parentDir
}
// Activate Python virtual environment if present, so hook scripts
// can resolve the venv's Python and installed packages.
if activated, err := runtime.ActivatePythonVenvIfPresent(c.Fs, c.Os, dirPath); err != nil {
c.IO.PrintDebug(ctx, "failed to activate Python virtual environment: %s", err)
} else if activated {
c.IO.PrintDebug(ctx, "Activated Python virtual environment .venv")
}
configFileBytes, err := afero.ReadFile(c.Fs, hooksJSONFilePath)
if err != nil {
return err // Fixes regression: do not wrap this error, so that the caller can use `os.IsNotExists`
}
err = c.InitSDKConfigFromJSON(ctx, configFileBytes)
// TODO: this is a side-effect-y way of signaling to the rest of the codebase "we are in an app project directory now"
c.SDKConfig.WorkingDirectory = dirPath
c.HookExecutor = hooks.GetHookExecutor(c.IO, c.SDKConfig)
return err
}
// InitSDKConfigFromJSON merges configuration values from the `get-hooks` hook and the local configuration file
func (c *ClientFactory) InitSDKConfigFromJSON(ctx context.Context, configFileBytes []byte) error {
// GetHooksConfig maps to the contents of a typical app's `hooks.json` file.
// Only the `get-hooks` script is extracted with this object but the entire contents of the app's
// `hooks.json` will later be merged with the config returned by `get-hooks`.
type GetHooksConfig struct {
Hooks struct {
GetHooks hooks.HookScript `json:"get-hooks,omitempty"`
} `json:"hooks,omitempty"`
}
// Load the config with the contents of the file
getHooksConfig := GetHooksConfig{}
err := json.Unmarshal(configFileBytes, &getHooksConfig)
if err != nil {
return slackerror.JSONUnmarshalError(err, configFileBytes)
}
// When GetHooks is available, load the scripts as the default values
// The project's config will then be used to override these default value
var SDKHooksResponse string
if getHooksConfig.Hooks.GetHooks.IsAvailable() {
getHooksConfig.Hooks.GetHooks.Name = "GetHooks"
getHooksArgs := map[string]string{}
if devInstanceHostname := getDevHostname(c.Config.APIHostResolved); devInstanceHostname != "" {
getHooksArgs[sdkSlackDevDomainFlag] = devInstanceHostname
getHooksArgs[sdkUnsafelyIgnoreCertErrorsFlag] = devInstanceHostname
}
var hookExecOpts = hooks.HookExecOpts{
Args: getHooksArgs,
Hook: getHooksConfig.Hooks.GetHooks,
}
defaultExecutor := hooks.HookExecutorDefaultProtocol{
IO: c.IO,
}
if SDKHooksResponse, err = defaultExecutor.Execute(ctx, hookExecOpts); err != nil {
return err
}
}
// Merge the default hooks (from get-hooks) with the hooks from the project file (hooks.json)
var config hooks.SDKCLIConfig
if err := goutils.MergeJSON(SDKHooksResponse, string(configFileBytes), &config); err != nil {
return slackerror.Wrap(err, slackerror.ErrSDKConfigLoad)
}
c.SDKConfig = config
// Reflect on the hooks struct to set the Name field for each hook
hooks := reflect.ValueOf(&c.SDKConfig.Hooks).Elem()
fields := reflect.VisibleFields(reflect.TypeOf(c.SDKConfig.Hooks))
for _, field := range fields {
hookPointer := hooks.FieldByName(field.Name)
if hookPointer.IsValid() {
hookNamePointer := hookPointer.FieldByName("Name")
if hookNamePointer.IsValid() && hookNamePointer.CanSet() {
hookNamePointer.SetString(field.Name)
}
}
}
c.IO.PrintDebug(ctx, "initialized SDK CLI config: %+v", c.SDKConfig)
return nil
}
// DebugMode is an example of defining a functional options helper
func DebugMode(c *ClientFactory) {
c.Config.DebugEnabled = true
}
// SetVersion is a functional option that sets the Cli version that the API Client references
// Learn more: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
func SetVersion(version string) func(c *ClientFactory) {
return func(c *ClientFactory) { c.CLIVersion = version }
}
// getDevHostname returns the hostname of the given URL if it is dev or a numbered dev instance
func getDevHostname(host string) string {
if host == "" {
return ""
}
u, err := url.Parse(host)
if err != nil {
return ""
}
match, err := regexp.MatchString("[dev|qa]([0-9]+)?\\.slack\\.com", u.Hostname())
if err != nil {
panic("Unable to parse regexp")
}
if match {
return u.Hostname()
}
return ""
}