-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from 9 commits
90dff7c
c252cd7
5fd56a2
45396a6
2c2c5e8
6af0af7
04854d0
aad04d0
9c8bb4c
1d12927
29ee8ee
35153a9
7a2b2b9
2634721
030644f
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,90 @@ | ||
| // 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" | ||
|
|
||
| "github.com/slackapi/slack-cli/internal/shared" | ||
| "github.com/slackapi/slack-cli/internal/slacktrace" | ||
| "github.com/slackapi/slack-cli/internal/style" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var searchFlag string | ||
|
|
||
| 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", | ||
| Command: "docs --search 'Block Kit'", | ||
| }, | ||
| }), | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return runDocsCommand(clients, cmd, args) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&searchFlag, "search", "", "search query for Slack docs") | ||
|
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: Related to the "required" search value if the "--search" flag is provided - I wonder if we can change this to be a "boolean" value? This might allow this command to accept 1 argument instead of 0 for sake of being the search query? We might then search if that flag is provided or if an argument is used: We ought not need to document the second case IMHO since it's identical to the third, and I don't think such approach limits future changes. Not a blocker here but the examples might need updating before merge! |
||
|
|
||
| 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 | ||
|
|
||
| if searchFlag != "" { | ||
| // Build search URL | ||
| searchQuery := url.QueryEscape(searchFlag) | ||
| docsURL = fmt.Sprintf("https://docs.slack.dev/search/?q=%s", searchQuery) | ||
| sectionText = "Docs Search" | ||
| } else { | ||
| // Default docs 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.
|
||
|
|
||
| clients.Browser().OpenURL(docsURL) | ||
|
|
||
| if searchFlag != "" { | ||
| clients.IO.PrintTrace(ctx, slacktrace.DocsSearchSuccess, searchFlag) | ||
| } else { | ||
| clients.IO.PrintTrace(ctx, slacktrace.DocsSuccess) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| 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 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.
|
||
| 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", | ||
| }, | ||
| }, | ||
| "opens docs with basic search query": { | ||
| 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 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 empty search query as homepage": { | ||
| CmdArgs: []string{"--search", ""}, | ||
| 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", | ||
| }, | ||
| }, | ||
|
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. 🧪 issue: This case is a bit misleading to me! I tried using the search flag without an argument to find this error:
I do think this would be ideal - perhaps "https://docs.slack.dev/search/" complete - if possible, without an argument? |
||
| }, 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!