| 111 | } |
| 112 | |
| 113 | func 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 | |
| 163 | func displayResults(io *iostreams.IOStreams, results search.CodeResult) error { |
| 164 | cs := io.ColorScheme() |