Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ powershell -c "irm https://github.com/ghraw/fulll/github-code-search/ma
github-code-search query "TODO" --org my-org
```

> [!TIP]
> `GITHUB_TOKEN` falls back to `gh auth token` when unset and the [GitHub CLI](https://cli.github.com/) is installed and authenticated.
> Set `GCS_DEFAULT_ORG=my-org` to omit `--org` on every call. See [Environment variables](https://fulll.github.io/github-code-search/reference/environment).

## Features

- **Org-wide search** — queries all repositories in a GitHub organization in one command, with automatic pagination up to 1 000 results
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/first-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This guide walks through a complete search session from first invocation to stru
Make sure you have:

- `github-code-search` [installed](/getting-started/installation)
- `GITHUB_TOKEN` set in your environment ([see Prerequisites](/getting-started/))
- `GITHUB_TOKEN` set in your environment, or the [GitHub CLI](https://cli.github.com/) installed and authenticated ([see Prerequisites](/getting-started/))

## Run a search

Expand Down
15 changes: 15 additions & 0 deletions docs/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ Add this to your shell profile (`~/.zshrc`, `~/.bashrc`, `~/.config/fish/config.
Never commit your token to version control. Use environment variables or a secrets manager.
:::

::: tip Already using the GitHub CLI?
If `GITHUB_TOKEN` isn't set and [`gh`](https://cli.github.com/) is installed and authenticated (`gh auth login`), `github-code-search` automatically retrieves a token via `gh auth token` — no extra setup needed.
:::

## Default organization

If you mostly search a single organization, set `GCS_DEFAULT_ORG` once to omit `--org` on every call:

```bash
export GCS_DEFAULT_ORG=my-org
github-code-search "useFeatureFlag"
```

An explicit `--org` flag always takes precedence over `GCS_DEFAULT_ORG`. See [Environment variables](/reference/environment) for details.

## Next step

→ [Install github-code-search](/getting-started/installation)
3 changes: 2 additions & 1 deletion docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ github-code-search completions [--shell <shell>]

| Option | Type | Required | Default | Description |
| ------------------------------------ | --------------------------------- | -------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--org <org>` | string | ✅ | — | GitHub organization to search in. Automatically injected as `org:<org>` in the query. |
| `--org <org>` | string | ❌¹ | — | GitHub organization to search in. Automatically injected as `org:<org>` in the query. Falls back to the `GCS_DEFAULT_ORG` environment variable when omitted. See [Environment variables](/reference/environment#gcs_default_org). |
| `--exclude-repositories <repos>` | string | ❌ | `""` | Comma-separated list of repositories to exclude. Short form (`repoA,repoB`) or full form (`org/repoA,org/repoB`) both accepted. |
| `--exclude-extracts <refs>` | string | ❌ | `""` | Comma-separated extract refs to exclude. Format: `repoName:path/to/file:index`. Short form (without org prefix) accepted. |
| `--no-interactive` | boolean (flag) | ❌ | `true` (on) | Disable interactive mode. Interactive mode is **on** by default; pass this flag to disable it. Also triggered by `CI=true`. |
Expand All @@ -57,6 +57,7 @@ github-code-search completions [--shell <shell>]

## Notes

- ¹ `--org` is required unless the `GCS_DEFAULT_ORG` environment variable is set. An explicit `--org` always takes precedence.
- `--no-interactive` and `CI=true` are equivalent — either one disables the TUI.
- The `--exclude-repositories` and `--exclude-extracts` options accept both the short form (without org prefix) and the full `org/repo` form.
- `--no-cache` has no effect unless `--group-by-team-prefix` is also set.
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| Variable | Required | Default | Description |
| ------------------------------ | -------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `GITHUB_TOKEN` | ✅ | — | GitHub personal access token. Used to authenticate API calls. See [Prerequisites](/getting-started/). |
| `GCS_DEFAULT_ORG` | ❌ | — | Default value for `--org` when the flag is omitted. An explicit `--org` always takes precedence. See [CLI options](/reference/cli-options). |
| `CI` | ❌ | `false` | Set to `true` to disable the interactive TUI and print results directly to stdout. Automatically set by GitHub Actions, GitLab CI, CircleCI and most CI platforms. |
| `GITHUB_CODE_SEARCH_CACHE_DIR` | ❌ | OS-dependent (below) | Override the directory used to cache the team list when `--group-by-team-prefix` is set. |

Expand All @@ -24,6 +25,17 @@ Add this to your shell profile (`~/.zshrc`, `~/.bashrc`, `~/.config/fish/config.
| `public_repo` | Searching public repositories only |
| `read:org` | Using [`--group-by-team-prefix`](/usage/team-grouping) |

If `GITHUB_TOKEN` isn't set and the [GitHub CLI](https://cli.github.com/) (`gh`) is installed and authenticated, `github-code-search` automatically falls back to the token returned by `gh auth token` — no extra configuration needed.

## `GCS_DEFAULT_ORG`

```bash
export GCS_DEFAULT_ORG=my-org
github-code-search "useFeatureFlag" # --org my-org is implied
```

Useful when you mostly (or only) search a single organization — set it once in your shell profile and omit `--org` on every call. Passing `--org` explicitly always overrides it.

## `CI`

```bash
Expand Down
23 changes: 20 additions & 3 deletions github-code-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* GITHUB_TOKEN env var must be set (for search; optional for upgrade).
*/

import { Command, program } from "commander";
import { Command, Option, program } from "commander";
import { writeFileSync } from "node:fs";
import { resolve } from "node:path";
import * as style from "./src/style.ts";
Expand Down Expand Up @@ -138,7 +138,15 @@ const helpFormatConfig = {
function addSearchOptions(cmd: Command): Command {
return cmd
.argument("<query>", "Search query")
.requiredOption("--org <org>", "GitHub organization to search in")
.addOption(
new Option(
"--org <org>",
[
"GitHub organization to search in.",
"Falls back to the GCS_DEFAULT_ORG environment variable when omitted.",
].join("\n"),
).env("GCS_DEFAULT_ORG"),
)
.option(
"--exclude-repositories <repos>",
[
Expand Down Expand Up @@ -256,7 +264,7 @@ function addSearchOptions(cmd: Command): Command {
async function searchAction(
query: string,
opts: {
org: string;
org?: string;
excludeRepositories: string;
excludeExtracts: string;
interactive: boolean;
Expand All @@ -279,6 +287,15 @@ async function searchAction(
process.exit(1);
}

if (!opts.org) {
console.error(
style.red(
"Error: --org is required (set the --org flag or the GCS_DEFAULT_ORG environment variable).",
),
);
process.exit(1);
}

// Fail fast on unbalanced quotes rather than surfacing a raw GitHub 422 — see issue #149
const quoteError = validateQuoteBalance(query);
if (quoteError) {
Expand Down
Loading