MCPcopy
hub / github.com/cli/cli / codeRun

Function codeRun

pkg/cmd/search/code/code.go:110–158  ·  view source on GitHub ↗
(opts *CodeOptions)

Source from the content-addressed store, hash-verified

108}
109
110func codeRun(opts *CodeOptions) error {
111 io := opts.IO
112 if opts.WebMode {
113 // Convert `filename` and `extension` legacy search qualifiers to the new code search's `path`
114 // qualifier when used with `--web` because they are incompatible.
115 if opts.Query.Qualifiers.Filename != "" || opts.Query.Qualifiers.Extension != "" {
116 cfg, err := opts.Config()
117 if err != nil {
118 return err
119 }
120 host, _ := cfg.Authentication().DefaultHost()
121 // FIXME: Remove this check once GHES supports the new `path` search qualifier.
122 if !ghauth.IsEnterprise(host) {
123 filename := opts.Query.Qualifiers.Filename
124 extension := opts.Query.Qualifiers.Extension
125 if extension != "" && !strings.HasPrefix(extension, ".") {
126 extension = "." + extension
127 }
128 opts.Query.Qualifiers.Filename = ""
129 opts.Query.Qualifiers.Extension = ""
130 opts.Query.Qualifiers.Path = fmt.Sprintf("%s%s", filename, extension)
131 }
132 }
133 url := opts.Searcher.URL(opts.Query)
134 if io.IsStdoutTTY() {
135 fmt.Fprintf(io.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(url))
136 }
137 return opts.Browser.Browse(url)
138 }
139 io.StartProgressIndicator()
140 results, err := opts.Searcher.Code(opts.Query)
141 io.StopProgressIndicator()
142 if err != nil {
143 return err
144 }
145 if len(results.Items) == 0 && opts.Exporter == nil {
146 return cmdutil.NewNoResultsError("no code results matched your search")
147 }
148 if err := io.StartPager(); err == nil {
149 defer io.StopPager()
150 } else {
151 fmt.Fprintf(io.ErrOut, "failed to start pager: %v\n", err)
152 }
153 if opts.Exporter != nil {
154 return opts.Exporter.Write(io, results.Items)
155 }
156
157 return displayResults(io, results)
158}
159
160func displayResults(io *iostreams.IOStreams, results search.CodeResult) error {
161 cs := io.ColorScheme()

Callers 2

NewCmdCodeFunction · 0.85
TestCodeRunFunction · 0.85

Calls 15

DisplayURLFunction · 0.92
NewNoResultsErrorFunction · 0.92
IsStdoutTTYMethod · 0.80
StartPagerMethod · 0.80
StopPagerMethod · 0.80
displayResultsFunction · 0.70
ConfigMethod · 0.65
DefaultHostMethod · 0.65
AuthenticationMethod · 0.65
URLMethod · 0.65
BrowseMethod · 0.65

Tested by 1

TestCodeRunFunction · 0.68