diff --git a/cmd/app/add.go b/cmd/app/add.go index 39b4623c..a63a0e15 100644 --- a/cmd/app/add.go +++ b/cmd/app/add.go @@ -16,13 +16,11 @@ 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" @@ -186,11 +184,8 @@ func RunAddCommand(ctx context.Context, clients *shared.ClientFactory, selection 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) + installedApp, installState, err := appInstall(ctx, clients, selection, orgGrantWorkspaceID) if err != nil { return ctx, installState, types.App{}, err // pass the installState because some callers may use it to handle the error } @@ -201,74 +196,19 @@ func RunAddCommand(ctx context.Context, clients *shared.ClientFactory, selection 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) { +func appInstall(ctx context.Context, clients *shared.ClientFactory, 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) + installedApp, _, installState, err := appInstallDevAppFunc(ctx, clients, "", selection.Auth, selection.App) return installedApp, installState, err } else { - installState, installedApp, err := appInstallProdAppFunc(ctx, clients, log, selection.Auth, selection.App, orgGrantWorkspaceID) + installState, installedApp, err := appInstallProdAppFunc(ctx, clients, selection.Auth, selection.App, orgGrantWorkspaceID) return installedApp, installState, err } } diff --git a/cmd/function/mocks_test.go b/cmd/function/mocks_test.go index b8e3c64d..fdf30eda 100644 --- a/cmd/function/mocks_test.go +++ b/cmd/function/mocks_test.go @@ -17,7 +17,6 @@ package function_test import ( "context" - "github.com/slackapi/slack-cli/internal/logger" "github.com/slackapi/slack-cli/internal/shared" "github.com/slackapi/slack-cli/internal/shared/types" "github.com/stretchr/testify/mock" @@ -27,22 +26,22 @@ type FunctionDistributorMock struct { mock.Mock } -func (m *FunctionDistributorMock) List(ctx context.Context, clients *shared.ClientFactory, fn string, log *logger.Logger) (types.Permission, []types.FunctionDistributionUser, error) { +func (m *FunctionDistributorMock) List(ctx context.Context, clients *shared.ClientFactory, fn string) (types.Permission, []types.FunctionDistributionUser, error) { args := m.Called() return args.Get(0).(types.Permission), args.Get(1).([]types.FunctionDistributionUser), args.Error(2) } -func (m *FunctionDistributorMock) Set(ctx context.Context, clients *shared.ClientFactory, function, distributionType, users string, log *logger.Logger) (string, error) { +func (m *FunctionDistributorMock) Set(ctx context.Context, clients *shared.ClientFactory, function, distributionType, users string) (string, error) { args := m.Called() return args.String(0), args.Error(1) } -func (m *FunctionDistributorMock) AddUsers(ctx context.Context, clients *shared.ClientFactory, function, users string, log *logger.Logger) (string, error) { +func (m *FunctionDistributorMock) AddUsers(ctx context.Context, clients *shared.ClientFactory, function, users string) (string, error) { args := m.Called() return args.String(0), args.Error(1) } -func (m *FunctionDistributorMock) RemoveUsers(ctx context.Context, clients *shared.ClientFactory, function, users string, log *logger.Logger) (string, error) { +func (m *FunctionDistributorMock) RemoveUsers(ctx context.Context, clients *shared.ClientFactory, function, users string) (string, error) { args := m.Called() return args.String(0), args.Error(1) } diff --git a/internal/pkg/apps/add.go b/internal/pkg/apps/add.go index 421fa940..81e2462d 100644 --- a/internal/pkg/apps/add.go +++ b/internal/pkg/apps/add.go @@ -19,35 +19,31 @@ import ( "strings" "github.com/opentracing/opentracing-go" - "github.com/slackapi/slack-cli/internal/logger" "github.com/slackapi/slack-cli/internal/shared" "github.com/slackapi/slack-cli/internal/shared/types" "github.com/slackapi/slack-cli/internal/slackerror" ) // Add will add an app -func Add(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) { +func Add(ctx context.Context, clients *shared.ClientFactory, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) { span, _ := opentracing.StartSpanFromContext(ctx, "pkg.apps.add") defer span.Finish() // Validate the auth - ctx, authSession, err := getAuthSession(ctx, clients, auth) + ctx, _, err := getAuthSession(ctx, clients, auth) if err != nil { return "", types.App{}, slackerror.Wrap(err, slackerror.ErrAddAppToProject) } - log.Data["teamName"] = *authSession.TeamName - - log.Info("on_apps_add_init") // Add app remotely via Slack API - installState, app, err := addAppRemotely(ctx, clients, log, auth, app, orgGrantWorkspaceID) + installState, app, err := addAppRemotely(ctx, clients, auth, app, orgGrantWorkspaceID) if err != nil { return "", types.App{}, slackerror.Wrap(err, slackerror.ErrAddAppToProject) } // Add app to apps.json if !clients.Config.SkipLocalFs() { - if _, err := addAppLocally(ctx, clients, log, app); err != nil { + if _, err := addAppLocally(ctx, clients, app); err != nil { return installState, types.App{}, slackerror.Wrap(err, slackerror.ErrAddAppToProject) } } @@ -56,9 +52,7 @@ func Add(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, } // addAppLocally will add the app to the project's apps file with an empty AppID, TeamID, etc -func addAppLocally(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, app types.App) (types.App, error) { - log.Info("on_apps_add_local") - +func addAppLocally(ctx context.Context, clients *shared.ClientFactory, app types.App) (types.App, error) { app, err := clients.AppClient().NewDeployed(ctx, app.TeamID) if err != nil { 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 } // addAppRemotely will create the app manifest using the current auth account's team -func addAppRemotely(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) { - log.Info("on_apps_add_remote_init") - +func addAppRemotely(ctx context.Context, clients *shared.ClientFactory, auth types.SlackAuth, app types.App, orgGrantWorkspaceID string) (types.InstallState, types.App, error) { if app.TeamID == "" { // App hasn't been created yet and // so the target team ID set by the auth app.TeamID = auth.TeamID } - app, installState, err := Install(ctx, clients, log, auth, CreateAppManifestAndInstall, app, orgGrantWorkspaceID) + app, installState, err := Install(ctx, clients, auth, CreateAppManifestAndInstall, app, orgGrantWorkspaceID) if err != nil { return installState, types.App{}, slackerror.Wrap(err, slackerror.ErrAppAdd) } - log.Info("on_apps_add_remote_success") - if !clients.Config.SkipLocalFs() { app, err = clients.AppClient().GetDeployed(ctx, app.TeamID) } diff --git a/internal/pkg/apps/install.go b/internal/pkg/apps/install.go index 7bfe922a..b27b7992 100644 --- a/internal/pkg/apps/install.go +++ b/internal/pkg/apps/install.go @@ -25,7 +25,6 @@ import ( "github.com/slackapi/slack-cli/internal/api" "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/manifest" "github.com/slackapi/slack-cli/internal/shared" "github.com/slackapi/slack-cli/internal/shared/types" @@ -42,7 +41,7 @@ const ( const additionalManifestInfoNotice = "App manifest contains some components that may require additional information" // Install installs the app to a team -func Install(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, auth types.SlackAuth, onlyCreateUpdateAppManifest bool, app types.App, orgGrantWorkspaceID string) (types.App, types.InstallState, error) { +func Install(ctx context.Context, clients *shared.ClientFactory, auth types.SlackAuth, onlyCreateUpdateAppManifest bool, app types.App, orgGrantWorkspaceID string) (types.App, types.InstallState, error) { span, ctx := opentracing.StartSpanFromContext(ctx, "pkg.apps.install") defer span.Finish() @@ -114,11 +113,6 @@ func Install(ctx context.Context, clients *shared.ClientFactory, log *logger.Log } } - log.Data["appName"] = slackManifest.DisplayInformation.Name - log.Data["isUpdate"] = app.AppID != "" - log.Data["teamName"] = *authSession.TeamName - log.Log("INFO", "app_install_manifest") - manifest := slackManifest.AppManifest if slackManifest.IsFunctionRuntimeSlackHosted() { configureHostedManifest(ctx, clients, &manifest) @@ -132,14 +126,26 @@ func Install(ctx context.Context, clients *shared.ClientFactory, log *logger.Log start := time.Now() switch { case manifestUpdates: - log.Info("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"`, slackManifest.DisplayInformation.Name, *authSession.TeamName), + }, + }))) clients.IO.PrintDebug(ctx, "updating app %s", app.AppID) _, err := apiInterface.UpdateApp(ctx, token, app.AppID, manifest, clients.Config.ForceFlag, true) if err != nil { return app, "", err } case manifestCreates: - log.Info("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"`, slackManifest.DisplayInformation.Name, *authSession.TeamName), + }, + }))) clients.IO.PrintDebug(ctx, "app not found so creating a new app") result, err := apiInterface.CreateApp(ctx, token, manifest, false) if err != nil { @@ -156,10 +162,6 @@ func Install(ctx context.Context, clients *shared.ClientFactory, log *logger.Log // app.EnterpriseID = config.GetContextEnterpriseID(ctx) } - appManageURL := fmt.Sprintf("%s/apps", apiInterface.Host()) - log.Data["appURL"] = fmt.Sprintf("%s%s", appManageURL, app.AppID) - log.Data["appName"] = manifest.DisplayInformation.Name - if !clients.Config.SkipLocalFs() { if err := clients.AppClient().SaveDeployed(ctx, app); err != nil { return types.App{}, "", err @@ -205,7 +207,13 @@ func Install(ctx context.Context, clients *shared.ClientFactory, log *logger.Log outgoingDomains = *manifest.OutgoingDomains } - log.Info("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"`, manifest.DisplayInformation.Name, *authSession.TeamName), + }, + }))) // Note - we use DeveloperAppInstall endpoint for both local (dev) runs // and hosted installs https://github.com/slackapi/slack-cli/pull/456#discussion_r830272175 @@ -234,14 +242,12 @@ func Install(ctx context.Context, clients *shared.ClientFactory, log *logger.Log } } if iconPath != "" { - log.Data["iconPath"] = iconPath err = updateIcon(ctx, clients, iconPath, app.AppID, token) if err != nil { clients.IO.PrintDebug(ctx, "icon error: %s", err) - log.Data["iconError"] = err.Error() - log.Info("app_install_icon_error") + _, _ = clients.IO.WriteOut().Write([]byte(style.SectionSecondaryf("Error updating app icon: %s", err))) } else { - log.Info("app_install_icon_success") + _, _ = clients.IO.WriteOut().Write([]byte(style.SectionSecondaryf("Updated app icon: %s", iconPath))) } // TODO: Optimization. // Save a md5 hash of the icon in environments.yaml @@ -255,8 +261,7 @@ func Install(ctx context.Context, clients *shared.ClientFactory, log *logger.Log // update config with latest yaml hash // env.Hash = slackYaml.Hash - log.Data["installTime"] = fmt.Sprintf("%.1fs", time.Since(start).Seconds()) - log.Info("app_install_complete") + _, _ = clients.IO.WriteOut().Write([]byte(style.SectionSecondaryf("Finished in %.1fs", time.Since(start).Seconds()))) return app, types.InstallSuccess, nil } @@ -356,7 +361,7 @@ func validateManifestForInstall(ctx context.Context, clients *shared.ClientFacto } // InstallLocalApp installs a non-hosted local app to a workspace. -func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGrantWorkspaceID string, log *logger.Logger, auth types.SlackAuth, app types.App) (types.App, api.DeveloperAppInstallResult, types.InstallState, error) { +func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGrantWorkspaceID string, auth types.SlackAuth, app types.App) (types.App, api.DeveloperAppInstallResult, types.InstallState, error) { span, ctx := opentracing.StartSpanFromContext(ctx, "installLocalApp") defer span.Finish() @@ -428,11 +433,6 @@ func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGran } } - log.Data["appName"] = slackManifest.DisplayInformation.Name - log.Data["isUpdate"] = app.AppID != "" - log.Data["teamName"] = *authSession.TeamName - log.Log("INFO", "app_install_manifest") - manifest := slackManifest.AppManifest appendLocalToDisplayName(&manifest) if manifest.IsFunctionRuntimeSlackHosted() { @@ -447,8 +447,13 @@ func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGran start := time.Now() switch { case manifestUpdates: - log.Info("app_install_manifest_update") - log.Info("on_update_app_install") + _, _ = 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"`, slackManifest.DisplayInformation.Name, *authSession.TeamName), + }, + }))) clients.IO.PrintDebug(ctx, "updating app %s", app.AppID) _, err := apiInterface.UpdateApp(ctx, token, app.AppID, manifest, clients.Config.ForceFlag, true) if err != nil { @@ -456,7 +461,13 @@ func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGran return app, api.DeveloperAppInstallResult{}, "", err } case manifestCreates: - log.Info("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"`, slackManifest.DisplayInformation.Name, *authSession.TeamName), + }, + }))) clients.IO.PrintDebug(ctx, "app not found so creating a new app") result, err := apiInterface.CreateApp(ctx, token, manifest, false) if err != nil { @@ -474,10 +485,6 @@ func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGran app.UserID = *authSession.UserID } - appManageURL := fmt.Sprintf("%s/apps", apiInterface.Host()) - log.Data["appURL"] = fmt.Sprintf("%s%s", appManageURL, app.AppID) - log.Data["appName"] = manifest.DisplayInformation.Name - // specifically set app.IsDev to be true for dev installation app.IsDev = true @@ -523,7 +530,13 @@ func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGran outgoingDomains = *manifest.OutgoingDomains } - log.Info("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"`, manifest.DisplayInformation.Name, *authSession.TeamName), + }, + }))) var installState types.InstallState result, installState, err := apiInterface.DeveloperAppInstall(ctx, clients.IO, token, app, botScopes, outgoingDomains, orgGrantWorkspaceID, clients.Config.AutoRequestAAAFlag) @@ -569,8 +582,7 @@ func InstallLocalApp(ctx context.Context, clients *shared.ClientFactory, orgGran // update config with latest yaml hash // env.Hash = slackYaml.Hash - log.Data["installTime"] = fmt.Sprintf("%.1fs", time.Since(start).Seconds()) - log.Info("app_install_complete") + _, _ = clients.IO.WriteOut().Write([]byte(style.SectionSecondaryf("Finished in %.1fs", time.Since(start).Seconds()))) return app, result, types.InstallSuccess, nil } diff --git a/internal/pkg/apps/install_test.go b/internal/pkg/apps/install_test.go index 60503614..bf0b190c 100644 --- a/internal/pkg/apps/install_test.go +++ b/internal/pkg/apps/install_test.go @@ -23,7 +23,6 @@ import ( "github.com/slackapi/slack-cli/internal/cache" "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/shared" "github.com/slackapi/slack-cli/internal/shared/types" "github.com/slackapi/slack-cli/internal/slackcontext" @@ -625,12 +624,10 @@ func TestInstall(t *testing.T) { mockProjectConfig.On("Cache").Return(mockProjectCache) clientsMock.Config.ProjectConfig = mockProjectConfig - log := logger.New(func(event *logger.LogEvent) {}) clients := shared.NewClientFactory(clientsMock.MockClientFactory()) app, state, err := Install( ctx, clients, - log, tc.mockAuth, false, tc.mockApp, @@ -1453,13 +1450,11 @@ func TestInstallLocalApp(t *testing.T) { mockProjectConfig.On("Cache").Return(mockProjectCache) clientsMock.Config.ProjectConfig = mockProjectConfig - log := logger.New(func(event *logger.LogEvent) {}) clients := shared.NewClientFactory(clientsMock.MockClientFactory()) app, _, state, err := InstallLocalApp( ctx, clients, tc.mockOrgGrantWorkspaceID, - log, tc.mockAuth, tc.mockApp, ) diff --git a/internal/pkg/platform/localserver.go b/internal/pkg/platform/localserver.go index 5834340c..4806e57f 100644 --- a/internal/pkg/platform/localserver.go +++ b/internal/pkg/platform/localserver.go @@ -414,7 +414,7 @@ func (r *LocalServer) WatchManifest(ctx context.Context, auth types.SlackAuth, a r.log.Info("on_cloud_run_watch_manifest_change") // Reinstall the app when manifest changes - if _, _, _, err := apps.InstallLocalApp(ctx, r.clients, "", r.log, auth, app); err != nil { + if _, _, _, err := apps.InstallLocalApp(ctx, r.clients, "", auth, app); err != nil { r.log.Data["cloud_run_watch_error"] = err.Error() r.log.Warn("on_cloud_run_watch_error") } else { diff --git a/internal/pkg/platform/run.go b/internal/pkg/platform/run.go index 4a9cf36d..127f46d7 100644 --- a/internal/pkg/platform/run.go +++ b/internal/pkg/platform/run.go @@ -77,7 +77,7 @@ func Run(ctx context.Context, clients *shared.ClientFactory, log *logger.Logger, } // Update local install - installedApp, localInstallResult, installState, err := apps.InstallLocalApp(ctx, clients, runArgs.OrgGrantWorkspaceID, log, runArgs.Auth, runArgs.App) + installedApp, localInstallResult, installState, err := apps.InstallLocalApp(ctx, clients, runArgs.OrgGrantWorkspaceID, runArgs.Auth, runArgs.App) if err != nil { return nil, "", slackerror.Wrap(err, slackerror.ErrLocalAppRun) }