Documentation
¶
Overview ¶
Package cli implements the writ commands. cmd/writ parses flags into the option structs below and calls one App method; everything a command does, including rendering human and JSON output, happens here.
Index ¶
- Constants
- Variables
- func AcceptsPlan(pos []string) bool
- func ColorFor(w io.Writer) bool
- func ExitCode(err error) int
- func ImportsBulk(pos []string) bool
- func ImportsFile(out string) string
- func RunIsolationProbe(req string, out io.Writer) int
- func WriteDevClient(dir, url, token, user string) error
- func WriteFailure(w io.Writer, command string, args []string, err error) error
- func WriteVersion(w io.Writer, version string) error
- type AgentInitOptions
- type App
- func (a *App) AgentInit(ctx context.Context, o AgentInitOptions) (err error)
- func (a *App) Approve(ctx context.Context, o ApproveOptions) (err error)
- func (a *App) CIGitLab(ctx context.Context, o CIOptions) error
- func (a *App) ImportAcceptPlan(ctx context.Context, o ImportAcceptPlanOptions) (err error)
- func (a *App) ImportBulk(ctx context.Context, o ImportBulkOptions) (err error)
- func (a *App) Login(ctx context.Context, o LoginOptions) (err error)
- func (a *App) Logout(ctx context.Context, o LogoutOptions) (err error)
- func (a *App) MCP(ctx context.Context, o MCPOptions) error
- func (a *App) RemoteApply(ctx context.Context, o RemoteApplyOptions) (err error)
- func (a *App) RemoteAudit(ctx context.Context, o AuditOptions) (err error)
- func (a *App) RemotePlan(ctx context.Context, o RemotePlanOptions) (err error)
- func (a *App) RemotePlans(ctx context.Context, o PlansOptions) (err error)
- func (a *App) RemoteRuns(ctx context.Context, o RemoteOptions, limit int, asJSON bool) (err error)
- func (a *App) RepoAdd(ctx context.Context, o RepoAddOptions) (err error)
- func (a *App) RepoList(ctx context.Context, asJSON bool) (err error)
- func (a *App) RepoShow(ctx context.Context, name string, asJSON bool) (err error)
- func (a *App) Serve(ctx context.Context, o ServeOptions) error
- func (a *App) ServeDev(ctx context.Context, o DevServeOptions) error
- func (a *App) Show(ctx context.Context, o ShowOptions) (err error)
- func (a *App) StateAdopt(ctx context.Context, o StateAdoptOptions) (err error)
- func (a *App) UI(ctx context.Context, asJSON bool) (err error)
- func (a *App) Whoami(ctx context.Context, asJSON bool) (err error)
- type ApproveOptions
- type AuditOptions
- type CIOptions
- type DevServeOptions
- type ImportAcceptPlanOptions
- type ImportBulkOptions
- type LoginOptions
- type LogoutOptions
- type MCPOptions
- type Options
- type PlansOptions
- type RemoteApplyOptions
- type RemoteOptions
- type RemotePlanOptions
- type RepoAddOptions
- type ServeOptions
- type ShowOptions
- type StateAdoptOptions
- type UsageError
Constants ¶
const ( ExitOK = 0 ExitFailed = 1 ExitUsage = 2 )
Exit codes.
const ( DefaultBulkMaxObjects = importer.DefaultMaxObjects DefaultBulkMaxBytes = importer.DefaultMaxBytes )
Defaults of writ import bulk: the file it writes when --out is unset, and its limits.
const DefaultAuditLimit = 50
DefaultAuditLimit is how many rows `writ audit` lists without --limit.
const DefaultComponent = "$CI_SERVER_FQDN/platform/writ-ci/writ-plan@0.1.0"
DefaultComponent is the GitLab CI/CD component writ ci gitlab includes, on the instance that runs the pipeline.
const (
// DefaultMCPRuns is how many runs the runs tool lists by default.
DefaultMCPRuns = 20
)
Bounds of the MCP tools that writ mcp itself keeps.
const DefaultPlansLimit = 50
DefaultPlansLimit is how many plans `writ plans` lists without --limit.
const DefaultRunsLimit = 50
DefaultRunsLimit is how many runs `writ runs` lists without --limit.
const DefaultTofuBulkFile = "imported.tf"
DefaultTofuBulkFile is the file writ import bulk writes in an OpenTofu stack when --out is unset.
const DevClientFile = "client.json"
DevClientFile is the file in the dev data directory that holds the dev control plane's URL and token, readable only by its user.
const DevListen = "127.0.0.1:7340"
DevListen is the address writ serve --dev listens on by default.
const IsolationProbeEnv = server.IsolationProbeEnv
IsolationProbeEnv, set in writ's environment, makes writ run the isolation probe instead of a command.
const SessionsFile = "sessions.json"
SessionsFile holds the sessions of writ login, in the user's writ config directory.
Variables ¶
var ErrFailed = errors.New("command failed")
ErrFailed means the command ran and reported its failure on stdout (diagnostics, a refused or failed plan, a failed apply). Exit 1.
Functions ¶
func AcceptsPlan ¶
AcceptsPlan reports whether the positional arguments of writ import ask to accept the plan of an OpenTofu stack: accept and a plan, the plan file that tofu plan -out saved, a JSON document, or - for JSON on stdin.
func ColorFor ¶
ColorFor reports whether human output to w should be colored, by the process environment. See wantColor.
func ExitCode ¶
ExitCode maps a command's error to the process exit code: 0 for success, 2 for a usage error, and 1 for everything else, whether the command reported its own failure (ErrFailed) or could not run.
func ImportsBulk ¶
ImportsBulk reports whether the positional arguments of writ import ask for a bulk import: bulk alone, as with --type and --id, or bulk and a selection file, whose name ends in .json or is - for stdin. Any other writ import bulk ID imports one object at an address named bulk.
func ImportsFile ¶
ImportsFile is the file beside the code file out in which writ import bulk records each object it selected in an OpenTofu stack, for writ import accept. Its name does not end in .tf.json, so tofu does not read it as code.
func RunIsolationProbe ¶
RunIsolationProbe is what writ runs, as a provider's user, for writ serve --check: it tries to read the files the check names and to reach the instance metadata addresses, and prints what happened as JSON.
func WriteDevClient ¶
WriteDevClient writes the client.json of a dev control plane at url, with its token, into the data directory dir, as writ serve --dev does. A test's in-process server writes one so the CLI finds it.
func WriteFailure ¶
WriteFailure prints the document of a command that stopped on err before it printed one. command is the command's name and args the rest of its command line, which tells `import accept` from `import bulk`.
Types ¶
type AgentInitOptions ¶
type AgentInitOptions struct {
JSON bool
// Print writes nothing and prints what init would write.
Print bool
// Force rewrites skill files that differ from this writ's.
Force bool
}
AgentInitOptions are the flags of `writ agent init`.
type App ¶
type App struct {
// contains filtered or unexported fields
}
App runs commands for one stack directory.
func (*App) AgentInit ¶
func (a *App) AgentInit(ctx context.Context, o AgentInitOptions) (err error)
AgentInit puts the writ skill, in .agents/skills/writ and a copy in .claude/skills/writ, and a section of AGENTS.md that points to it into the repository at --dir. It writes regular files only, since writ serve refuses a commit whose tree holds a symbolic link, and replaces the link an older writ agent init made at .claude/skills/writ. It never writes through a symbolic link at a skill directory or below it: without --force such a link is refused, and with --force the link itself is removed and its target left as it is. It never changes AGENTS.md outside its own section, and never rewrites a skill file that differs without --force. Run twice, the second run changes nothing. Every file it reads or writes goes through an os.Root on --dir, so a symbolic link or .. that leads outside is refused, and so is a file that is not a regular file, such as a named pipe, which it never opens for reading.
func (*App) Approve ¶
func (a *App) Approve(ctx context.Context, o ApproveOptions) (err error)
Approve approves a plan writ serve keeps, every plan of the CI job's pipeline, or withdraws an approval.
func (*App) CIGitLab ¶
CIGitLab is `writ ci gitlab`: it prints the lines a repository's .gitlab-ci.yml needs, a plan job and an apply job for each stack and environment the control plane serves and one approve job, and the teams, grants, and approval rule the control plane's config needs for the project, by its numeric id.
func (*App) ImportAcceptPlan ¶
func (a *App) ImportAcceptPlan(ctx context.Context, o ImportAcceptPlanOptions) (err error)
ImportAcceptPlan accepts the plan of an OpenTofu stack after writ import bulk: it passes when the plan imports exactly the objects the import selected, each at its address by its import id, and changes nothing but the provider-only settings it prints.
func (*App) ImportBulk ¶
func (a *App) ImportBulk(ctx context.Context, o ImportBulkOptions) (err error)
ImportBulk reads many existing objects into an OpenTofu stack: it writes the resource blocks that declare them into one new file, and the objects it selected into another, for writ import accept.
func (*App) Login ¶
func (a *App) Login(ctx context.Context, o LoginOptions) (err error)
Login is `writ login`: it signs the person in through writ serve, with the device flow or in a browser, and keeps the session. With --json, the prompts go to stderr and stdout holds writ.login/v0, which never holds the token.
func (*App) Logout ¶
func (a *App) Logout(ctx context.Context, o LogoutOptions) (err error)
Logout is `writ logout`: it ends the session on writ serve and deletes it from SessionsFile. A session writ serve no longer knows is deleted too. When writ serve cannot be reached the session is still deleted here, and the command fails, since the session stays live on the server until it ends.
func (*App) MCP ¶
func (a *App) MCP(ctx context.Context, o MCPOptions) error
MCP serves the control plane's tools for OpenTofu stacks on stdin and stdout until the client closes stdin. The tools read and plan through the control plane a remote command finds, with its token, and return the documents it serves. The schema and import tools read a stack under --dir, through an os.Root on --dir, so no tool reads a file outside it. No tool applies or approves.
--dir is opened as given, so the kernel walks it, and every path after that is spelled from the directory it found.
func (*App) RemoteApply ¶
func (a *App) RemoteApply(ctx context.Context, o RemoteApplyOptions) (err error)
RemoteApply asks writ serve to apply a plan it keeps, and follows the run until it ends. The run belongs to writ serve: the first interrupt stops following it, and the run goes on.
func (*App) RemoteAudit ¶ added in v0.1.5
func (a *App) RemoteAudit(ctx context.Context, o AuditOptions) (err error)
RemoteAudit is `writ audit --remote`: the rows of writ serve's audit log that the token may see, newest first, as GET /v1/audit filters them.
func (*App) RemotePlan ¶
func (a *App) RemotePlan(ctx context.Context, o RemotePlanOptions) (err error)
RemotePlan sends a plan to writ serve, waits for it, and prints it. The plan names a commit, which the control plane fetches: --commit, or the commit of a clean work tree of a repository the control plane reads.
func (*App) RemotePlans ¶ added in v0.1.6
func (a *App) RemotePlans(ctx context.Context, o PlansOptions) (err error)
RemotePlans is `writ plans --remote`: the plans writ serve keeps in one environment that the token may read, newest first, with what started each, as GET /v1/plans filters them.
func (*App) RemoteRuns ¶
RemoteRuns is `writ runs --remote`: the runs writ serve started in --env, newest first, of the stack --stack names, or of every stack.
func (*App) RepoAdd ¶
func (a *App) RepoAdd(ctx context.Context, o RepoAddOptions) (err error)
RepoAdd is `writ repo add`: the control plane reads the default branch, finds the stacks, and keeps the repository.
func (*App) RepoShow ¶
RepoShow is `writ repo show NAME`: the repository and the stacks found at the head of its default branch.
func (*App) Serve ¶
func (a *App) Serve(ctx context.Context, o ServeOptions) error
Serve runs writ serve, the control plane, until ctx ends.
func (*App) ServeDev ¶
func (a *App) ServeDev(ctx context.Context, o DevServeOptions) error
ServeDev runs writ serve --dev until ctx ends: a control plane for this user alone, on loopback, with the same API and code as a team's.
func (*App) Show ¶
func (a *App) Show(ctx context.Context, o ShowOptions) (err error)
Show prints a plan writ serve keeps, by its id or its ref, or a run, or the outcome of an apply request by its key.
func (*App) StateAdopt ¶
func (a *App) StateAdopt(ctx context.Context, o StateAdoptOptions) (err error)
StateAdopt is `writ state adopt`: writ serve seals a secret set with the newest state of a stack@env, which no apply of it sealed one with.
type ApproveOptions ¶
type ApproveOptions struct {
JSON bool
// ID is the plan to approve, or with Withdraw the plan whose approval
// to take back.
ID string
// Pipeline approves every live plan of the CI job's own pipeline.
Pipeline bool
Withdraw bool
}
ApproveOptions are the flags of `writ approve --remote`.
type AuditOptions ¶ added in v0.1.5
type AuditOptions struct {
Stack, Plan, Run, Person, Op string
// Since and Until are RFC 3339 times.
Since, Until string
Cursor string
Limit int
JSON bool
}
AuditOptions are the filters of `writ audit --remote`. The environment is --env.
type CIOptions ¶
type CIOptions struct {
Repo string
// Envs narrows the environments; empty means every one the control
// plane serves for the repository's stacks.
Envs []string
// Component is the CI/CD component to include, with its version.
Component string
Image string
}
CIOptions are the flags of `writ ci gitlab`.
type DevServeOptions ¶
type DevServeOptions struct {
// Data is the data directory; empty means DevDir.
Data string
// Listen is a loopback address; empty means DevListen. Port 0 picks
// one, which client.json records.
Listen string
// Database is a postgres:// URL for the control database and every
// stack's state; empty means SQLite files under Data.
Database string
// Config is an HCL file of forge and repo blocks and poll_every, for repositories
// at a forge; empty for none.
Config string
Version string
}
DevServeOptions are the flags of `writ serve --dev`.
type ImportAcceptPlanOptions ¶
type ImportAcceptPlanOptions struct {
JSON bool
// Plan is the plan file that tofu plan -out saved for the stack after
// the import, or a file, "-" for stdin, that holds a JSON document of
// that plan: the masked plan document, writ plan --remote --json's
// output, or tofu show -json's. A JSON document is accepted only for
// an import that plans no update, since nothing proves which plan it
// shows.
Plan string
// Imports is the file in which writ import bulk recorded each object
// it selected, the address and the import id of each.
Imports string
}
ImportAcceptPlanOptions are the arguments of writ import accept PLAN.
type ImportBulkOptions ¶
type ImportBulkOptions struct {
JSON bool
// Selection is the selection file, "-" for stdin. Without it, Type
// and IDs select the objects.
Selection string
Type string
IDs []string
// Provider goes with Type: the provider configuration to read through.
Provider string
// Out is the file to write in the stack directory.
Out string
MaxObjects int
MaxBytes int
MaxStaleness *time.Duration
Params map[string]string
}
ImportBulkOptions are the arguments of `writ import bulk`.
type LoginOptions ¶
type LoginOptions struct {
JSON bool
// URL is the control plane; empty means the server precedence.
URL string
// Issuer names the people issuer when writ serve has several.
Issuer string
// Device and Browser force a flow. With neither, writ login uses the
// device flow when the issuer offers it, and the browser otherwise.
Device, Browser bool
}
LoginOptions are the flags of `writ login [URL]`.
type LogoutOptions ¶
LogoutOptions are the flags of `writ logout [URL]`.
type MCPOptions ¶
type MCPOptions struct {
// AllowProviders is --allow-providers: serve the import_bulk tool,
// which starts the stack's providers with writ mcp's environment,
// credentials, and file access, and let the schema tool download a
// provider that Writ's cache lacks.
AllowProviders bool
}
MCPOptions are the flags of writ mcp.
type Options ¶
type Options struct {
Dir string // stack directory, from --dir; default "."
// Files reads the stack's files and writes what writ import and writ
// agent init write. writ mcp confines them to the directory it serves.
// The zero value reaches any path.
Files confine.Files
// CacheDir holds provider binaries and schemas. Empty means
// WRIT_CACHE_DIR, else os.UserCacheDir()/writ.
CacheDir string
// Stdin is what writ mcp reads requests from.
Stdin io.Reader
Stdout io.Writer
// Stderr takes what a command says about its output when stdout is the
// output itself, as for the code writ import --type prints.
Stderr io.Writer
Log *slog.Logger // text handler on stderr
Now func() time.Time
LookupEnv func(string) (string, bool)
// Drain closes on the first interrupt, when writ apply --remote stops
// following its run.
Drain <-chan struct{}
// Env is the environment to run in, from --env; empty means WRIT_ENV,
// then none.
Env string
// Version is writ's own version.
Version string
// Server is the control plane the remote commands use, from --server:
// a URL, or "dev". Empty means WRIT_SERVER, then the dev control plane.
Server string
// Color colors human output; main sets it from ColorFor(os.Stdout).
Color bool
}
Options configures an App. main fills it once.
type PlansOptions ¶ added in v0.1.6
type PlansOptions struct {
Stack, Status string
// StartedBy is person or ci: what started the plans to list.
StartedBy string
Cursor string
Limit int
JSON bool
}
PlansOptions are the filters of `writ plans --remote`. The environment is --env.
type RemoteApplyOptions ¶
type RemoteApplyOptions struct {
JSON bool
// ID is the plan to apply. Empty with Merged applies the approved plan
// whose sources equal the merged commit's, and empty with Yes plans
// first.
ID string
Override string
// Merged applies after a merge: the plan of Stack in the environment
// whose sources equal Commit's, a commit of Repo's default branch. A
// CI job's token names its own repository and commit.
Merged bool
Stack string
Repo, Commit string
// Yes plans the stack, as writ plan --remote does, and applies the plan
// when it needs no approval.
Yes bool
Plan RemotePlanOptions
}
RemoteApplyOptions are the flags of `writ apply --remote`.
type RemoteOptions ¶
type RemoteOptions struct {
// Stack names the stack on the server.
Stack string
}
RemoteOptions are the flags every --remote command shares.
type RemotePlanOptions ¶
type RemotePlanOptions struct {
RemoteOptions
JSON bool
// HintMergeRequest is the merge request the plan belongs to, which the
// server keeps only for a merge request pipeline.
HintMergeRequest int
// Repo and Commit name the commit to plan, and no file is read: a CI
// job with no checkout sends them. A CI job's token names its own
// repository, so Repo may be empty there.
Repo, Commit string
}
RemotePlanOptions are the flags of `writ plan --remote`.
type RepoAddOptions ¶
type RepoAddOptions struct {
JSON bool
// Name names the repository; empty takes it from the directory.
Name string
// Location is a directory, for a local Git repository under writ serve
// --dev, or FORGE/PROJECT, where FORGE is a forge's name or host.
Location string
// Path is the directory in the project that holds the stacks. For a
// directory, empty means that directory.
Path string
Prefix string
}
RepoAddOptions are the arguments of `writ repo add`.
type ServeOptions ¶
type ServeOptions struct {
Config string
// Check reads the config, prints what it allows, and serves nothing.
Check bool
// NotifyTest, with Check, sends a test message to each notifier.
NotifyTest bool
// Version is writ's own version, which GET /v1/server reports.
Version string
}
ServeOptions are the flags of `writ serve`.
type ShowOptions ¶
ShowOptions are the flags of `writ show --remote ID`: a plan's id or ref, or a run's id. Request, in place of ID, is the key of an apply request.
type StateAdoptOptions ¶
type StateAdoptOptions struct {
JSON bool
Stack string
// Version names a state version on the http backend, and Serial a
// serial of a state on S3: exactly one is set.
Version, Serial int64
Reason string
}
StateAdoptOptions are the flags of `writ state adopt STACK`.
type UsageError ¶
type UsageError struct {
Msg string
}
UsageError means the command line was wrong: an unknown flag value, a malformed address, a missing argument. Exit 2.
func (*UsageError) Error ¶
func (e *UsageError) Error() string