| 27 | } |
| 28 | |
| 29 | func NewCmdCode(f *cmdutil.Factory, runF func(*CodeOptions) error) *cobra.Command { |
| 30 | opts := &CodeOptions{ |
| 31 | Browser: f.Browser, |
| 32 | Config: f.Config, |
| 33 | IO: f.IOStreams, |
| 34 | Query: search.Query{Kind: search.KindCode}, |
| 35 | } |
| 36 | |
| 37 | cmd := &cobra.Command{ |
| 38 | Use: "code <query>", |
| 39 | Short: "Search within code", |
| 40 | Long: heredoc.Docf(` |
| 41 | Search within code in GitHub repositories. |
| 42 | |
| 43 | The search syntax is documented at: |
| 44 | <https://docs.github.com/search-github/searching-on-github/searching-code> |
| 45 | |
| 46 | Note that these search results are powered by what is now a legacy GitHub code search engine. |
| 47 | The results might not match what is seen on %[1]sgithub.com%[1]s, and new features like regex search |
| 48 | are not yet available via the GitHub API. |
| 49 | |
| 50 | For more information on handling search queries containing a hyphen, run %[1]sgh search --help%[1]s. |
| 51 | `, "`"), |
| 52 | Example: heredoc.Doc(` |
| 53 | # Search code matching "react" and "lifecycle" |
| 54 | $ gh search code react lifecycle |
| 55 | |
| 56 | # Search code matching "error handling" |
| 57 | $ gh search code "error handling" |
| 58 | |
| 59 | # Search code using raw search qualifiers as separate arguments |
| 60 | $ gh search code panic path:pkg language:go |
| 61 | |
| 62 | # Search code matching "deque" in Python files |
| 63 | $ gh search code deque --language=python |
| 64 | |
| 65 | # Search code matching "cli" in repositories owned by microsoft organization |
| 66 | $ gh search code cli --owner=microsoft |
| 67 | |
| 68 | # Search code matching "panic" in the GitHub CLI repository |
| 69 | $ gh search code panic --repo cli/cli |
| 70 | |
| 71 | # Search code matching keyword "lint" in package.json files |
| 72 | $ gh search code lint --filename package.json |
| 73 | `), |
| 74 | RunE: func(c *cobra.Command, args []string) error { |
| 75 | if len(args) == 0 && c.Flags().NFlag() == 0 { |
| 76 | return cmdutil.FlagErrorf("specify search keywords or flags") |
| 77 | } |
| 78 | if opts.Query.Limit < 1 || opts.Query.Limit > shared.SearchMaxResults { |
| 79 | return cmdutil.FlagErrorf("`--limit` must be between 1 and 1000") |
| 80 | } |
| 81 | opts.Query.Keywords = args |
| 82 | if runF != nil { |
| 83 | return runF(opts) |
| 84 | } |
| 85 | var err error |
| 86 | opts.Searcher, err = shared.Searcher(f) |