Skip to content

Commit 1b94ec1

Browse files
committed
refactor: remove logger from app install, add, and function mocks
1 parent c449532 commit 1b94ec1

7 files changed

Lines changed: 65 additions & 129 deletions

File tree

cmd/app/add.go

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ package app
1616

1717
import (
1818
"context"
19-
"fmt"
2019

2120
"github.com/slackapi/slack-cli/internal/app"
2221
"github.com/slackapi/slack-cli/internal/cmdutil"
2322
"github.com/slackapi/slack-cli/internal/config"
2423
"github.com/slackapi/slack-cli/internal/experiment"
25-
"github.com/slackapi/slack-cli/internal/logger"
2624
"github.com/slackapi/slack-cli/internal/pkg/apps"
2725
"github.com/slackapi/slack-cli/internal/prompts"
2826
"github.com/slackapi/slack-cli/internal/shared"
@@ -186,11 +184,8 @@ func RunAddCommand(ctx context.Context, clients *shared.ClientFactory, selection
186184

187185
clients.Config.ManifestEnv = app.SetManifestEnvTeamVars(clients.Config.ManifestEnv, selection.App.TeamDomain, selection.App.IsDev)
188186

189-
// Set up event logger
190-
log := newAddLogger(clients, selection.Auth.TeamDomain)
191-
192187
// Install dev app or prod app to a workspace
193-
installedApp, installState, err := appInstall(ctx, clients, log, selection, orgGrantWorkspaceID)
188+
installedApp, installState, err := appInstall(ctx, clients, selection, orgGrantWorkspaceID)
194189
if err != nil {
195190
return ctx, installState, types.App{}, err // pass the installState because some callers may use it to handle the error
196191
}
@@ -201,74 +196,19 @@ func RunAddCommand(ctx context.Context, clients *shared.ClientFactory, selection
201196
return ctx, installState, installedApp, nil
202197
}
203198

204-
// newAddLogger creates a logger instance to receive event notifications
205-
func newAddLogger(clients *shared.ClientFactory, envName string) *logger.Logger {
206-
return logger.New(
207-
// OnEvent
208-
func(event *logger.LogEvent) {
209-
teamName := event.DataToString("teamName")
210-
appName := event.DataToString("appName")
211-
switch event.Name {
212-
case "app_install_manifest":
213-
// Ignore this event and format manifest outputs in create/update events
214-
case "app_install_manifest_create":
215-
_, _ = clients.IO.WriteOut().Write([]byte(style.Sectionf(style.TextSection{
216-
Emoji: "books",
217-
Text: "App Manifest",
218-
Secondary: []string{
219-
fmt.Sprintf(`Creating app manifest for "%s" in "%s"`, appName, teamName),
220-
},
221-
})))
222-
case "app_install_manifest_update":
223-
_, _ = clients.IO.WriteOut().Write([]byte("\n" + style.Sectionf(style.TextSection{
224-
Emoji: "books",
225-
Text: "App Manifest",
226-
Secondary: []string{
227-
fmt.Sprintf(`Updated app manifest for "%s" in "%s"`, appName, teamName),
228-
},
229-
})))
230-
case "app_install_start":
231-
_, _ = clients.IO.WriteOut().Write([]byte("\n" + style.Sectionf(style.TextSection{
232-
Emoji: "house",
233-
Text: "App Install",
234-
Secondary: []string{
235-
fmt.Sprintf(`Installing "%s" app to "%s"`, appName, teamName),
236-
},
237-
})))
238-
case "app_install_icon_success":
239-
iconPath := event.DataToString("iconPath")
240-
_, _ = clients.IO.WriteOut().Write([]byte(
241-
style.SectionSecondaryf("Updated app icon: %s", iconPath),
242-
))
243-
case "app_install_icon_error":
244-
iconError := event.DataToString("iconError")
245-
_, _ = clients.IO.WriteOut().Write([]byte(
246-
style.SectionSecondaryf("Error updating app icon: %s", iconError),
247-
))
248-
case "app_install_complete":
249-
_, _ = clients.IO.WriteOut().Write([]byte(
250-
style.SectionSecondaryf("Finished in %s", event.DataToString("installTime")),
251-
))
252-
default:
253-
// Ignore the event
254-
}
255-
},
256-
)
257-
}
258-
259199
// printAddSuccess will print a list of the environments
260200
func printAddSuccess(clients *shared.ClientFactory, cmd *cobra.Command, appInstance types.App) error {
261201
return runListCommand(cmd, clients)
262202
}
263203

264204
// appInstall will install an app to a team. It supports both local and deployed app types.
265-
func appInstall(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, selection *prompts.SelectedApp, orgGrantWorkspaceID string) (types.App, types.InstallState, error) {
205+
func appInstall(ctx context.Context, clients *shared.ClientFactory, selection *prompts.SelectedApp, orgGrantWorkspaceID string) (types.App, types.InstallState, error) {
266206
if selection != nil && selection.App.IsDev {
267207
// Install local dev app to a team
268-
installedApp, _, installState, err := appInstallDevAppFunc(ctx, clients, "", log, selection.Auth, selection.App)
208+
installedApp, _, installState, err := appInstallDevAppFunc(ctx, clients, "", selection.Auth, selection.App)
269209
return installedApp, installState, err
270210
} else {
271-
installState, installedApp, err := appInstallProdAppFunc(ctx, clients, log, selection.Auth, selection.App, orgGrantWorkspaceID)
211+
installState, installedApp, err := appInstallProdAppFunc(ctx, clients, selection.Auth, selection.App, orgGrantWorkspaceID)
272212
return installedApp, installState, err
273213
}
274214
}

cmd/function/mocks_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package function_test
1717
import (
1818
"context"
1919

20-
"github.com/slackapi/slack-cli/internal/logger"
2120
"github.com/slackapi/slack-cli/internal/shared"
2221
"github.com/slackapi/slack-cli/internal/shared/types"
2322
"github.com/stretchr/testify/mock"
@@ -27,22 +26,22 @@ type FunctionDistributorMock struct {
2726
mock.Mock
2827
}
2928

30-
func (m *FunctionDistributorMock) List(ctx context.Context, clients *shared.ClientFactory, fn string, log *logger.Logger) (types.Permission, []types.FunctionDistributionUser, error) {
29+
func (m *FunctionDistributorMock) List(ctx context.Context, clients *shared.ClientFactory, fn string) (types.Permission, []types.FunctionDistributionUser, error) {
3130
args := m.Called()
3231
return args.Get(0).(types.Permission), args.Get(1).([]types.FunctionDistributionUser), args.Error(2)
3332
}
3433

35-
func (m *FunctionDistributorMock) Set(ctx context.Context, clients *shared.ClientFactory, function, distributionType, users string, log *logger.Logger) (string, error) {
34+
func (m *FunctionDistributorMock) Set(ctx context.Context, clients *shared.ClientFactory, function, distributionType, users string) (string, error) {
3635
args := m.Called()
3736
return args.String(0), args.Error(1)
3837
}
3938

40-
func (m *FunctionDistributorMock) AddUsers(ctx context.Context, clients *shared.ClientFactory, function, users string, log *logger.Logger) (string, error) {
39+
func (m *FunctionDistributorMock) AddUsers(ctx context.Context, clients *shared.ClientFactory, function, users string) (string, error) {
4140
args := m.Called()
4241
return args.String(0), args.Error(1)
4342
}
4443

45-
func (m *FunctionDistributorMock) RemoveUsers(ctx context.Context, clients *shared.ClientFactory, function, users string, log *logger.Logger) (string, error) {
44+
func (m *FunctionDistributorMock) RemoveUsers(ctx context.Context, clients *shared.ClientFactory, function, users string) (string, error) {
4645
args := m.Called()
4746
return args.String(0), args.Error(1)
4847
}

internal/pkg/apps/add.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,31 @@ import (
1919
"strings"
2020

2121
"github.com/opentracing/opentracing-go"
22-
"github.com/slackapi/slack-cli/internal/logger"
2322
"github.com/slackapi/slack-cli/internal/shared"
2423
"github.com/slackapi/slack-cli/internal/shared/types"
2524
"github.com/slackapi/slack-cli/internal/slackerror"
2625
)
2726

2827
// Add will add an app
29-
func Add(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) {
28+
func Add(ctx context.Context, clients *shared.ClientFactory, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) {
3029
span, _ := opentracing.StartSpanFromContext(ctx, "pkg.apps.add")
3130
defer span.Finish()
3231

3332
// Validate the auth
34-
ctx, authSession, err := getAuthSession(ctx, clients, auth)
33+
ctx, _, err := getAuthSession(ctx, clients, auth)
3534
if err != nil {
3635
return "", types.App{}, slackerror.Wrap(err, slackerror.ErrAddAppToProject)
3736
}
38-
log.Data["teamName"] = *authSession.TeamName
39-
40-
log.Info("on_apps_add_init")
4137

4238
// Add app remotely via Slack API
43-
installState, app, err := addAppRemotely(ctx, clients, log, auth, app, orgGrantWorkspaceID)
39+
installState, app, err := addAppRemotely(ctx, clients, auth, app, orgGrantWorkspaceID)
4440
if err != nil {
4541
return "", types.App{}, slackerror.Wrap(err, slackerror.ErrAddAppToProject)
4642
}
4743

4844
// Add app to apps.json
4945
if !clients.Config.SkipLocalFs() {
50-
if _, err := addAppLocally(ctx, clients, log, app); err != nil {
46+
if _, err := addAppLocally(ctx, clients, app); err != nil {
5147
return installState, types.App{}, slackerror.Wrap(err, slackerror.ErrAddAppToProject)
5248
}
5349
}
@@ -56,9 +52,7 @@ func Add(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger,
5652
}
5753

5854
// addAppLocally will add the app to the project's apps file with an empty AppID, TeamID, etc
59-
func addAppLocally(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, app types.App) (types.App, error) {
60-
log.Info("on_apps_add_local")
61-
55+
func addAppLocally(ctx context.Context, clients *shared.ClientFactory, app types.App) (types.App, error) {
6256
app, err := clients.AppClient().NewDeployed(ctx, app.TeamID)
6357
if err != nil {
6458
if !strings.Contains(err.Error(), slackerror.ErrAppFound) { // Ignore the error when the app already exists
@@ -69,22 +63,18 @@ func addAppLocally(ctx context.Context, clients *shared.ClientFactory, log *logg
6963
}
7064

7165
// addAppRemotely will create the app manifest using the current auth account's team
72-
func addAppRemotely(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) {
73-
log.Info("on_apps_add_remote_init")
74-
66+
func addAppRemotely(ctx context.Context, clients *shared.ClientFactory, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) {
7567
if app.TeamID == "" {
7668
// App hasn't been created yet and
7769
// so the target team ID set by the auth
7870
app.TeamID = auth.TeamID
7971
}
8072

81-
app, installState, err := Install(ctx, clients, log, auth, CreateAppManifestAndInstall, app, orgGrantWorkspaceID)
73+
app, installState, err := Install(ctx, clients, auth, CreateAppManifestAndInstall, app, orgGrantWorkspaceID)
8274
if err != nil {
8375
return installState, types.App{}, slackerror.Wrap(err, slackerror.ErrAppAdd)
8476
}
8577

86-
log.Info("on_apps_add_remote_success")
87-
8878
if !clients.Config.SkipLocalFs() {
8979
app, err = clients.AppClient().GetDeployed(ctx, app.TeamID)
9080
}

0 commit comments

Comments
 (0)