-
Notifications
You must be signed in to change notification settings - Fork 25
feat: output a list of samples for a language with the samples command #192
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
Changes from all commits
0332c01
cae2e65
7fe571c
0cd4233
da1d105
6534c6f
cac13b7
d2e22de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,13 @@ | |
| package project | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/slackapi/slack-cli/internal/api" | ||
| "github.com/slackapi/slack-cli/internal/pkg/create" | ||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/style" | ||
| "github.com/spf13/cobra" | ||
|
|
@@ -26,6 +30,7 @@ import ( | |
| // Flags | ||
| var samplesTemplateURLFlag string | ||
| var samplesGitBranchFlag string | ||
| var samplesListFlag bool | ||
| var samplesLanguageFlag string | ||
|
|
||
| func NewSamplesCommand(clients *shared.ClientFactory) *cobra.Command { | ||
|
|
@@ -35,6 +40,10 @@ func NewSamplesCommand(clients *shared.ClientFactory) *cobra.Command { | |
| Short: "List available sample apps", | ||
| Long: "List and create an app from the available samples", | ||
| Example: style.ExampleCommandsf([]style.ExampleCommand{ | ||
| { | ||
| Meaning: "List Bolt for JavaScript samples", | ||
| Command: "samples --list --language node", | ||
| }, | ||
| { | ||
| Meaning: "Select a sample app to create", | ||
| Command: "samples my-project", | ||
|
|
@@ -50,6 +59,7 @@ func NewSamplesCommand(clients *shared.ClientFactory) *cobra.Command { | |
| cmd.Flags().StringVarP(&samplesTemplateURLFlag, "template", "t", "", "template URL for your app") | ||
| cmd.Flags().StringVarP(&samplesGitBranchFlag, "branch", "b", "", "name of git branch to checkout") | ||
| cmd.Flags().StringVar(&samplesLanguageFlag, "language", "", "runtime for the app framework\n ex: \"deno\", \"node\", \"python\"") | ||
| cmd.Flags().BoolVar(&samplesListFlag, "list", false, "prints samples without interactivity") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
@@ -60,7 +70,18 @@ func runSamplesCommand(clients *shared.ClientFactory, cmd *cobra.Command, args [ | |
| sampler := api.NewHTTPClient(api.HTTPClientOptions{ | ||
| TotalTimeOut: 60 * time.Second, | ||
| }) | ||
| selectedSample, err := PromptSampleSelection(ctx, clients, sampler) | ||
| samples, err := create.GetSampleRepos(sampler) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if samplesListFlag || !clients.IO.IsTTY() { | ||
| err := listSampleSelection(ctx, clients, samples) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
| selectedSample, err := promptSampleSelection(ctx, clients, samples) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -85,3 +106,55 @@ func runSamplesCommand(clients *shared.ClientFactory, cmd *cobra.Command, args [ | |
| // Execute the `create` command with the set flag | ||
| return createCmd.ExecuteContext(ctx) | ||
| } | ||
|
|
||
| // listSampleSelection outputs available samples matching a language flag filter | ||
| func listSampleSelection(ctx context.Context, clients *shared.ClientFactory, sampleRepos []create.GithubRepo) error { | ||
| filteredRepos := filterRepos(sampleRepos, samplesLanguageFlag) | ||
| sortedRepos := sortRepos(filteredRepos) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mwbrooks This confused me too but these might find order from the stars? ⭐ I wonder if showing the number of stars would be helpful? I might expect this to prefix the sample, but am not sure at all if that'd be right: |
||
| templateRepos := []create.GithubRepo{} | ||
| exampleRepos := []create.GithubRepo{} | ||
| for _, repo := range sortedRepos { | ||
| if strings.Contains(repo.FullName, "template") { | ||
| templateRepos = append(templateRepos, repo) | ||
| } else { | ||
| exampleRepos = append(exampleRepos, repo) | ||
| } | ||
| } | ||
| message := "" | ||
| if samplesLanguageFlag != "" { | ||
| message = fmt.Sprintf( | ||
| "Listing %d \"%s\" templates and project samples", | ||
| len(filteredRepos), | ||
| samplesLanguageFlag, | ||
| ) | ||
| } else { | ||
| message = fmt.Sprintf("Listing %d template and project samples", len(filteredRepos)) | ||
| } | ||
| clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{ | ||
| Emoji: "toolbox", | ||
| Text: "Samples", | ||
| Secondary: []string{ | ||
| message, | ||
| }, | ||
| })) | ||
| samples := append( | ||
| templateRepos, | ||
| exampleRepos..., | ||
| ) | ||
| for _, sample := range samples { | ||
| clients.IO.PrintInfo(ctx, false, style.Sectionf(style.TextSection{ | ||
| Emoji: "hammer_and_wrench", | ||
| Text: fmt.Sprintf( | ||
| " %s | %s | %d %s", | ||
| style.Bold(sample.Name), | ||
| sample.Description, | ||
| sample.StargazersCount, | ||
| style.Pluralize("star", "stars", sample.StargazersCount), | ||
| ), | ||
| Secondary: []string{ | ||
| fmt.Sprintf("https://github.com/%s", sample.FullName), | ||
| }, | ||
| })) | ||
| } | ||
| return nil | ||
| } | ||

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.
👁️🗨️ note: We avoid attempting prompts in a non-interactive environment and instead list samples!
🗣️ note: The changes of #193 make this behavior more clear IMO since flag substitutes will then have matching prompts or the
createcommand should be used.