-
Notifications
You must be signed in to change notification settings - Fork 28
feat: adds docs command with optional search flag
#352
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
base: main
Are you sure you want to change the base?
Changes from all commits
90dff7c
c252cd7
5fd56a2
45396a6
2c2c5e8
6af0af7
04854d0
aad04d0
9c8bb4c
1d12927
29ee8ee
35153a9
7a2b2b9
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||
| // 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 docs | ||||||
|
|
||||||
| import ( | ||||||
| "fmt" | ||||||
| "net/url" | ||||||
| "strings" | ||||||
|
|
||||||
| "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" | ||||||
| "github.com/spf13/cobra" | ||||||
| ) | ||||||
|
|
||||||
| var searchMode bool | ||||||
|
|
||||||
| func NewCommand(clients *shared.ClientFactory) *cobra.Command { | ||||||
| cmd := &cobra.Command{ | ||||||
| Use: "docs", | ||||||
| Short: "Open Slack developer docs", | ||||||
| Long: "Open the Slack developer docs in your browser, with optional search functionality", | ||||||
| Example: style.ExampleCommandsf([]style.ExampleCommand{ | ||||||
| { | ||||||
| Meaning: "Open Slack developer docs homepage", | ||||||
| Command: "docs", | ||||||
| }, | ||||||
| { | ||||||
| Meaning: "Search Slack developer docs for Block Kit", | ||||||
| Command: "docs --search \"Block Kit\"", | ||||||
| }, | ||||||
| { | ||||||
| Meaning: "Open Slack docs search page", | ||||||
| Command: "docs --search", | ||||||
| }, | ||||||
| }), | ||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||
| return runDocsCommand(clients, cmd, args) | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| cmd.Flags().BoolVar(&searchMode, "search", false, "open Slack docs search page or search with query") | ||||||
|
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.
Suggested change
🪓 suggestion(non-blocking): We can perhaps remove |
||||||
|
|
||||||
| return cmd | ||||||
| } | ||||||
|
|
||||||
| // runDocsCommand opens Slack developer docs in the browser | ||||||
| func runDocsCommand(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error { | ||||||
| ctx := cmd.Context() | ||||||
|
|
||||||
| var docsURL string | ||||||
| var sectionText string | ||||||
|
|
||||||
| // Validate: if there are arguments, --search flag must be used | ||||||
| if len(args) > 0 && !cmd.Flags().Changed("search") { | ||||||
|
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. 🪬 suggestion: We might still want to keep a max arguments setting for this command, but set to Although we can also update this section with "variadic" arguments to join the arguments together - IMHO this might be ideal if not so complicated 👻
Contributor
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. it now joins the arguments together!
|
||||||
| query := strings.Join(args, " ") | ||||||
| return slackerror.New(slackerror.ErrDocsSearchFlagRequired).WithRemediation( | ||||||
| "Use --search flag: %s", | ||||||
| style.Commandf(fmt.Sprintf("docs --search \"%s\"", query), false), | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| if cmd.Flags().Changed("search") { | ||||||
| if len(args) > 0 { | ||||||
| // --search "query" (space-separated) - join all args as the query | ||||||
| query := strings.Join(args, " ") | ||||||
| encodedQuery := url.QueryEscape(query) | ||||||
| docsURL = fmt.Sprintf("https://docs.slack.dev/search/?q=%s", encodedQuery) | ||||||
| sectionText = "Docs Search" | ||||||
| } else { | ||||||
| // --search (no argument) - open search page | ||||||
| docsURL = "https://docs.slack.dev/search/" | ||||||
| sectionText = "Docs Search" | ||||||
| } | ||||||
| } else { | ||||||
| // No search flag: default homepage | ||||||
| docsURL = "https://docs.slack.dev" | ||||||
| sectionText = "Docs Open" | ||||||
| } | ||||||
|
|
||||||
| clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{ | ||||||
| Emoji: "books", | ||||||
| Text: sectionText, | ||||||
| Secondary: []string{ | ||||||
| docsURL, | ||||||
| }, | ||||||
| })) | ||||||
lukegalbraithrussell marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| clients.Browser().OpenURL(docsURL) | ||||||
|
|
||||||
| if cmd.Flags().Changed("search") { | ||||||
| traceValue := "" | ||||||
| if len(args) > 0 { | ||||||
| traceValue = strings.Join(args, " ") | ||||||
| } | ||||||
| clients.IO.PrintTrace(ctx, slacktrace.DocsSearchSuccess, traceValue) | ||||||
| } else { | ||||||
| clients.IO.PrintTrace(ctx, slacktrace.DocsSuccess) | ||||||
| } | ||||||
|
|
||||||
| return nil | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // 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 docs | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/slacktrace" | ||
| "github.com/slackapi/slack-cli/test/testutil" | ||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| func Test_Docs_DocsCommand(t *testing.T) { | ||
lukegalbraithrussell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| testutil.TableTestCommand(t, testutil.CommandTests{ | ||
| "opens docs homepage without search": { | ||
| Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { | ||
| }, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| expectedURL := "https://docs.slack.dev" | ||
| cm.Browser.AssertCalled(t, "OpenURL", expectedURL) | ||
| cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.DocsSuccess, mock.Anything) | ||
| }, | ||
| ExpectedOutputs: []string{ | ||
| "Docs Open", | ||
| "https://docs.slack.dev", | ||
| }, | ||
| }, | ||
| "fails when positional argument provided without search flag": { | ||
| CmdArgs: []string{"Block Kit"}, | ||
| ExpectedErrorStrings: []string{"Invalid docs command. Did you mean to search?"}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| // No browser calls should be made when command fails | ||
| cm.Browser.AssertNotCalled(t, "OpenURL") | ||
| }, | ||
| }, | ||
| "fails when multiple positional arguments provided without search flag": { | ||
| CmdArgs: []string{"webhook", "send", "message"}, | ||
| ExpectedErrorStrings: []string{"Invalid docs command. Did you mean to search?"}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| // No browser calls should be made when command fails | ||
| cm.Browser.AssertNotCalled(t, "OpenURL") | ||
| }, | ||
| }, | ||
| "opens docs with search query using space syntax": { | ||
| CmdArgs: []string{"--search", "messaging"}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| expectedURL := "https://docs.slack.dev/search/?q=messaging" | ||
| cm.Browser.AssertCalled(t, "OpenURL", expectedURL) | ||
| cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.DocsSearchSuccess, mock.Anything) | ||
| }, | ||
| ExpectedOutputs: []string{ | ||
| "Docs Search", | ||
| "https://docs.slack.dev/search/?q=messaging", | ||
| }, | ||
| }, | ||
| "handles search with multiple arguments": { | ||
| CmdArgs: []string{"--search", "Block", "Kit", "Element"}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| expectedURL := "https://docs.slack.dev/search/?q=Block+Kit+Element" | ||
| cm.Browser.AssertCalled(t, "OpenURL", expectedURL) | ||
| cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.DocsSearchSuccess, mock.Anything) | ||
| }, | ||
| ExpectedOutputs: []string{ | ||
| "Docs Search", | ||
| "https://docs.slack.dev/search/?q=Block+Kit+Element", | ||
| }, | ||
| }, | ||
| "handles search query with multiple words": { | ||
| CmdArgs: []string{"--search", "socket mode"}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| expectedURL := "https://docs.slack.dev/search/?q=socket+mode" | ||
| cm.Browser.AssertCalled(t, "OpenURL", expectedURL) | ||
| cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.DocsSearchSuccess, mock.Anything) | ||
| }, | ||
| ExpectedOutputs: []string{ | ||
| "Docs Search", | ||
| "https://docs.slack.dev/search/?q=socket+mode", | ||
| }, | ||
| }, | ||
| "handles special characters in search query": { | ||
| CmdArgs: []string{"--search", "messages & webhooks"}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| expectedURL := "https://docs.slack.dev/search/?q=messages+%26+webhooks" | ||
| cm.Browser.AssertCalled(t, "OpenURL", expectedURL) | ||
| cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.DocsSearchSuccess, mock.Anything) | ||
| }, | ||
| ExpectedOutputs: []string{ | ||
| "Docs Search", | ||
| "https://docs.slack.dev/search/?q=messages+%26+webhooks", | ||
| }, | ||
| }, | ||
| "handles search query with quotes": { | ||
| CmdArgs: []string{"--search", "webhook \"send message\""}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| expectedURL := "https://docs.slack.dev/search/?q=webhook+%22send+message%22" | ||
| cm.Browser.AssertCalled(t, "OpenURL", expectedURL) | ||
| cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.DocsSearchSuccess, mock.Anything) | ||
| }, | ||
| ExpectedOutputs: []string{ | ||
| "Docs Search", | ||
| "https://docs.slack.dev/search/?q=webhook+%22send+message%22", | ||
| }, | ||
| }, | ||
| "handles search flag without argument": { | ||
| CmdArgs: []string{"--search"}, | ||
| ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { | ||
| expectedURL := "https://docs.slack.dev/search/" | ||
| cm.Browser.AssertCalled(t, "OpenURL", expectedURL) | ||
| cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.DocsSearchSuccess, mock.Anything) | ||
| }, | ||
| ExpectedOutputs: []string{ | ||
| "Docs Search", | ||
| "https://docs.slack.dev/search/", | ||
| }, | ||
| }, | ||
| }, func(cf *shared.ClientFactory) *cobra.Command { | ||
| return NewCommand(cf) | ||
| }) | ||
| } | ||
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.
🪓 suggesetion(non-blocking): Related to the earlier comment!