# CLI

`logmastctl` manages enrollment, configuration, verification, and authorized reads. It does not tail logs or install a collector. The HTTP API remains sufficient for application collection.

## Build and enroll

Build the public Rust workspace with `cargo build --release -p logmast-cli`. Use `target/release/logmastctl` on Linux or macOS. Current sources are linked from the [SDK guide](/docs/sdk/).

```sh
logmastctl catalog
logmastctl schema
logmastctl enroll --workspace 'My team' --project Production --service api
logmastctl verify
logmastctl workspaces
logmastctl status WORKSPACE_UUID
logmastctl claim WORKSPACE_UUID --email owner@example.com
```

Enrollment persists its identity and secrets before sending. Resume with the same arguments and state after an uncertain outcome. The owner must follow the email proof; supplying an address does not establish ownership.

## Sign in

```sh
logmastctl login                                   # read access, anywhere on this machine
logmastctl login --permission manage --days 1      # a short management grant
logmastctl login --workspace WORKSPACE_UUID --project PROJECT_UUID --scope .
logmastctl whoami                                  # which credential a command here uses
logmastctl logout                                  # revoke it and forget it
```

`login` asks a person to approve this machine. Without a terminal it must name `--workspace`: otherwise whoever approves the code chooses the workspace, which only a person at a terminal, shown the workspace's name and ID, may accept. At a terminal it opens the browser at `/device` and waits; captured, as an agent runs it, it prints the link and an eight-letter code and exits, and `login --wait` finishes once someone approves. The approver must be an owner of the workspace, sees the computer name, directory and network the CLI reported, may narrow the request but never widen it, and needs their authenticator code to approve management. Codes expire after ten minutes.

The CLI creates the token itself and sends Logmast only its hash, so it never appears in a response, a browser or a conversation. It is kept in `$XDG_STATE_HOME/logmast/logins/` (mode `0600`) and never printed. Each login is its own agent identity, listed and revocable in **Access → Agents**; the audit log attributes its actions to it, whoever typed the command.

A login is machine-wide unless `--scope DIR` limits it to a directory tree. A command uses the login whose scope is nearest and whose workspace (and project) matches the one it names or the directory's binding. A second login for the same workspace, project and scope replaces the first once approved, revoking it. One login waits for approval at a time: another `login` is refused until the waiting one is approved, denied, cancelled (`login --cancel`) or expired, so a person and an agent sharing the machine never finish each other's logins. Scope selects a login; it is not a security boundary: anyone who can run commands as your user can use it.

Credentials resolve in this order: `LOGMAST_AGENT_TOKEN`, `--credential` (or `LOGMAST_CREDENTIAL`), the applicable login, then the enrollment state. In CI, use a credential from your secret manager rather than `login`.

## A person and an agent on one machine

On a terminal, `logmastctl` prints short text for a person. When its output is captured or piped, as an agent or a script runs it, it prints stable JSON; `--format human|json` overrides the choice, and scripts should pin `--format json` because a pseudo-terminal (for example `docker run -t`) counts as a terminal. Every failure has a stable `code` and names its `next` step. Work only a person may do (inviting, deleting, billing, granting access) is handed off as a link to the console page that does it.

Bind a repository once, and its commands no longer need IDs:

```sh
logmastctl setup --project PROJECT_UUID      # in the repository; the workspace defaults to the only one you reach
logmastctl projects                          # projects and services of the bound workspace
logmastctl services create --name api --env-file .env.logmast
logmastctl verify --service SERVICE_UUID
```

`setup` records the binding in the CLI's own configuration (`$XDG_CONFIG_HOME/logmast/bindings.json`), not in the repository. The nearest bound directory wins. A binding holds identifiers, never secrets. Without a binding, a command that acts on a workspace uses the credential's own, so after one `logmastctl login` even `logmastctl projects` needs no ID; a binding still chooses the project.

## Service credentials

`services create` and `credentials rotate` keep the new ingest credential privately beside the CLI state (`services/SERVICE_UUID.json`, mode `0600`) and never print it. `--env-file PATH` also writes `LOGMAST_*` settings, and `--dsn-file PATH` a `SENTRY_DSN` line, to a new file with mode `0600`; the directory must not be writable by everyone, and an existing file is never overwritten. Files are checked before anything changes on the server, and if writing them still fails afterwards the service is reported with the command that exports its credential again. Keep those files out of version control and load them into the application's secret manager.

```sh
logmastctl credentials export --service SERVICE_UUID --env-file /private/logmast/api.env
logmastctl credentials export --service SERVICE_UUID --reveal   # only to a terminal a person reads
logmastctl credentials rotate --service SERVICE_UUID            # earlier credentials stop at once
```

`credentials export` reads only this machine's files and sends nothing; it also covers the service an enrollment created. Credentials are shown only once by Logmast; a machine that never held one gets a new one by rotation.

## Private state and origin

Global `--url`, `--state`, `--credential` and `--format` options can appear before or after the subcommand. The default origin is `https://app.logmast.com`. State defaults to `$XDG_STATE_HOME/logmast/session.json`, or `$HOME/.local/state/logmast/session.json`.

Use directory mode `0700` and file mode `0600`. The client rejects unsafe files, symlinks, origin changes, redirects, and non-HTTPS origins except loopback development. Use a separate state file for each installation and enrollment. Never print or upload its secrets.

## Verification runs

`logmastctl verify` verifies the enrollment's service; `verify --service SERVICE_UUID` verifies any service whose credential this machine holds, reading the evidence back with the management credential. Either resumes an incomplete run or creates a fresh one after completion. `--run-id UUID` addresses one persisted run. Re-reading a completed run returns historical proof explicitly. See [verification stages](/docs/verification/).

## Delegated management for CI

Where nobody can approve a code, such as in CI, an owner can issue a read-only or member-level token from **Access → Agents** in the console. Duration defaults to 180 days and can be set from 1 to 360 days. Choose **Copy token**, save it in your secret manager, and inject it into the CLI process as `LOGMAST_AGENT_TOKEN`. Set `LOGMAST_URL` or `--url` for your installation. Keep the token out of command arguments, shell history and logs. `LOGMAST_TOKEN` is the separate application-ingestion setting.

```sh
# LOGMAST_AGENT_TOKEN is supplied by your secret manager.
logmastctl status WORKSPACE_UUID
logmastctl control WORKSPACE_UUID \
  --command-file /private/logmast/check-command.json \
  --output /private/logmast/check-result.json
```

**Download JSON** remains available as an alternative. Store the file privately and use `--credential` or `LOGMAST_CREDENTIAL`; unset `LOGMAST_AGENT_TOKEN` first. Mixing token and file credentials is rejected. Downloaded files retain origin verification; a plain token uses the configured server URL. If neither is supplied, the CLI uses its private enrollment state.

```sh
logmastctl --credential /private/logmast/agent.json status WORKSPACE_UUID
logmastctl --credential /private/logmast/agent.json control WORKSPACE_UUID \
  --command-file /private/logmast/check-command.json \
  --output /private/logmast/check-result.json
```

Control outputs may contain ingest secrets and are written to a new private file. The CLI makes one mutation request. After uncertainty, read current state and reconcile before repeating creation or rotation. Permission to inspect a workspace does not authorize purchases. With agent access you can verify the services you create or rotate on this machine: `verify --service SERVICE_UUID`.

For bounded procedures and handoff rules, start at [Agent documentation](/docs/agents/).
