Skip to content
Merged
128 changes: 64 additions & 64 deletions cmd/app/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewLinkCommand(clients *shared.ClientFactory) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
clients.IO.PrintTrace(ctx, slacktrace.AppLinkStart)
return LinkCommandRunE(ctx, clients, app)
return LinkCommandRunE(ctx, clients, app, false, false)
},
PostRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
Expand All @@ -93,15 +93,71 @@ func NewLinkCommand(clients *shared.ClientFactory) *cobra.Command {
}

// LinkCommandRunE saves details about the provided application
func LinkCommandRunE(ctx context.Context, clients *shared.ClientFactory, app *types.App) (err error) {
// Add empty line between executed command and first output
clients.IO.PrintInfo(ctx, false, "")
func LinkCommandRunE(ctx context.Context, clients *shared.ClientFactory, app *types.App, shouldConfirm bool, quiet bool) (err error) {
if !quiet {
// Add empty line between executed command and first output
clients.IO.PrintInfo(ctx, false, "")
// Header section
LinkAppHeaderSection(ctx, clients, shouldConfirm)
}

// Confirm to add an existing app; useful for third-party callers
if shouldConfirm {
proceed, err := clients.IO.ConfirmPrompt(ctx, LinkAppConfirmPromptText, true)
if err != nil {
clients.IO.PrintDebug(ctx, "Error prompting to add an existing app: %s", err)
return err
}

// Add newline to match the trailing newline inserted from the footer section
clients.IO.PrintInfo(ctx, false, "")

if !proceed {
return nil
}
}

err = LinkExistingApp(ctx, clients, app, false)
if !quiet {
// App Manifest section
manifestSource, err := clients.Config.ProjectConfig.GetManifestSource(ctx)
if err != nil {
return err
}

configPath := filepath.Join(config.ProjectConfigDirName, config.ProjectConfigJSONFilename)
clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{
Emoji: "books",
Text: "App Manifest",
Secondary: []string{
"Manifest source is gathered from " + style.Highlight(manifestSource.Human()),
"Manifest source is configured in " + style.Highlight(configPath),
},
}))
}

err = LinkExistingApp(ctx, clients, app)
if err != nil {
return err
}

// App summary section
clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{
Emoji: "house",
Text: "App",
Secondary: formatListSuccess([]types.App{*app}),
}))
Comment thread
zimeg marked this conversation as resolved.

if !quiet {
// Footer section
clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{
Emoji: "house_with_garden",
Text: "App Link",
Secondary: []string{
"Added existing app to project",
},
}))
}

return nil
}

Expand All @@ -126,46 +182,9 @@ func LinkAppHeaderSection(ctx context.Context, clients *shared.ClientFactory, sh
}))
}

// LinkExistingApp prompts for an existing App ID and saves the details to the project.
// When shouldConfirm is true, a confirmation prompt will ask the user if they want to
// link an existing app and additional information is included in the header.
// The shouldConfirm option is encouraged for third-party callers.
func LinkExistingApp(ctx context.Context, clients *shared.ClientFactory, app *types.App, shouldConfirm bool) (err error) {
// Header section
LinkAppHeaderSection(ctx, clients, shouldConfirm)

// Confirm to add an existing app; useful for third-party callers
if shouldConfirm {
proceed, err := clients.IO.ConfirmPrompt(ctx, LinkAppConfirmPromptText, true)
if err != nil {
clients.IO.PrintDebug(ctx, "Error prompting to add an existing app: %s", err)
return err
}

// Add newline to match the trailing newline inserted from the footer section
clients.IO.PrintInfo(ctx, false, "")

if !proceed {
return nil
}
}

// App Manifest section
manifestSource, err := clients.Config.ProjectConfig.GetManifestSource(ctx)
if err != nil {
return err
}

configPath := filepath.Join(config.ProjectConfigDirName, config.ProjectConfigJSONFilename)
clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{
Emoji: "books",
Text: "App Manifest",
Secondary: []string{
"Manifest source is gathered from " + style.Highlight(manifestSource.Human()),
"Manifest source is configured in " + style.Highlight(configPath),
},
}))

// LinkExistingApp resolves app details, validates the app, and saves it to the
// project. It produces no output — callers handle their own display.
func LinkExistingApp(ctx context.Context, clients *shared.ClientFactory, app *types.App) (err error) {
// Prompt to get app details
var auth *types.SlackAuth
*app, auth, err = promptExistingApp(ctx, clients)
Expand All @@ -186,28 +205,9 @@ func LinkExistingApp(ctx context.Context, clients *shared.ClientFactory, app *ty
return err
}

// Footer section
LinkAppFooterSection(ctx, clients, app)

return nil
}

// LinkAppFooterSection displays the details of app that was added to the project.
func LinkAppFooterSection(ctx context.Context, clients *shared.ClientFactory, app *types.App) {
clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{
Emoji: "house",
Text: "App",
Secondary: formatListSuccess([]types.App{*app}),
}))
clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{
Emoji: "house_with_garden",
Text: "App Link",
Secondary: []string{
"Added existing app to project",
},
}))
}

// promptExistingApp gathers details to represent app information
func promptExistingApp(ctx context.Context, clients *shared.ClientFactory) (types.App, *types.SlackAuth, error) {
slackAuth, err := prompts.PromptTeamSlackAuth(ctx, clients, "Select the existing app team", nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func runCreateCommand(clients *shared.ClientFactory, cmd *cobra.Command, args []
defer func() {
_ = os.Chdir(originalDir)
}()
if err := app.LinkExistingApp(ctx, clients, &types.App{}, false); err != nil {
if err := app.LinkCommandRunE(ctx, clients, &types.App{}, false, true); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/project/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func projectInitCommandRunE(clients *shared.ClientFactory, cmd *cobra.Command, a
_ = create.InstallProjectDependencies(ctx, clients, projectDirPath)

// Add an existing app to the project
err = app.LinkExistingApp(ctx, clients, &types.App{}, true)
err = app.LinkCommandRunE(ctx, clients, &types.App{}, true, false)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 thought: No blocker but we might want to refactor the confirmation prompt to be here instead of within the link command?

⚡ ramble: I find boolean arguments unclear and also think a command should run if it's called - it'd be up to the caller to decide if that command should run instead!

if err != nil {
// Display the error but continue to init
clients.IO.PrintError(ctx, "%s", err.Error())
Expand Down
Loading