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

Function codeRun

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

Source from the content-addressed store, hash-verified

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