-
Notifications
You must be signed in to change notification settings - Fork 29
feat(charm): adds 'huh' dynamic forms to create command #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srtaalej
wants to merge
2
commits into
main
Choose a base branch
from
charm-dynamic-forms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+317
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // 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 project | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| "github.com/charmbracelet/huh" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/slackerror" | ||
| "github.com/slackapi/slack-cli/internal/slacktrace" | ||
| "github.com/slackapi/slack-cli/internal/style" | ||
| ) | ||
|
|
||
| // templateSelectionResult holds the user's selections from the dynamic template form. | ||
| type templateSelectionResult struct { | ||
| CategoryID string // e.g. "slack-cli#getting-started" or viewMoreSamples | ||
| TemplateRepo string // e.g. "slack-samples/bolt-js-starter-template" | ||
| } | ||
|
|
||
| // charmPromptTemplateSelectionFunc is a package-level function variable for test overriding. | ||
| var charmPromptTemplateSelectionFunc = charmPromptTemplateSelection | ||
|
|
||
| // buildTemplateSelectionForm constructs a single-screen huh form where the category | ||
| // and template selects are in the same group. Changing the category dynamically | ||
| // updates the template options via OptionsFunc. | ||
| func buildTemplateSelectionForm(clients *shared.ClientFactory, category *string, template *string) *huh.Form { | ||
| categoryOptions := getSelectionOptionsForCategory(clients) | ||
| var catOpts []huh.Option[string] | ||
| for _, opt := range categoryOptions { | ||
| catOpts = append(catOpts, huh.NewOption(opt.Title, opt.Repository)) | ||
| } | ||
|
|
||
| categorySelect := huh.NewSelect[string](). | ||
| Title("Select an app:"). | ||
| Options(catOpts...). | ||
| Value(category) | ||
|
|
||
| templateSelect := huh.NewSelect[string](). | ||
| Title("Select a language:"). | ||
| OptionsFunc(func() []huh.Option[string] { | ||
| if *category == viewMoreSamples { | ||
| return []huh.Option[string]{ | ||
| huh.NewOption("Browse sample gallery...", viewMoreSamples), | ||
| } | ||
| } | ||
|
|
||
| options := getSelectionOptions(clients, *category) | ||
| var opts []huh.Option[string] | ||
| for _, opt := range options { | ||
| opts = append(opts, huh.NewOption(opt.Title, opt.Repository)) | ||
| } | ||
| return opts | ||
| }, category). | ||
| Value(template) | ||
|
|
||
| return huh.NewForm( | ||
| huh.NewGroup(categorySelect, templateSelect), | ||
| ).WithTheme(style.ThemeSlack()) | ||
| } | ||
|
|
||
| // charmPromptTemplateSelection runs the dynamic template selection form and returns the result. | ||
| func charmPromptTemplateSelection(ctx context.Context, clients *shared.ClientFactory) (templateSelectionResult, error) { | ||
| // Print trace with category options | ||
| categoryOptions := getSelectionOptionsForCategory(clients) | ||
| categoryTitles := make([]string, len(categoryOptions)) | ||
| for i, opt := range categoryOptions { | ||
| categoryTitles[i] = opt.Title | ||
| } | ||
| clients.IO.PrintTrace(ctx, slacktrace.CreateCategoryOptions, strings.Join(categoryTitles, ", ")) | ||
|
|
||
| var category string | ||
| var template string | ||
| err := buildTemplateSelectionForm(clients, &category, &template).Run() | ||
| if err != nil { | ||
| return templateSelectionResult{}, slackerror.ToSlackError(err) | ||
| } | ||
|
|
||
| // Print trace with template options | ||
| templateOptions := getSelectionOptions(clients, category) | ||
| templateTitles := make([]string, len(templateOptions)) | ||
| for i, opt := range templateOptions { | ||
| templateTitles[i] = opt.Title | ||
| } | ||
| if len(templateTitles) > 0 { | ||
| clients.IO.PrintTrace(ctx, slacktrace.CreateTemplateOptions, strings.Join(templateTitles, ", ")) | ||
| } | ||
|
|
||
| return templateSelectionResult{ | ||
| CategoryID: category, | ||
| TemplateRepo: template, | ||
| }, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // 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 project | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
| "github.com/charmbracelet/huh" | ||
| "github.com/charmbracelet/x/ansi" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // doAllUpdates recursively processes all commands returned by form updates, | ||
| // including batch messages from OptionsFunc evaluations and group transitions. | ||
| // This mirrors the helper in huh's own test suite. | ||
| func doAllUpdates(f *huh.Form, cmd tea.Cmd) { | ||
| if cmd == nil { | ||
| return | ||
| } | ||
| var cmds []tea.Cmd | ||
| switch msg := cmd().(type) { | ||
| case tea.BatchMsg: | ||
| for _, subcommand := range msg { | ||
| doAllUpdates(f, subcommand) | ||
| } | ||
| return | ||
| default: | ||
| _, result := f.Update(msg) | ||
| cmds = append(cmds, result) | ||
| } | ||
| doAllUpdates(f, tea.Batch(cmds...)) | ||
| } | ||
|
|
||
| func TestBuildTemplateSelectionForm(t *testing.T) { | ||
| t.Run("renders category and template on one screen", func(t *testing.T) { | ||
| cm := shared.NewClientsMock() | ||
| cm.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(cm.MockClientFactory()) | ||
|
|
||
| var category, template string | ||
| f := buildTemplateSelectionForm(clients, &category, &template) | ||
| doAllUpdates(f, f.Init()) | ||
|
|
||
| view := ansi.Strip(f.View()) | ||
| assert.Contains(t, view, "Select an app:") | ||
| assert.Contains(t, view, "Starter app") | ||
| assert.Contains(t, view, "AI Agent app") | ||
| assert.Contains(t, view, "Automation app") | ||
| assert.Contains(t, view, "View more samples") | ||
| assert.Contains(t, view, "Select a language:") | ||
| }) | ||
|
|
||
| t.Run("selecting a category updates template options", func(t *testing.T) { | ||
| cm := shared.NewClientsMock() | ||
| cm.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(cm.MockClientFactory()) | ||
|
|
||
| var category, template string | ||
| f := buildTemplateSelectionForm(clients, &category, &template) | ||
| doAllUpdates(f, f.Init()) | ||
|
|
||
| // Submit first option (Starter app -> getting-started) | ||
| _, cmd := f.Update(tea.KeyMsg{Type: tea.KeyEnter}) | ||
| doAllUpdates(f, cmd) | ||
|
|
||
| view := ansi.Strip(f.View()) | ||
| assert.Contains(t, view, "Bolt for JavaScript") | ||
| assert.Contains(t, view, "Bolt for Python") | ||
| }) | ||
|
|
||
| t.Run("selecting view more samples shows browse option", func(t *testing.T) { | ||
| cm := shared.NewClientsMock() | ||
| cm.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(cm.MockClientFactory()) | ||
|
|
||
| var category, template string | ||
| f := buildTemplateSelectionForm(clients, &category, &template) | ||
| doAllUpdates(f, f.Init()) | ||
|
|
||
| // Navigate down to "View more samples" (4th option, index 3) | ||
| _, cmd := f.Update(tea.KeyMsg{Type: tea.KeyDown}) | ||
| doAllUpdates(f, cmd) | ||
| _, cmd = f.Update(tea.KeyMsg{Type: tea.KeyDown}) | ||
| doAllUpdates(f, cmd) | ||
| _, cmd = f.Update(tea.KeyMsg{Type: tea.KeyDown}) | ||
| doAllUpdates(f, cmd) | ||
| _, cmd = f.Update(tea.KeyMsg{Type: tea.KeyEnter}) | ||
| doAllUpdates(f, cmd) | ||
|
|
||
| assert.Equal(t, viewMoreSamples, category) | ||
| view := ansi.Strip(f.View()) | ||
| assert.Contains(t, view, "Browse sample gallery...") | ||
| }) | ||
|
|
||
| t.Run("automation category shows Deno option", func(t *testing.T) { | ||
| cm := shared.NewClientsMock() | ||
| cm.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(cm.MockClientFactory()) | ||
|
|
||
| var category, template string | ||
| f := buildTemplateSelectionForm(clients, &category, &template) | ||
| doAllUpdates(f, f.Init()) | ||
|
|
||
| // Navigate to Automation app (3rd option, index 2) and submit | ||
| _, cmd := f.Update(tea.KeyMsg{Type: tea.KeyDown}) | ||
| doAllUpdates(f, cmd) | ||
| _, cmd = f.Update(tea.KeyMsg{Type: tea.KeyDown}) | ||
| doAllUpdates(f, cmd) | ||
| _, cmd = f.Update(tea.KeyMsg{Type: tea.KeyEnter}) | ||
| doAllUpdates(f, cmd) | ||
|
|
||
| view := ansi.Strip(f.View()) | ||
| assert.Contains(t, view, "Deno Slack SDK") | ||
| }) | ||
|
|
||
| t.Run("complete flow selects a template", func(t *testing.T) { | ||
| cm := shared.NewClientsMock() | ||
| cm.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(cm.MockClientFactory()) | ||
|
|
||
| var category, template string | ||
| f := buildTemplateSelectionForm(clients, &category, &template) | ||
| doAllUpdates(f, f.Init()) | ||
|
|
||
| // Select first category (Starter app) | ||
| _, cmd := f.Update(tea.KeyMsg{Type: tea.KeyEnter}) | ||
| doAllUpdates(f, cmd) | ||
| // Select first template (Bolt for JavaScript) | ||
| _, cmd = f.Update(tea.KeyMsg{Type: tea.KeyEnter}) | ||
| doAllUpdates(f, cmd) | ||
|
|
||
| assert.Equal(t, "slack-cli#getting-started", category) | ||
| assert.Equal(t, "slack-samples/bolt-js-starter-template", template) | ||
| }) | ||
|
|
||
| t.Run("uses Slack theme", func(t *testing.T) { | ||
| cm := shared.NewClientsMock() | ||
| cm.AddDefaultMocks() | ||
| clients := shared.NewClientFactory(cm.MockClientFactory()) | ||
|
|
||
| var category, template string | ||
| f := buildTemplateSelectionForm(clients, &category, &template) | ||
| doAllUpdates(f, f.Init()) | ||
|
|
||
| view := f.View() | ||
| assert.Contains(t, view, "┃") | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Do we need this testing override? We are trying to remove this pattern and a glance at
charmPromptTemplateSelectionlooks like we can test this function safely sinceclientsandclients.IOhave mocks available. 🤞🏻There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ill look into this!