> ## Documentation Index
> Fetch the complete documentation index at: https://latchkey.getgalaxy.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Latchkey CLI

> The latchkey command line client, its admin commands and the operator commands the UI does not offer.

Every admin UI action has a CLI equivalent, plus operator commands the UI
does not offer. The CLI is a thin client of the management API and
authenticates like any other caller.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
latchkey members list|add|deactivate|activate
latchkey groups list|create|rename|delete|members|add|remove
latchkey bindings list|add|remove <role> <member> [<kind> <id>]
latchkey roles list|show|create|update|delete
latchkey explain <subject> [<kind> <action> [<id>]]                     # why a subject can or cannot act
latchkey audit [--since <time>] [--actor <subject>] [--entity <entity>] [--instance] [--limit <n>] [--next <cursor>]
latchkey register -f latchkey.manifest.json [--dry-run]
latchkey tenants ensure <tenant> --admin <subject>                      # operators
latchkey audit prune --db <postgres-dsn|sqlite:path> --older-than <time> [--force] # operators
latchkey export --from sqlite:<path> --to <postgres-dsn>                # operators
```

## Installing

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
brew install --cask galaxy-io/tap/latchkey
```

Each [release](https://github.com/galaxy-io/latchkey/releases) also carries
the binary for Linux and macOS, on amd64 and arm64, with its checksums.
`latchkey version` prints the version, commit and build date.

## Signing in

The CLI sends a bearer token from your identity provider, the same token the
admin UI uses. `latchkey auth login` checks it against the server before it
keeps anything, then selects a context for that server.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
latchkey auth login --server https://latchkey.example.com --tenant acme --context staging   # reads the token from stdin, kept out of shell history
latchkey auth status
latchkey context list
latchkey --context staging explain user:alice
```

A context names a server and the tenant that tenant-scoped commands act on.
Operators belong to no tenant, so their contexts leave it out. Contexts live
in `$XDG_CONFIG_HOME/latchkey/contexts.yaml`, `~/.config/latchkey` without
it, and tokens live beside them in `credentials.yaml`, readable by you alone.
`LATCHKEY_TOKEN` is sent instead of the stored token when set, for CI and
scripts. `LATCHKEY_LAYOUT=plain` or `--layout plain` draws tables without
rules. Locally, `just token operator` prints an operator's token from the
issuer `just infra` starts.

## Administering a tenant

The admin commands act on the context's tenant and need its Admin. A member
is `user:<id>` or `service:<id>`, and a binding's member can also be
`group:<name>`. A binding covers the whole tenant, or one resource when it
names a kind and an id. Removing a binding names it the way adding did.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
latchkey members add user:alice --name "Alice Park"
latchkey groups create platform
latchkey groups add platform user:alice
latchkey roles create editor --allow filament.pipeline:read --allow filament.pipeline:update
latchkey bindings add editor group:platform filament.pipeline p1
latchkey explain user:alice filament.pipeline update p1     # Allowed, by editor for group:platform
latchkey bindings remove editor group:platform filament.pipeline p1
```

Every write prints the tenant's revision after it, which a change's audit
entry carries.
Deleting a group or a role and removing a binding ask first, and `--force`
skips the question. Lists take `--limit` and print the command for the next
page. `roles update` changes only what it is given, and `--allow` replaces
every permission the role has. It is refused if someone else changed the role
in the same moment, and running it again applies it to the role as it is now.

## Explain and audit

`latchkey explain` is the command line form of `ExplainAccess`. It lists the
bindings that reach the subject, directly or through a group, and flags a
subject id that matched nothing. With a kind and an action it also gives the
decision for that one check. `latchkey audit` reads the context's tenant's
log, newest first, and `--instance` reads the instance's log instead, for
operators.

## Registering kinds

`latchkey register -f` pushes a domain's kinds the way the product does when
it starts, and only the domain's owner may run it. The manifest is a
`RegisterDomainRequest` in protobuf JSON, which the SDK's `reg.Manifest()`
returns.

A push lists what the server says it changed. Each kind, and the domain
itself, has one row behind `+` for new, `~` for changed or `-` for retired.

With `--dry-run` the server checks the push as it would a real one and keeps
nothing, and the rows are what the push would change. The command fails when
the server would refuse the push, so a CI step catches that before the build
ships. It succeeds whether or not the push would change anything. A push
older than the newest version the server has seen is ignored, so its kinds
are not checked.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
LATCHKEY_TOKEN=$(just token filament) latchkey register -f latchkey.manifest.json --dry-run
```

## Operator commands

`latchkey tenants ensure` creates a tenant and binds its first Admin. For a
tenant that exists, `--admin` is bound only when the tenant has no Admin,
such as after the directory deactivated its last one, and only when an
operator runs it. A tenant that has an Admin is left alone.

`latchkey audit prune` works on the database directly, so it takes no
context and needs no login. `--older-than` is a duration back from now, such
as `9600h` for 400 days, or an RFC 3339 time. It asks first, and a scheduled
job passes `--force`.

On Postgres it connects as `latchkey_owner`, with the password in
`PGPASSWORD`. The log is kept by month in UTC, so it drops the months that
ended before the month the cutoff is in, and an entry can outlive the cutoff
by up to a month. A drop waits for anything reading the log, so the command
gives up after five seconds rather than hold audit writes behind it, and can
be run again. SQLite has no roles, so there it needs write access to
`latchkey.db` and deletes every entry older than the cutoff. Stop the product
first. One process owns the file, and a management write the product makes
at the same moment can fail and need trying again.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
PGPASSWORD=... latchkey audit prune --db postgres://latchkey_owner@db/latchkey --older-than 9600h --force
latchkey audit prune --db sqlite:data/latchkey.db --older-than 9600h
```

`latchkey export` moves an embedded install into a service, as described in
[Deployment](https://latchkey.getgalaxy.io/pages/guides/deployment/overview.md#service-or-embedded).

Migrations are not a CLI command. The server applies them with `-migrate`,
as described in [Deployment](https://latchkey.getgalaxy.io/pages/guides/deployment/overview.md#database-boundary).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.