Skip to content

Commit f59f7b7

Browse files
zimegmwbrooks
andauthored
feat: include bolt for javascript and python in the 'samples' command (#139)
Co-authored-by: Michael Brooks <[email protected]>
1 parent da8a61f commit f59f7b7

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

cmd/feedback/feedback.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ func chooseSurveyPrompt(ctx context.Context, clients *shared.ClientFactory, surv
377377
selection, err := clients.IO.SelectPrompt(ctx, msg, surveyPromptOptions,
378378
iostreams.SelectPromptConfig{
379379
Flag: clients.Config.Flags.Lookup("name"),
380+
Required: true,
380381
PageSize: 4,
381382
Description: func(value string, index int) string {
382383
if index < len(surveyNames) {

cmd/project/create_samples.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/slackapi/slack-cli/internal/iostreams"
2525
"github.com/slackapi/slack-cli/internal/pkg/create"
2626
"github.com/slackapi/slack-cli/internal/shared"
27+
"github.com/slackapi/slack-cli/internal/style"
28+
"github.com/spf13/pflag"
2729
)
2830

2931
//go:embed samples.tmpl
@@ -36,13 +38,57 @@ func PromptSampleSelection(ctx context.Context, clients *shared.ClientFactory, s
3638
return "", err
3739
}
3840

39-
projectType := "deno"
40-
filteredRepos := filterRepos(sampleRepos, projectType)
41+
projectTypes := []string{}
42+
selection, err := clients.IO.SelectPrompt(ctx, "Select a language:",
43+
[]string{
44+
fmt.Sprintf("Bolt for JavaScript %s", style.Secondary("Node.js")),
45+
fmt.Sprintf("Bolt for Python %s", style.Secondary("Python")),
46+
fmt.Sprintf("Deno Slack SDK %s", style.Secondary("Deno")),
47+
},
48+
iostreams.SelectPromptConfig{
49+
Flags: []*pflag.Flag{
50+
clients.Config.Flags.Lookup("language"),
51+
clients.Config.Flags.Lookup("template"), // Skip filtering with a template
52+
},
53+
Required: false,
54+
},
55+
)
56+
if err != nil {
57+
return "", err
58+
} else if selection.Prompt {
59+
switch selection.Index {
60+
case 0:
61+
projectTypes = []string{"bolt-js", "bolt-ts"}
62+
case 1:
63+
projectTypes = []string{"bolt-python"}
64+
case 2:
65+
projectTypes = []string{"deno"}
66+
}
67+
} else if selection.Flag {
68+
switch strings.ToLower(strings.TrimSpace(selection.Option)) {
69+
case "node":
70+
projectTypes = []string{"bolt-js", "bolt-ts"}
71+
case "python":
72+
projectTypes = []string{"bolt-python"}
73+
case "deno":
74+
projectTypes = []string{"deno"}
75+
default:
76+
projectTypes = []string{selection.Option}
77+
}
78+
}
79+
80+
filteredRepos := []create.GithubRepo{}
81+
if len(projectTypes) <= 0 {
82+
filteredRepos = sampleRepos
83+
}
84+
for _, language := range projectTypes {
85+
filteredRepos = append(filteredRepos, filterRepos(sampleRepos, language)...)
86+
}
4187
sortedRepos := sortRepos(filteredRepos)
4288
selectOptions := createSelectOptions(sortedRepos)
4389

4490
var selectedTemplate string
45-
selection, err := clients.IO.SelectPrompt(ctx, "Select a sample to build upon:", selectOptions, iostreams.SelectPromptConfig{
91+
selection, err = clients.IO.SelectPrompt(ctx, "Select a sample to build upon:", selectOptions, iostreams.SelectPromptConfig{
4692
Description: func(value string, index int) string {
4793
return sortedRepos[index].Description + "\n https://github.com/" + sortedRepos[index].FullName
4894
},

cmd/project/create_samples_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ func TestSamples_PromptSampleSelection(t *testing.T) {
109109
res := w.Result()
110110
sampler.On("Do", mock.Anything).Return(res, nil)
111111
clientsMock := shared.NewClientsMock()
112+
clientsMock.IO.On(
113+
"SelectPrompt",
114+
mock.Anything,
115+
"Select a language:",
116+
mock.Anything,
117+
mock.Anything,
118+
).Return(iostreams.SelectPromptResponse{
119+
Index: 2,
120+
Prompt: true,
121+
}, nil)
112122
clientsMock.IO.On(
113123
"SelectPrompt",
114124
mock.Anything,

cmd/project/samples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
// Flags
2727
var samplesTemplateURLFlag string
2828
var samplesGitBranchFlag string
29+
var samplesLanguageFlag string
2930

3031
func NewSamplesCommand(clients *shared.ClientFactory) *cobra.Command {
3132
cmd := &cobra.Command{
@@ -45,6 +46,7 @@ func NewSamplesCommand(clients *shared.ClientFactory) *cobra.Command {
4546

4647
cmd.Flags().StringVarP(&samplesTemplateURLFlag, "template", "t", "", "template URL for your app")
4748
cmd.Flags().StringVarP(&samplesGitBranchFlag, "branch", "b", "", "name of git branch to checkout")
49+
cmd.Flags().StringVar(&samplesLanguageFlag, "language", "", "runtime for the app framework\n ex: \"deno\", \"node\", \"python\"")
4850

4951
return cmd
5052
}

cmd/project/samples_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ func TestSamplesCommand(t *testing.T) {
4747
}
4848
return repos, nil
4949
}
50+
cm.IO.On("SelectPrompt", mock.Anything, "Select a language:", mock.Anything, mock.Anything).
51+
Return(
52+
iostreams.SelectPromptResponse{
53+
Index: 2,
54+
Prompt: true,
55+
},
56+
nil,
57+
)
5058
cm.IO.On("SelectPrompt", mock.Anything, "Select a sample to build upon:", mock.Anything, mock.Anything).
5159
Return(
5260
iostreams.SelectPromptResponse{

internal/iostreams/survey.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,11 @@ func (io *IOStreams) SelectPrompt(ctx context.Context, msg string, options []str
447447
return SelectPromptResponse{}, slackerror.New(slackerror.ErrMissingOptions)
448448
}
449449
if !io.IsTTY() {
450-
return SelectPromptResponse{}, errInteractivityFlags(cfg)
450+
if cfg.IsRequired() {
451+
return SelectPromptResponse{}, errInteractivityFlags(cfg)
452+
} else {
453+
return SelectPromptResponse{}, nil
454+
}
451455
}
452456

453457
defaultSelectTemplate := survey.SelectQuestionTemplate
@@ -491,6 +495,9 @@ func (io *IOStreams) retrieveFlagValue(flagset []*pflag.Flag) (*pflag.Flag, erro
491495
return nil, nil
492496
}
493497
for _, opt := range flagset {
498+
if opt == nil {
499+
continue
500+
}
494501
if !opt.Changed {
495502
continue
496503
} else if flag != nil {

0 commit comments

Comments
 (0)