|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/cli/cli/api" |
| 9 | + "github.com/cli/cli/internal/ghrepo" |
| 10 | + "github.com/cli/cli/utils" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + RootCmd.AddCommand(searchCmd) |
| 16 | + searchCmd.Flags().IntP("limit", "L", 10, "limit the number of results") |
| 17 | +} |
| 18 | + |
| 19 | +var searchCmd = &cobra.Command{ |
| 20 | + Use: "search {repos|issues} <query>", |
| 21 | + Args: cobra.ExactArgs(2), |
| 22 | + Short: "Search GitHub", |
| 23 | + Long: `Search GitHub repositories or issues using terms in a query string. |
| 24 | +
|
| 25 | +The literal string "{baseRepo}" inside the search string will be substituted with |
| 26 | +the full name of the current repository. Similarly, "{owner}" will be replaced with |
| 27 | +the handle of the owner for the current repository. |
| 28 | +
|
| 29 | +Examples: |
| 30 | +
|
| 31 | + $ gh search repos 'in:readme "GitHub CLI"' |
| 32 | + $ gh search repos 'org:microsoft stars:>=15000' |
| 33 | +
|
| 34 | + $ gh search issues 'repo:cli/cli editor' |
| 35 | + $ gh search issues 'repo:{baseRepo} is:open team:{owner}/robots' |
| 36 | + $ gh search issues 'repo:{baseRepo} is:pr is:open review-requested:@me' |
| 37 | +
|
| 38 | +See <https://help.github.com/en/github/searching-for-information-on-github>`, |
| 39 | + RunE: search, |
| 40 | +} |
| 41 | + |
| 42 | +func search(cmd *cobra.Command, args []string) error { |
| 43 | + ctx := contextForCommand(cmd) |
| 44 | + |
| 45 | + baseRepo, err := ctx.BaseRepo() |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + client, err := apiClientForContext(ctx) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + searchType := args[0] |
| 56 | + searchTerm := strings.ReplaceAll(args[1], "{baseRepo}", ghrepo.FullName(baseRepo)) |
| 57 | + searchTerm = strings.ReplaceAll(searchTerm, "{owner}", baseRepo.RepoOwner()) |
| 58 | + |
| 59 | + limit, err := cmd.Flags().GetInt("limit") |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + switch searchType { |
| 65 | + case "repos": |
| 66 | + results, err := api.SearchRepos(client, searchTerm, limit) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + table := utils.NewTablePrinter(cmd.OutOrStdout()) |
| 72 | + for _, r := range results { |
| 73 | + table.AddField(r.NameWithOwner, nil, nil) |
| 74 | + table.AddField(fmt.Sprintf("%d", r.Stargazers.TotalCount), nil, utils.Yellow) |
| 75 | + table.AddField(replaceExcessiveWhitespace(r.Description), nil, nil) |
| 76 | + table.EndRow() |
| 77 | + } |
| 78 | + _ = table.Render() |
| 79 | + case "issues": |
| 80 | + results, err := api.SearchIssues(client, searchTerm, limit) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + table := utils.NewTablePrinter(cmd.OutOrStdout()) |
| 86 | + for _, i := range results { |
| 87 | + issueID := fmt.Sprintf("%s#%d", i.Repository.NameWithOwner, i.Number) |
| 88 | + createdAt := i.CreatedAt.Format(time.RFC3339) |
| 89 | + if table.IsTTY() { |
| 90 | + createdAt = utils.FuzzyAgo(time.Since(i.CreatedAt)) |
| 91 | + } |
| 92 | + |
| 93 | + table.AddField(issueID, nil, colorFuncForState(i.State)) |
| 94 | + table.AddField(replaceExcessiveWhitespace(i.Title), nil, nil) |
| 95 | + table.AddField(createdAt, nil, utils.Gray) |
| 96 | + table.EndRow() |
| 97 | + } |
| 98 | + _ = table.Render() |
| 99 | + default: |
| 100 | + return fmt.Errorf("unrecognized search type: %q", searchType) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
0 commit comments