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

Code before:
	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()
	results, err := opts.Searcher.Code(opts.Query)
	io.StopProgressIndicator()
	if err != nil {
		return err
	}
	if len(results.Items) == 0 && opts.Exporter == nil {
		return cmdutil.NewNoResultsError("no code results matched your search")

Review comment (refactor):
```suggestion
			// FIXME: Remove this check once GHES supports the new `path` search qualifier.
```

Code after:
	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()
	results, err := opts.Searcher.Code(opts.Query)
	io.StopProgressIndicator()
	if err != nil {
		return err
	}
	if len(results.Items) == 0 && opts.Exporter == nil {
		return cmdutil.NewNoResultsError("no code results matched your search")