Repository: cli/cli
File: pkg/cmd/search/code/code.go (Go)

Code before:
	}

	// Output flags
	cmdutil.AddJSONFlags(cmd, &opts.Exporter, search.CodeFields)
	cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the search query in the web browser")

	// Query parameter flags
	cmd.Flags().IntVarP(&opts.Query.Limit, "limit", "L", 30, "Maximum number of code results to fetch")

	// Query qualifier flags
	cmd.Flags().StringVar(&opts.Query.Qualifiers.Extension, "extension", "", "Filter on file extension")
	cmd.Flags().StringVar(&opts.Query.Qualifiers.Filename, "filename", "", "Filter on filename")
	cmdutil.StringSliceEnumFlag(cmd, &opts.Query.Qualifiers.In, "match", "", nil, []string{"file", "path"}, "Restrict search to file contents or file path")
	cmd.Flags().StringVarP(&opts.Query.Qualifiers.Language, "language", "", "", "Filter results by language")
	cmd.Flags().StringSliceVarP(&opts.Query.Qualifiers.Repo, "repo", "R", nil, "Filter on repository")
	cmd.Flags().StringVar(&opts.Query.Qualifiers.Size, "size", "", "Filter on size range, in kilobytes")
	cmd.Flags().StringSliceVar(&opts.Query.Qualifiers.User, "owner", nil, "Filter on owner")

	return cmd
}

func codeRun(opts *CodeOptions) error {
	io := opts.IO
	if opts.WebMode {
		// Convert legacy `filename` and `extension` elasticsearch qualifiers to Blackbird's `path`
		// qualifier when opening web search.
		if opts.Query.Qualifiers.Filename != "" || opts.Query.Qualifiers.Extension != "" {
			cfg, err := opts.Config()
			if err != nil {
				return err
			}
			host, _ := cfg.Authentication().DefaultHost()
			// FIXME: Remove this check once GHES supports Blackbird.
			if !ghauth.IsEnterprise(host) {
				filename := opts.Query.Qualifiers.Filename
				extension := opts.Query.Qualifiers.Extension
				if extension != "" && !strings.HasPrefix(extension, ".") {
					extension = "." + extension
				}
				opts.Query.Qualifiers.Filename = ""
				opts.Query.Qualifiers.Extension = ""
				opts.Query.Qualifiers.Path = fmt.Sprintf("%s%s", filename, extension)
			}
		}
		url := opts.Searcher.URL(opts.Query)
		if io.IsStdoutTTY() {
			fmt.Fprintf(io.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(url))
		}
		return opts.Browser.Browse(url)
	}
	io.StartProgressIndicator()

Review comment (suggestion):
Pairing reviewing this with @andyfeller to get this across the line. Wanted to adjust this comment to be more open-source friendly by leaning away from codenames.

```suggestion
		// Convert `filename` and `extension` legacy search qualifiers to the new code search's `path`
		// qualifier when used with `--web` because they are incompatible.
```

Code after:
	}

	// Output flags
	cmdutil.AddJSONFlags(cmd, &opts.Exporter, search.CodeFields)
	cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the search query in the web browser")

	// Query parameter flags
	cmd.Flags().IntVarP(&opts.Query.Limit, "limit", "L", 30, "Maximum number of code results to fetch")

	// Query qualifier flags
	cmd.Flags().StringVar(&opts.Query.Qualifiers.Extension, "extension", "", "Filter on file extension")
	cmd.Flags().StringVar(&opts.Query.Qualifiers.Filename, "filename", "", "Filter on filename")
	cmdutil.StringSliceEnumFlag(cmd, &opts.Query.Qualifiers.In, "match", "", nil, []string{"file", "path"}, "Restrict search to file contents or file path")
	cmd.Flags().StringVarP(&opts.Query.Qualifiers.Language, "language", "", "", "Filter results by language")
	cmd.Flags().StringSliceVarP(&opts.Query.Qualifiers.Repo, "repo", "R", nil, "Filter on repository")
	cmd.Flags().StringVar(&opts.Query.Qualifiers.Size, "size", "", "Filter on size range, in kilobytes")
	cmd.Flags().StringSliceVar(&opts.Query.Qualifiers.User, "owner", nil, "Filter on owner")

	return cmd
}

func codeRun(opts *CodeOptions) error {
	io := opts.IO
	if opts.WebMode {
		// Convert `filename` and `extension` legacy search qualifiers to the new code search's `path`
		// qualifier when used with `--web` because they are incompatible.
		if opts.Query.Qualifiers.Filename != "" || opts.Query.Qualifiers.Extension != "" {
			cfg, err := opts.Config()
			if err != nil {
				return err
			}
			host, _ := cfg.Authentication().DefaultHost()
			// FIXME: Remove this check once GHES supports the new `path` search qualifier.
			if !ghauth.IsEnterprise(host) {
				filename := opts.Query.Qualifiers.Filename
				extension := opts.Query.Qualifiers.Extension
				if extension != "" && !strings.HasPrefix(extension, ".") {
					extension = "." + extension
				}
				opts.Query.Qualifiers.Filename = ""
				opts.Query.Qualifiers.Extension = ""
				opts.Query.Qualifiers.Path = fmt.Sprintf("%s%s", filename, extension)
			}
		}
		url := opts.Searcher.URL(opts.Query)
		if io.IsStdoutTTY() {
			fmt.Fprintf(io.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(url))
		}
		return opts.Browser.Browse(url)
	}
	io.StartProgressIndicator()