MCPcopy Index your code
hub / github.com/cli/cli / NewCmdCode

Function NewCmdCode

pkg/cmd/search/code/code.go:29–108  ·  view source on GitHub ↗
(f *cmdutil.Factory, runF func(*CodeOptions) error)

Source from the content-addressed store, hash-verified

27}
28
29func 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 matching "deque" in Python files
60 $ gh search code deque --language=python
61
62 # Search code matching "cli" in repositories owned by microsoft organization
63 $ gh search code cli --owner=microsoft
64
65 # Search code matching "panic" in the GitHub CLI repository
66 $ gh search code panic --repo cli/cli
67
68 # Search code matching keyword "lint" in package.json files
69 $ gh search code lint --filename package.json
70 `),
71 RunE: func(c *cobra.Command, args []string) error {
72 if len(args) == 0 && c.Flags().NFlag() == 0 {
73 return cmdutil.FlagErrorf("specify search keywords or flags")
74 }
75 if opts.Query.Limit < 1 || opts.Query.Limit > shared.SearchMaxResults {
76 return cmdutil.FlagErrorf("`--limit` must be between 1 and 1000")
77 }
78 opts.Query.Keywords = args
79 if runF != nil {
80 return runF(opts)
81 }
82 var err error
83 opts.Searcher, err = shared.Searcher(f)
84 if err != nil {
85 return err
86 }

Callers 1

TestNewCmdCodeFunction · 0.85

Calls 5

FlagErrorfFunction · 0.92
SearcherFunction · 0.92
AddJSONFlagsFunction · 0.92
StringSliceEnumFlagFunction · 0.92
codeRunFunction · 0.85

Tested by 1

TestNewCmdCodeFunction · 0.68