# CLI

The pilvia command — app commands, flags, configuration.

The `pilvia` CLI is one native binary; Pilvia App lives under
`pilvia app`. Install it [through npm](/start/installation.md) as
`@pilvia/cli`.

## Commands

```sh
pilvia app init [DIR]
```

Scaffold a new app in `DIR` (default: the current directory).

```sh
pilvia app login [--no-browser]
pilvia app login --token <token>
```

Sign in with a browser and store the [named token](/hosting/tokens.md) the
deploy plane mints for you. With `--token`, store a token you already hold
instead. The token is checked before it is written to
`~/.config/pilvia/config.json` (mode `0600`). A new login revokes the named
token it replaces on the same plane. Browser sign-in needs 0.2.6 or later
and a deploy plane that takes browser logins; one that does not says so.

```sh
pilvia upgrade [--check] [--force]
```

Replace the binary with the newest release from `dl.pilvia.com`. It runs
only after the release's ed25519 signature checks out against the key the
installer pins and the download matches its signed checksum. `--check`
only reports. A copy installed through npm or built with cargo is left to
that tool, with the command to run (0.2.6 and later).

```sh
pilvia app deploy [DIR] [--name <app>] [--bundle <file>] [--branch [<name>]]
```

Deploy the app in `DIR` (default `.`), reading `pilvia.app.json` beside
it. Builds the server half if the app has one (the platform never builds;
[the CLI does, locally](/hosting/deploys.md)), then pushes differentially:
plan, upload only missing blobs, commit by hash. `--name` overrides the
app name; `--bundle` pushes a prebuilt server bundle as-is.

`--branch <name>` deploys a preview: the release gets its build URL and
`<name>--<app>.pilvia.app` moves to it, and production is not touched.
`--branch` alone uses the git branch the directory is on. With no name
anywhere — no `name` in the config, no `--name` — the platform mints one,
creates the app and writes the name into `pilvia.app.json`. Every deploy
prints the production URL (unless it was a branch deploy), the build URL
and, for a branch deploy, the branch URL. See
[Preview URLs](/hosting/previews.md).

```sh
pilvia app list
```

List deployed apps with their active release, its build name, and the git
commit each was built from.

```sh
pilvia app rollback <name> [--to <build|version>]
```

Return an app to its previous release — or, with `--to`, to any retained
release by its build name (the four words a deploy printed) or version.
This is also how a branch preview is promoted to production.

```sh
pilvia app secret <name> KEY=VALUE...
```

Replace the app's secrets — a **full replace**, so state every secret on
each call. These are the app's platform credentials, such as the
`TRIGGER_TOKEN` of an `auth: token` app. Your server code cannot read
them: see
[Configuration and secrets](/hosting/server-functions.md#configuration-and-secrets).

No redeploy is needed for a change to take hold. Every replica serving the
app picks the new values up within the store poll interval (10 seconds by
default) and applies them on the next instantiation, so requests already in
flight finish on the old values and everything after them runs on the new
ones.

```sh
pilvia app sql exec <name> "<SQL>" | --file <file> [--json]
pilvia app sql migrations list <name> [--dir migrations] [--pattern '*.sql']
pilvia app sql migrations apply <name> [--dir migrations] [--pattern '*.sql']
pilvia app sql export <name> [--output <file>] [--force]
```

The app's [SQL database](/hosting/sql.md) (`pilvia` 0.2.11 and later): run SQL as its operator, apply `.sql` migrations in order, each
once and each in one transaction, and download the database as one SQLite
file (default `<name>.sqlite`, never over an existing file without
`--force`). `migrations apply` stops at the first file that fails, and that
file leaves nothing behind. These need a token that may manage the app.

## Flags and environment

Every command accepts `--server` and `--token`, overriding the stored
config. The server resolves flag, then `PILVIA_APP_SERVER`, then the config,
then `https://deploy.pilvia.dev`. In CI, set `PILVIA_DEPLOY_TOKEN` and skip
`login` entirely.

```sh
pilvia app whoami
```

Show which token the CLI is using, what it reaches and when it expires.

```sh
pilvia app token create <name> [--app <app>... | --all-apps] [--create] [--target <target>] [--days <n>] [--fixed]
pilvia app token list
pilvia app token revoke <id|self>
```

Mint, list and revoke named tokens, one per person or pipeline. Minting and
listing take the operator token; any token can revoke itself. `token create`
prints the token alone on stdout. See [Tokens](/hosting/tokens.md) (0.2.5 and
later).

## Exit behavior worth knowing

- A deploy refused because a colleague's deploy landed in between is a
  clear error, not an overwrite — rerun to deploy over the current state.
- Deploying from a dirty git tree is allowed and **recorded** — the
  release remembers it was built from uncommitted work.
