# Scripting with the CLI

> Use the Corfedo CLI in scripts and CI with JSON output, request files, paging limits, exit codes and API keys.

Source: https://docs.corfedo.ai/connect/cli/scripting/

Scripts and pipelines sign in with an API key file, read JSON output and act on exit codes.

## Run in CI with an API key

1. [Create an API key](https://docs.corfedo.ai/connect/cli/sign-in-and-profiles/#use-an-api-key-file) with only the repositories the pipeline needs.
2. Store the key file as a CI secret and write it to a file only the job's user can read.
3. Configure a profile and run commands with it:

```sh
corfedo auth configure-api-key --key-file "$RUNNER_TEMP/corfedo.key.json" --profile ci
corfedo search --profile ci --query '"TODO(security)"' --format json
```

Only `auth login` opens a browser. In unattended runs, `--non-interactive` makes it fail at once with `interaction_required`.

## Output formats

| Option | Output |
| --- | --- |
| `--format text` | The default. Readable output for a terminal |
| `--format json` | One JSON envelope per command |
| `--format jsonl` | One JSON record per page, then a summary record. Required with `--all` |

The JSON envelope:

```json
{
  "schemaVersion": "1",
  "command": "search",
  "outcome": "complete",
  "data": { },
  "pagination": { "continuation": null, "exhausted": true, "stoppedByClientLimit": false },
  "error": null,
  "warnings": []
}
```

`outcome` is, for example, `complete`, `partial`, `not_found`, `stale`, `unavailable` or `error`. On failure, `error.code` names the reason. Usage messages and text errors go to stderr.

## Read every page

`--all` reads every page of a listing command. It needs `--format jsonl`:

```sh
corfedo search --query '"RetryPolicy"' --all --format jsonl --max-pages 20 --max-output-bytes 8388608
```

`--max-pages` (default 20) and `--max-output-bytes` (default 8 MiB) stop the stream early with the outcome `partial` and exit 7. The final summary record holds the last continuation, so a request file can continue from there. Without a summary record the output is incomplete.

## Requests from a file

Read commands also accept the complete API request as JSON with `--request FILE`, or `--request -` for standard input. It replaces the command's arguments and can carry a `continuation` from an earlier page.

```sh
echo '{"prefix": "acme/", "target": "repositories"}' | corfedo scope lookup --request -
echo '{"field": "symbol", "prefix": "Retry"}' | corfedo completions --request -
```

`corfedo schema --command NAME` lists a command's request fields.

## Exit codes

Act on the exit code: 0 is a complete result, 7 a partial one, and other codes name the kind of failure. `--allow-partial` turns 7 into 0. [CLI commands](https://docs.corfedo.ai/reference/cli/#exit-codes) lists every code, and [Ask from the CLI](https://docs.corfedo.ai/connect/cli/ask/#exit-codes) the codes of `ask`.

## Related

- [CLI commands](https://docs.corfedo.ai/reference/cli/) lists every command and option.
- [API reference](https://docs.corfedo.ai/reference/api/) describes every request field.
