-
Notifications
You must be signed in to change notification settings - Fork 31
feat: add slack docs search subcommand #433
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 5 commits
f248043
94c8a90
d7bbb84
727d7ce
e1b0d80
4985713
91b5ac5
086b9e5
2bf8133
47613d0
5650feb
671b4c8
0baf948
6c6875e
3317e11
d815e69
1bb0949
2fdee9b
8fd1999
962d75d
fb1c29d
7e615c8
086e69e
b1216f9
e26eab4
2bc9fa7
7a1eacd
f1ca5c3
e89d94d
5eff68e
8fc0ea4
28e103f
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,136 @@ | ||||||||||
| // 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" | ||||||||||
| "encoding/json" | ||||||||||
| "fmt" | ||||||||||
| "net/http" | ||||||||||
| "net/url" | ||||||||||
| "os" | ||||||||||
| "strings" | ||||||||||
|
|
||||||||||
| "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" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| const docsSearchAPIURL = "https://docs-slack-d-search-api-duu9zr.herokuapp.com/api/search" | ||||||||||
|
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. This will be updated to docs.slack.dev once api endpoint PR is merged in private docs repo
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: We might want to abstract this to the
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. 🔬 note: Thanks for also including these as now separate variables: Line 29 in 671b4c8
slack-cli/internal/api/docs.go Line 27 in 671b4c8
|
||||||||||
|
|
||||||||||
| type searchConfig struct { | ||||||||||
| output string | ||||||||||
| limit int | ||||||||||
| } | ||||||||||
|
|
||||||||||
| type DocsSearchResponse struct { | ||||||||||
| TotalResults int `json:"total_results"` | ||||||||||
| Results []DocsSearchResult `json:"results"` | ||||||||||
| Limit int `json:"limit"` | ||||||||||
| } | ||||||||||
|
|
||||||||||
| type DocsSearchResult struct { | ||||||||||
| URL string `json:"url"` | ||||||||||
| Title string `json:"title"` | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func NewSearchCommand(clients *shared.ClientFactory) *cobra.Command { | ||||||||||
| cfg := &searchConfig{} | ||||||||||
|
|
||||||||||
| cmd := &cobra.Command{ | ||||||||||
| Use: "search <query>", | ||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
Outdated
|
||||||||||
| Short: "Search Slack developer docs", | ||||||||||
| Long: "Search the Slack developer docs and return results in browser or JSON format", | ||||||||||
| Example: style.ExampleCommandsf([]style.ExampleCommand{ | ||||||||||
| { | ||||||||||
| Meaning: "Search docs and return JSON results", | ||||||||||
| Command: "docs search \"Block Kit\"", | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| Meaning: "Search docs and open results in browser", | ||||||||||
| Command: "docs search \"webhooks\" --output=browser", | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| Meaning: "Search docs with limited JSON results", | ||||||||||
| Command: "docs search \"api\" --output=json --limit=5", | ||||||||||
| }, | ||||||||||
| }), | ||||||||||
| Args: cobra.MinimumNArgs(1), | ||||||||||
|
zimeg marked this conversation as resolved.
|
||||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||
| return runDocsSearchCommand(clients, cmd, args, cfg, http.DefaultClient) | ||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
Outdated
|
||||||||||
| }, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| cmd.Flags().StringVar(&cfg.output, "output", "json", "output format: browser, json") | ||||||||||
| cmd.Flags().IntVar(&cfg.limit, "limit", 20, "maximum number of search results to return (only applies with --output=json)") | ||||||||||
|
|
||||||||||
| return cmd | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func runDocsSearchCommand(clients *shared.ClientFactory, cmd *cobra.Command, args []string, cfg *searchConfig, httpClient *http.Client) error { | ||||||||||
| ctx := cmd.Context() | ||||||||||
|
|
||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
|
||||||||||
| query := strings.Join(args, " ") | ||||||||||
|
|
||||||||||
| if cfg.output == "json" { | ||||||||||
| return fetchAndOutputSearchResults(ctx, clients, query, cfg.limit, httpClient) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
Outdated
|
||||||||||
| encodedQuery := url.QueryEscape(query) | ||||||||||
| docsURL := fmt.Sprintf("https://docs.slack.dev/search/?q=%s", encodedQuery) | ||||||||||
|
|
||||||||||
| clients.IO.PrintInfo(ctx, false, "\n%s", style.Sectionf(style.TextSection{ | ||||||||||
| Emoji: "books", | ||||||||||
| Text: "Docs Search", | ||||||||||
| Secondary: []string{ | ||||||||||
| docsURL, | ||||||||||
| }, | ||||||||||
| })) | ||||||||||
|
|
||||||||||
| clients.Browser().OpenURL(docsURL) | ||||||||||
| clients.IO.PrintTrace(ctx, slacktrace.DocsSearchSuccess, query) | ||||||||||
|
|
||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func fetchAndOutputSearchResults(ctx context.Context, clients *shared.ClientFactory, query string, limit int, httpClient *http.Client) error { | ||||||||||
|
Contributor
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'd be nice if URLs were full https://docs.slack.dev URLs so they're {
"url": "https://docs.slack.dev/block-kit/",
"title": "Block Kit"
},right now the json outputs as {
"url": "/block-kit/",
"title": "Block Kit"
},
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. 📚 thought: I agree listing entire links is ideal and might suggest we add a $ slack docs search "Block Kit" --output=text👾 ramble: If this format is supported it seems awkward to write that flag instead of defaulting to it but this is personal preference I think! |
||||||||||
| apiURL := fmt.Sprintf("%s?q=%s&limit=%d", docsSearchAPIURL, url.QueryEscape(query), limit) | ||||||||||
|
|
||||||||||
| resp, err := httpClient.Get(apiURL) | ||||||||||
| if err != nil { | ||||||||||
| return fmt.Errorf("failed to fetch search results: %w", err) | ||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
Outdated
|
||||||||||
| } | ||||||||||
| defer resp.Body.Close() | ||||||||||
|
|
||||||||||
| if resp.StatusCode != http.StatusOK { | ||||||||||
| return fmt.Errorf("API returned status %d", resp.StatusCode) | ||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
Outdated
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| var searchResponse DocsSearchResponse | ||||||||||
| if err := json.NewDecoder(resp.Body).Decode(&searchResponse); err != nil { | ||||||||||
| return fmt.Errorf("failed to parse search results: %w", err) | ||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
Outdated
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| encoder := json.NewEncoder(os.Stdout) | ||||||||||
| encoder.SetIndent("", " ") | ||||||||||
| if err := encoder.Encode(searchResponse); err != nil { | ||||||||||
| return fmt.Errorf("failed to output search results: %w", err) | ||||||||||
| } | ||||||||||
|
lukegalbraithrussell marked this conversation as resolved.
Outdated
|
||||||||||
|
|
||||||||||
| clients.IO.PrintTrace(ctx, slacktrace.DocsSearchSuccess, query) | ||||||||||
|
|
||||||||||
| 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.
🔭 suggestion: Let's prefer to hide deprecated features!
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 might now consider breaking this for the next update. I'm starting to think removing these fallbacks can be alright in this PR? @lukegalbraithrussell