OXYGENHUB
# f53cbe7a204e oxygen / src / cli / mod.rs
48.8 KiB · Rust · last changed Policies grant subjects an issuer certifies (keyless step 4a) 18 days ago source blame history raw
1
//! Command-line definitions and dispatch.
2
3
mod approve;
4
mod auth;
5
mod format;
6
mod identity;
7
mod json;
8
mod proposal;
9
mod receipt;
10
mod sync;
11
pub mod ui;
12
mod workspace;
13
14
use anyhow::{Context, Result};
15
use clap::{Parser, Subcommand};
16
use oxy_core::repo::Repo;
17
use std::path::PathBuf;
18
19
#[derive(Parser)]
20
#[command(
21
    name = "oxy",
22
    version = env!("OXY_VERSION_LONG"),
23
    about = "Oxygen: breathing fresh air into version control"
24
)]
25
struct Cli {
26
    /// Plain output: no colour, no spinner (also implied by NO_COLOR or a non-terminal)
27
    #[arg(long, global = true)]
28
    plain: bool,
29
    /// Read the repository as it was after operation OP (an ID from oxy op log, @ or @-); commands that would change it are refused
30
    #[arg(long, global = true, value_name = "OP")]
31
    at_op: Option<String>,
32
    #[command(subcommand)]
33
    cmd: Cmd,
34
}
35
36
/// `--at-op`, for `open` to apply.
37
static AT_OP: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
38
39
#[derive(Subcommand)]
40
enum Cmd {
41
    /// Create a repository in PATH (default: current directory)
42
    Init {
43
        /// Directory to create the repository in (default: the current directory)
44
        path: Option<PathBuf>,
45
        /// No working copy: an object store and references only, for hosting
46
        #[arg(long)]
47
        bare: bool,
48
    },
49
    /// Clone a repository from a local path or an http(s) URL
50
    Clone {
51
        /// Local path or http(s) URL of the repository
52
        url: String,
53
        /// Directory to clone into (default: the last part of the URL)
54
        dest: Option<PathBuf>,
55
        /// Leave blobs larger than this (e.g. 1M) to be fetched on first read
56
        #[arg(long)]
57
        max_blob: Option<String>,
58
        /// Materialize only these paths (repeatable); implies lazy blobs
59
        #[arg(long = "sparse")]
60
        sparse: Vec<String>,
61
        /// Shallow clone: fetch only this many levels of history below the heads
62
        #[arg(long)]
63
        depth: Option<u32>,
64
        /// Clone the subrepos too (as oxy submodule update --init)
65
        #[arg(long)]
66
        recursive: bool,
67
    },
68
    /// Fetch more history below a shallow clone's boundary
69
    Deepen {
70
        /// Levels of history to add below the boundary
71
        #[arg(default_value_t = 10)]
72
        depth: u32,
73
        /// Remote to fetch the history from
74
        #[arg(long, default_value = "origin")]
75
        remote: String,
76
    },
77
    /// Show or set the sparse filter (paths to materialize); re-checks out the bookmark
78
    Sparse {
79
        /// Paths to materialize; none prints the current filter
80
        paths: Vec<String>,
81
    },
82
    /// Show the working copy: changed paths and conflicts
83
    Status {
84
        /// Machine-readable output: paths, and every conflict with its kind and contributing changes
85
        #[arg(long)]
86
        json: bool,
87
    },
88
    /// One-line summary for shell prompts; fast and records nothing
89
    Prompt {
90
        /// Placeholders: %b bookmark, %c change id, %h heads, %d dirty (*), %x conflict (!), %A/%D ahead/behind (+n/-n), %a/%B raw counts, %s state glyph, %U/%O ahead/behind arrows, %i icon
91
        #[arg(long, default_value = oxy_core::repo::DEFAULT_PROMPT_FORMAT)]
92
        format: String,
93
        /// Glyph format (%i %b %c %s%U%O) instead of the plain default
94
        #[arg(long)]
95
        glyphs: bool,
96
        /// Colour by state (cyan clean, yellow dirty, red conflicted): raw escapes, or guarded for a bash or zsh PS1
97
        #[arg(long, value_parser = ["raw", "bash", "zsh"])]
98
        color: Option<String>,
99
    },
100
    /// Record the working copy as a change on the current bookmark
101
    Commit {
102
        /// The change's message: a subject line, then optionally a blank line and a body
103
        #[arg(short, long, required_unless_present = "file", conflicts_with = "file")]
104
        message: Option<String>,
105
        /// Read the message from FILE (`-` for standard input)
106
        #[arg(short = 'F', long, value_name = "FILE")]
107
        file: Option<PathBuf>,
108
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
109
        #[arg(long = "depends-on")]
110
        depends_on: Vec<String>,
111
        /// Commit only these paths; the rest stays as uncommitted work
112
        paths: Vec<String>,
113
        /// Choose hunks one by one
114
        #[arg(short, long)]
115
        interactive: bool,
116
        /// Commit these hunks (as `oxy hunks` numbers them), comma separated
117
        #[arg(long, value_delimiter = ',')]
118
        hunks: Vec<usize>,
119
    },
120
    /// Fold CHANGE into one of its dependencies (keeps that change's id)
121
    Squash {
122
        /// The change to fold (ChangeId prefix, #hash prefix)
123
        change: String,
124
        /// The dependency that absorbs it
125
        #[arg(long)]
126
        into: String,
127
    },
128
    /// Re-express CHANGE against another base state
129
    Rebase {
130
        /// The change to re-express (ChangeId prefix, #hash prefix)
131
        change: String,
132
        /// The changes, bookmark or tag whose state becomes the new base
133
        #[arg(long, required = true, num_args = 1..)]
134
        onto: Vec<String>,
135
        /// Where the rebase would be conflicted, merge the target into the
136
        /// current bookmark instead, leaving the conflict in the working
137
        /// copy to fix and commit
138
        #[arg(long)]
139
        into_working_copy: bool,
140
    },
141
    /// Show which change introduced each line of a file
142
    Blame {
143
        /// File to annotate, relative to the repository root
144
        path: String,
145
        /// Machine-readable output: one JSON document on stdout
146
        #[arg(long)]
147
        json: bool,
148
    },
149
    /// Find the change that introduced a regression
150
    Bisect {
151
        #[command(subcommand)]
152
        cmd: BisectCmd,
153
    },
154
    /// List changes reachable from the bookmark and the working copy
155
    Log {
156
        /// Show at most this many changes
157
        #[arg(short = 'n', long)]
158
        limit: Option<usize>,
159
        /// Show full messages, dependencies, and signatures
160
        #[arg(short, long)]
161
        verbose: bool,
162
        /// Only changes touching this path (renames followed)
163
        #[arg(long)]
164
        path: Option<String>,
165
        /// Only changes that wrote or deleted lines START to END of PATH
166
        /// (numbered as in the working copy's base), or deleted lines from
167
        /// between them: the history of a function
168
        #[arg(short = 'L', value_name = "START,END:PATH", conflicts_with = "path")]
169
        lines: Option<String>,
170
        /// Machine-readable output: one JSON document on stdout
171
        #[arg(long)]
172
        json: bool,
173
    },
174
    /// Show what the working copy changes against its base
175
    Diff {
176
        /// A change instead of the working copy
177
        change: Option<String>,
178
        /// Machine-readable output: one JSON document on stdout
179
        #[arg(long)]
180
        json: bool,
181
    },
182
    /// Print a file as it is in a state (default: the working copy's base)
183
    Cat {
184
        /// File to print, relative to the repository root
185
        path: String,
186
        /// A change, bookmark, or tag
187
        #[arg(short = 'r', long)]
188
        change: Option<String>,
189
    },
190
    /// Print the repository root, workspace, and bookmark
191
    Root {
192
        /// Machine-readable output: one JSON document on stdout
193
        #[arg(long)]
194
        json: bool,
195
    },
196
    /// Print the version of oxy, its repository format, and protocol
197
    Version {
198
        /// Machine-readable output: one JSON document on stdout
199
        #[arg(long)]
200
        json: bool,
201
    },
202
    /// Restore paths to the working copy's base, or undo chosen hunks
203
    Discard {
204
        /// Paths to restore; none with --hunks, or everything with neither
205
        paths: Vec<String>,
206
        /// Hunk indices as `oxy hunks` numbers them, comma separated
207
        #[arg(long, value_delimiter = ',')]
208
        hunks: Vec<usize>,
209
    },
210
    /// List the working copy's hunks, numbered as commit --hunks expects
211
    Hunks {
212
        /// Machine-readable output: one JSON document on stdout
213
        #[arg(long)]
214
        json: bool,
215
    },
216
    /// With -m, change the message of a head; without, name the working
217
    /// copy for a build: its tag, or the nearest tag plus distance and
218
    /// change, or the bookmark and change, and -dirty
219
    Describe {
220
        /// The new message
221
        #[arg(short, long)]
222
        message: Option<String>,
223
        /// The head to describe (default: the one the working copy sits on)
224
        change: Option<String>,
225
        /// Machine-readable output (naming only)
226
        #[arg(long, conflicts_with = "message")]
227
        json: bool,
228
    },
229
    /// Fold the working copy into the head it sits on, or into CHANGE with --into
230
    Amend {
231
        /// Replace the message too
232
        #[arg(short, long)]
233
        message: Option<String>,
234
        /// Amend an older change; the edits must touch only lines it already knows
235
        #[arg(long)]
236
        into: Option<String>,
237
    },
238
    /// Split a head: the named paths become a separate first change
239
    Split {
240
        /// The head to split
241
        change: String,
242
        /// Paths whose edits become the first change
243
        paths: Vec<String>,
244
    },
245
    /// Drop a head from the bookmark; its content stays on disk as uncommitted work
246
    Abandon {
247
        /// The head to drop (ChangeId prefix, #hash prefix)
248
        change: String,
249
    },
250
    /// Show one change (ChangeId prefix, #hash prefix, or @)
251
    Show {
252
        /// ChangeId prefix, #hash prefix, or @ for the working copy
253
        change: String,
254
        /// Machine-readable output: one JSON document on stdout
255
        #[arg(long)]
256
        json: bool,
257
    },
258
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
259
    Checkout {
260
        /// Changes whose state to check out (default: the bookmark heads)
261
        changes: Vec<String>,
262
        /// Throw away uncommitted working-copy changes
263
        #[arg(long)]
264
        discard: bool,
265
        /// Take uncommitted changes along; refused when the target has
266
        /// other versions of the files they touch
267
        #[arg(long, conflicts_with = "discard")]
268
        carry: bool,
269
    },
270
    /// Report which conflicts of the working copy's base are resolved by the current files
271
    Resolve {
272
        /// Machine-readable output: one JSON document on stdout
273
        #[arg(long)]
274
        json: bool,
275
    },
276
    /// Undo the last operation
277
    Undo,
278
    /// Write a checkpoint of the current bookmark's state now
279
    Checkpoint,
280
    /// Print a shell completion script: `oxy completions bash > ~/.local/share/bash-completion/completions/oxy`
281
    Completions {
282
        /// bash, zsh, fish, elvish or powershell
283
        shell: clap_complete::Shell,
284
    },
285
    /// Write man pages (oxy.1 and one per command) into DIR, or print oxy.1
286
    Man {
287
        /// Directory to write every page into, e.g. ~/.local/share/man/man1
288
        #[arg(long)]
289
        dir: Option<PathBuf>,
290
    },
291
    /// Check the identity, configuration and remotes, and say what to do about any problem
292
    Doctor {
293
        /// Do not contact the remotes
294
        #[arg(long)]
295
        offline: bool,
296
        /// Machine-readable output: one JSON document on stdout
297
        #[arg(long)]
298
        json: bool,
299
    },
300
    /// Check the repository's integrity: objects, operation log, references, signatures, checkpoints
301
    Fsck {
302
        /// Machine-readable output: one JSON document on stdout
303
        #[arg(long)]
304
        json: bool,
305
    },
306
    /// Prune unreachable objects older than the grace period and repack everything into one pack
307
    Gc {
308
        /// Ignore the grace period (core.gc-grace) and prune immediately
309
        #[arg(long)]
310
        now: bool,
311
    },
312
    /// List operations
313
    #[command(name = "op")]
314
    Op {
315
        #[command(subcommand)]
316
        cmd: OpCmd,
317
    },
318
    /// Take the advisory local lock on a lockable file, making it writable
319
    Lock {
320
        /// The lockable file
321
        path: String,
322
    },
323
    /// Release the local lock and make the file read-only again
324
    Unlock {
325
        /// The locked file
326
        path: String,
327
    },
328
    /// Workspaces: extra checkout directories sharing this repository
329
    Workspace {
330
        #[command(subcommand)]
331
        cmd: WorkspaceCmd,
332
    },
333
    /// Bookmarks: named states; each workspace works on one
334
    Bookmark {
335
        #[command(subcommand)]
336
        cmd: BookmarkCmd,
337
    },
338
    /// Union another bookmark's heads into the current one (no merge change)
339
    Merge {
340
        /// The bookmark whose heads to add
341
        bookmark: String,
342
    },
343
    /// Sign in to a hub once, for every repository on it
344
    Auth {
345
        #[command(subcommand)]
346
        cmd: AuthCmd,
347
    },
348
    /// Subrepositories: nested repositories pinned to a state
349
    Submodule {
350
        #[command(subcommand)]
351
        cmd: SubmoduleCmd,
352
    },
353
    /// Execution receipts: signed records of what a runner ran on a state
354
    Receipt {
355
        #[command(subcommand)]
356
        cmd: ReceiptCmd,
357
    },
358
    /// Tags: immutable named states
359
    Tag {
360
        #[command(subcommand)]
361
        cmd: TagCmd,
362
    },
363
    /// Manage remotes
364
    Remote {
365
        #[command(subcommand)]
366
        cmd: RemoteCmd,
367
    },
368
    /// Fetch every bookmark of a remote into its tracking references
369
    Fetch {
370
        /// Remote name (default: origin)
371
        remote: Option<String>,
372
        /// Machine-readable output: one JSON document on stdout
373
        #[arg(long)]
374
        json: bool,
375
    },
376
    /// Fetch, then union the remote bookmark into the local one (no merge change)
377
    Pull {
378
        /// Remote name (default: origin)
379
        remote: Option<String>,
380
        /// Machine-readable output: one JSON document on stdout
381
        #[arg(long)]
382
        json: bool,
383
    },
384
    /// Push the current bookmark to a remote
385
    Push {
386
        /// Remote name (default: origin)
387
        remote: Option<String>,
388
        /// Allow dropping heads the remote has
389
        #[arg(long)]
390
        force: bool,
391
        /// Push this bookmark instead of the workspace's current one
392
        #[arg(short = 'b', long)]
393
        bookmark: Option<String>,
394
        /// Delete this bookmark on the remote (never its default bookmark);
395
        /// the local bookmark is kept
396
        #[arg(long, value_name = "NAME", conflicts_with_all = ["bookmark", "force"])]
397
        delete: Option<String>,
398
        /// Machine-readable output: one JSON document on stdout
399
        #[arg(long)]
400
        json: bool,
401
    },
402
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
403
    Serve {
404
        /// Directory with one repository per subdirectory
405
        #[arg(long, default_value = ".")]
406
        root: PathBuf,
407
        /// Address and port to listen on
408
        #[arg(long, default_value = "127.0.0.1:8890")]
409
        bind: String,
410
        /// Bearer token allowed to push (repeatable); none means open
411
        #[arg(long = "token")]
412
        tokens: Vec<String>,
413
        /// PEM certificate chain; enables HTTPS together with --tls-key
414
        #[arg(long, requires = "tls_key")]
415
        tls_cert: Option<PathBuf>,
416
        /// PEM private key
417
        #[arg(long, requires = "tls_cert")]
418
        tls_key: Option<PathBuf>,
419
    },
420
    /// Manage the signing identity
421
    Identity {
422
        #[command(subcommand)]
423
        cmd: IdentityCmd,
424
    },
425
    /// Seal changes with your key: named ones, or every change on the current bookmark you can authenticate but have not
426
    Seal {
427
        /// Changes to seal (default: every change on the bookmark you can authenticate and have not sealed)
428
        changes: Vec<String>,
429
        /// Seal every change of a hub proposal and push the seal (your approval)
430
        #[arg(long, conflicts_with = "changes")]
431
        proposal: Option<String>,
432
        /// Approve a job of a hub run that waits for a sealed approval: `--flow https://hub/OWNER/NAME/flow/N --job apply`
433
        #[arg(long, conflicts_with_all = ["changes", "proposal"], requires = "job")]
434
        flow: Option<String>,
435
        /// The job to approve, with --flow
436
        #[arg(long, requires = "flow")]
437
        job: Option<String>,
438
        /// Reject the job instead of approving it, with --flow
439
        #[arg(long, requires = "flow")]
440
        reject: bool,
441
        /// Machine-readable output: one JSON document on stdout
442
        #[arg(long)]
443
        json: bool,
444
    },
445
    /// Hub proposals: show one, or land it from this client
446
    Proposal {
447
        #[command(subcommand)]
448
        cmd: ProposalCmd,
449
    },
450
    /// Verify the signatures on a change (default: every change in the log)
451
    Verify {
452
        /// The change to verify (default: every change in the log)
453
        change: Option<String>,
454
        /// Machine-readable output: one JSON document on stdout
455
        #[arg(long)]
456
        json: bool,
457
    },
458
    /// Git interoperability
459
    Git {
460
        #[command(subcommand)]
461
        cmd: GitCmd,
462
    },
463
    /// Manage the trust policy (who may update which bookmarks)
464
    Policy {
465
        #[command(subcommand)]
466
        cmd: PolicyCmd,
467
    },
468
    /// Set a configuration key (author.*, core.sign, core.op-retention, core.gc-grace, core.checkpoint-interval, filter.max-blob, filter.paths, workspace.bookmark, remote.<name>.url|token|ca, auth.helper with --user)
469
    Config {
470
        /// The key, such as author.name or remote.origin.token
471
        key: String,
472
        /// The value to set
473
        value: String,
474
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
475
        #[arg(long)]
476
        user: bool,
477
    },
478
}
479
480
#[derive(Subcommand)]
481
enum OpCmd {
482
    /// List operations, newest first, with what each changed
483
    Log {
484
        /// Show only the latest N (snapshots folded together count as one)
485
        #[arg(short = 'n', long)]
486
        limit: Option<usize>,
487
        /// Machine-readable output: one JSON document on stdout
488
        #[arg(long)]
489
        json: bool,
490
    },
491
    /// Go back to the bookmarks and working copy as they were after OP (recorded as a new operation, so it can be undone)
492
    Restore {
493
        /// The operation: an ID prefix from oxy op log, @ or @-
494
        op: String,
495
        /// Throw away uncommitted working-copy changes
496
        #[arg(long)]
497
        discard: bool,
498
    },
499
    /// Undo OP alone, keeping every operation after it: the bookmarks it moved go back
500
    Revert {
501
        /// The operation: an ID prefix from oxy op log
502
        op: String,
503
    },
504
}
505
506
#[derive(Subcommand)]
507
enum WorkspaceCmd {
508
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
509
    Add {
510
        /// Directory for the new workspace (must be empty or absent)
511
        path: PathBuf,
512
        /// Changes whose state to check out there
513
        changes: Vec<String>,
514
    },
515
    /// List workspaces and their working-copy changes
516
    List {
517
        /// Machine-readable output: one JSON document on stdout
518
        #[arg(long)]
519
        json: bool,
520
    },
521
    /// Stop tracking a workspace (by id prefix or path); its files stay
522
    Forget {
523
        /// Workspace id prefix or path
524
        workspace: String,
525
    },
526
}
527
528
#[derive(Subcommand)]
529
enum BisectCmd {
530
    /// Begin; remembers where the working copy was
531
    Start,
532
    /// Mark CHANGE... (or the state under test) as good
533
    Good {
534
        /// Changes known to be good (default: the state under test)
535
        changes: Vec<String>,
536
    },
537
    /// Mark CHANGE... (or the state under test) as bad
538
    Bad {
539
        /// Changes known to be bad (default: the state under test)
540
        changes: Vec<String>,
541
    },
542
    /// The state under test cannot be tested; try another
543
    Skip,
544
    /// Run a command for each step: exit 0 good, 125 skip, else bad
545
    Run {
546
        /// Command and arguments to run at each step
547
        #[arg(required = true, trailing_var_arg = true)]
548
        command: Vec<String>,
549
    },
550
    /// Where the search stands
551
    Status {
552
        /// Machine-readable output: one JSON document on stdout
553
        #[arg(long)]
554
        json: bool,
555
    },
556
    /// Restore the working copy and forget the search
557
    Reset,
558
}
559
560
#[derive(Subcommand)]
561
enum BookmarkCmd {
562
    /// Create a bookmark at CHANGE... (default: the working copy's base)
563
    Create {
564
        /// Name of the new bookmark
565
        name: String,
566
        /// Its heads (default: the working copy's base). A state is its
567
        /// heads and what they depend on: a change alone may leave out
568
        /// earlier changes of the current state, which is refused unless
569
        /// --upto or --only says which is meant
570
        changes: Vec<String>,
571
        /// Everything of the current state up to CHANGE..., as a Git
572
        /// branch at a commit would hold
573
        #[arg(long, conflicts_with = "only")]
574
        upto: bool,
575
        /// Just CHANGE... and what they depend on
576
        #[arg(long)]
577
        only: bool,
578
    },
579
    /// Point an existing bookmark at CHANGE...
580
    Set {
581
        /// The bookmark to move
582
        name: String,
583
        /// Its new heads. A change alone may leave out changes the
584
        /// bookmark now holds that were recorded before it, which is
585
        /// refused unless --upto or --only says which is meant
586
        changes: Vec<String>,
587
        /// Everything the bookmark holds up to CHANGE..., as a Git branch
588
        /// moved to a commit would hold
589
        #[arg(long, conflicts_with = "only")]
590
        upto: bool,
591
        /// Just CHANGE... and what they depend on
592
        #[arg(long)]
593
        only: bool,
594
    },
595
    /// Remove a bookmark; its changes stay
596
    Delete {
597
        /// The bookmark to remove
598
        name: String,
599
    },
600
    /// List bookmarks with their heads and difference from origin
601
    List {
602
        /// Also list remotes' bookmarks that have no local bookmark
603
        #[arg(long, short)]
604
        all: bool,
605
        /// Machine-readable output: one JSON document on stdout
606
        #[arg(long)]
607
        json: bool,
608
    },
609
    /// Make NAME this workspace's bookmark and check it out. Uncommitted work stops it; --carry takes the work along, --discard drops it
610
    Switch {
611
        /// The bookmark to work on; one only a remote has (NAME or REMOTE/NAME) is created from it
612
        name: String,
613
        /// Throw away uncommitted working-copy changes
614
        #[arg(long)]
615
        discard: bool,
616
        /// Take uncommitted changes along; refused when NAME has other
617
        /// versions of the files they touch
618
        #[arg(long, conflicts_with = "discard")]
619
        carry: bool,
620
    },
621
}
622
623
#[derive(Subcommand)]
624
enum AuthCmd {
625
    /// Sign in to HUB in the browser (device sign-in) and keep the token for
626
    /// every repository there
627
    Login {
628
        /// The hub, as https://oxygenhub.io or oxygenhub.io (default: this checkout's origin)
629
        hub: Option<String>,
630
        /// Read a token from standard input instead (for CI and scripts)
631
        #[arg(long)]
632
        with_token: bool,
633
        /// PEM file of extra roots to trust (a hub with a self-signed certificate)
634
        #[arg(long)]
635
        ca: Option<PathBuf>,
636
    },
637
    /// List the hubs you are signed in to, checking each token with its hub
638
    Status,
639
    /// Forget HUB's token and revoke it on the hub
640
    Logout {
641
        /// The hub (default: this checkout's origin, or the only one)
642
        hub: Option<String>,
643
    },
644
}
645
646
#[derive(Subcommand)]
647
enum SubmoduleCmd {
648
    /// Clone URL (Oxygen or Git) into PATH and register it in .oxymodules
649
    Add {
650
        /// URL or path of the Oxygen or Git repository
651
        url: String,
652
        /// Where to put it, relative to the repository root
653
        path: String,
654
        /// Bookmark (Oxygen) or branch (Git) to check out and track
655
        #[arg(long)]
656
        bookmark: Option<String>,
657
    },
658
    /// Bring nested repositories to their pins
659
    Update {
660
        /// Clone nested repositories that are absent
661
        #[arg(long)]
662
        init: bool,
663
        /// Subrepos to update (default: all)
664
        paths: Vec<String>,
665
    },
666
    /// List subrepos with their pins and on-disk state
667
    Status {
668
        /// Machine-readable output: one JSON document on stdout
669
        #[arg(long)]
670
        json: bool,
671
    },
672
    /// Unregister PATH; the directory stays, the node goes with the next commit
673
    Remove {
674
        /// The subrepo to unregister
675
        path: String,
676
    },
677
}
678
679
#[derive(Subcommand)]
680
enum ReceiptCmd {
681
    /// The receipts kept for a state (default: the bookmark heads)
682
    List {
683
        /// Change, bookmark or tag whose state to look at
684
        rev: Option<String>,
685
        /// Machine-readable output: one JSON document on stdout
686
        #[arg(long)]
687
        json: bool,
688
    },
689
    /// One receipt in full, and whether its signature verifies
690
    Show {
691
        /// The receipt (a prefix of its hash, as `list` shows it)
692
        receipt: String,
693
        /// Change, bookmark or tag whose state holds it
694
        #[arg(long)]
695
        rev: Option<String>,
696
        /// Machine-readable output: one JSON document on stdout
697
        #[arg(long)]
698
        json: bool,
699
    },
700
    /// Write a signed receipt to FILE
701
    Export {
702
        /// The receipt (a prefix of its hash, as `list` shows it)
703
        receipt: String,
704
        /// Where to write it
705
        file: String,
706
        /// Change, bookmark or tag whose state holds it
707
        #[arg(long)]
708
        rev: Option<String>,
709
        /// `oxygen`: the runner's signed receipt, as `verify` and `import`
710
        /// read it. `slsa`: SLSA provenance in a DSSE envelope, signed with
711
        /// your key, for in-toto and cosign (`oxy identity export --pem`
712
        /// gives the key they check it with)
713
        #[arg(long, default_value = "oxygen", value_parser = ["oxygen", "slsa"])]
714
        format: String,
715
    },
716
    /// Check a signed receipt in FILE without keeping it
717
    Verify {
718
        /// A signed receipt, as a runner or `export` writes it
719
        file: String,
720
        /// Machine-readable output: one JSON document on stdout
721
        #[arg(long)]
722
        json: bool,
723
    },
724
    /// Verify a signed receipt in FILE and keep it with its state
725
    Import {
726
        /// A signed receipt, as a runner or `export` writes it
727
        file: String,
728
    },
729
}
730
731
#[derive(Subcommand)]
732
enum TagCmd {
733
    /// Tag the state of CHANGE... (default: the bookmark heads)
734
    Create {
735
        /// Name of the tag; tags never move
736
        name: String,
737
        /// Changes whose state to tag (default: the bookmark heads)
738
        changes: Vec<String>,
739
        /// A message recorded with the tag
740
        #[arg(short, long, default_value = "")]
741
        message: String,
742
    },
743
    /// List tags and the states they name
744
    List {
745
        /// Machine-readable output: one JSON document on stdout
746
        #[arg(long)]
747
        json: bool,
748
    },
749
}
750
751
#[derive(Subcommand)]
752
enum RemoteCmd {
753
    /// Add a remote
754
    Add {
755
        /// Name to refer to the remote by, such as origin
756
        name: String,
757
        /// Local path or http(s) URL of the repository
758
        url: String,
759
    },
760
    /// Remove a remote
761
    Remove {
762
        /// The remote to remove
763
        name: String,
764
    },
765
    /// List remotes and their URLs
766
    List {
767
        /// Machine-readable output: one JSON document on stdout
768
        #[arg(long)]
769
        json: bool,
770
    },
771
}
772
773
#[derive(Subcommand)]
774
enum ProposalCmd {
775
    /// Show a proposal by its hub URL
776
    Show {
777
        /// The proposal's URL on the hub
778
        url: String,
779
        /// Machine-readable output: one JSON document on stdout
780
        #[arg(long)]
781
        json: bool,
782
    },
783
    /// Fetch the proposal's changes, set the target bookmark to the projected state, and push with your key
784
    Land {
785
        /// The proposal's URL on the hub
786
        url: String,
787
        /// Land even when the hub reports blockers
788
        #[arg(long)]
789
        force: bool,
790
    },
791
}
792
793
#[derive(Subcommand)]
794
enum IdentityCmd {
795
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
796
    Create {
797
        /// Your name (default: author.name)
798
        #[arg(long)]
799
        name: Option<String>,
800
        /// Your email (default: author.email)
801
        #[arg(long)]
802
        email: Option<String>,
803
        /// Record the author in this repository only, not in the user config
804
        #[arg(long)]
805
        local: bool,
806
        /// Expire the identity this many days from now
807
        #[arg(long)]
808
        expires: Option<u64>,
809
    },
810
    /// Show the configured identity
811
    Show,
812
    /// Write your identity object to a file, to hand to a policy holder
813
    Export {
814
        /// File to write (default: <key>.identity, or <key>.pem with --pem, in the current directory)
815
        path: Option<PathBuf>,
816
        /// Write the Ed25519 public key as PEM instead, as cosign's --key and
817
        /// other DSSE verifiers take it
818
        #[arg(long)]
819
        pem: bool,
820
    },
821
    /// Sign a host's challenge (hex, at least 16 bytes) to prove you hold your key; prints the signature as hex
822
    SignChallenge {
823
        /// The host's challenge, as hex
824
        challenge: String,
825
        /// Machine-readable output: one JSON document on stdout
826
        #[arg(long)]
827
        json: bool,
828
    },
829
    /// Move to a fresh key pair; the policy follows if it names your key
830
    Rotate,
831
    /// Re-issue your identity with a new expiry (in days from now; 0 = never)
832
    Renew {
833
        /// Days from now until it expires; 0 for never
834
        #[arg(long)]
835
        expires: u64,
836
    },
837
    /// Revoke a key: yours by default, or any key if you hold a policy root
838
    Revoke {
839
        /// KeyId prefix (default: your own key)
840
        key: Option<String>,
841
        /// Why, recorded in the revocation
842
        #[arg(long, default_value = "")]
843
        reason: String,
844
    },
845
    /// Keys allowed to sign or seal on your behalf
846
    Delegate {
847
        #[command(subcommand)]
848
        cmd: DelegateCmd,
849
    },
850
}
851
852
#[derive(Subcommand)]
853
enum DelegateCmd {
854
    /// Add a delegate from an exported identity file
855
    Add {
856
        /// The delegate's exported identity file
857
        identity_file: PathBuf,
858
        /// What it may do for you: changes, refs, seal (comma-separated) or all.
859
        /// An agent gets `changes`; a host that lands for you needs `changes,refs`
860
        #[arg(long, default_value = "changes")]
861
        scope: String,
862
        /// How long for: 30m, 8h, 7d, or a number of days (default: until removed)
863
        #[arg(long)]
864
        expires: Option<String>,
865
    },
866
    /// Remove a delegate by KeyId prefix
867
    Remove {
868
        /// KeyId prefix of the delegate
869
        key: String,
870
    },
871
    /// List delegates
872
    List,
873
}
874
875
#[derive(Subcommand)]
876
enum GitCmd {
877
    /// Export the current bookmark to a Git repository (created bare if absent)
878
    Export {
879
        /// Git repository to write to (created bare if absent)
880
        git_dir: PathBuf,
881
        /// Git branch to write (default: the bookmark name)
882
        #[arg(long)]
883
        branch: Option<String>,
884
    },
885
    /// Fetch a branch from a Git remote (path or URL) into the current bookmark
886
    Fetch {
887
        /// Git remote: a path or URL
888
        url: String,
889
        /// Branch to fetch
890
        #[arg(long, default_value = "main")]
891
        branch: String,
892
    },
893
    /// Push the current bookmark to a branch of a Git remote (fetch first)
894
    Push {
895
        /// Git remote: a path or URL
896
        url: String,
897
        /// Branch to push to
898
        #[arg(long, default_value = "main")]
899
        branch: String,
900
    },
901
    /// Check this repository's import of a Git branch: every commit's tree must be reproduced exactly
902
    Verify {
903
        /// The Git repository the import came from
904
        git_dir: PathBuf,
905
        /// Branch that was imported
906
        #[arg(long, default_value = "main")]
907
        branch: String,
908
    },
909
    /// Import a Git branch into a new Oxygen repository at DEST
910
    Import {
911
        /// Git repository to read
912
        git_dir: PathBuf,
913
        /// Directory for the new Oxygen repository
914
        dest: PathBuf,
915
        /// Branch to import
916
        #[arg(long, default_value = "main")]
917
        branch: String,
918
    },
919
}
920
921
#[derive(Subcommand)]
922
enum PolicyCmd {
923
    /// Create the initial policy with your identity as the only root
924
    Init,
925
    /// Show the current policy
926
    Show {
927
        /// Machine-readable output: one JSON document on stdout
928
        #[arg(long)]
929
        json: bool,
930
    },
931
    /// Allow an identity (from `oxy identity export`) to update bookmarks
932
    Grant {
933
        /// The identity to grant, exported with `oxy identity export`
934
        identity_file: PathBuf,
935
        /// Reference pattern the grant applies to (default: every bookmark)
936
        #[arg(long)]
937
        pattern: Option<String>,
938
        /// Where to write the proposal when more root signatures are needed
939
        #[arg(long)]
940
        out: Option<PathBuf>,
941
    },
942
    /// Allow holders of an issuer's certificates for SUBJECT to sign (keyless)
943
    GrantSubject {
944
        /// Who: `repo:OWNER/NAME:bookmark:main`, `agent:NAME@ACCOUNT`; `*` matches any run of characters
945
        subject: String,
946
        /// The issuer's identity file, e.g. a hub's /.well-known/oxyhub-key
947
        #[arg(long)]
948
        issuer: PathBuf,
949
        /// What its keys may sign: changes, refs, seal
950
        #[arg(long, default_value = "changes,refs")]
951
        scope: String,
952
        /// Reference pattern the grant applies to (default: every bookmark)
953
        #[arg(long)]
954
        pattern: Option<String>,
955
        /// Where to write the proposal when more root signatures are needed
956
        #[arg(long)]
957
        out: Option<PathBuf>,
958
    },
959
    /// Revoke a key (KeyId prefix of an identity in the policy)
960
    Revoke {
961
        /// KeyId prefix of the identity
962
        key: String,
963
        /// Where to write the proposal when more root signatures are needed
964
        #[arg(long)]
965
        out: Option<PathBuf>,
966
    },
967
    /// Manage root keys and the signature threshold for policy updates
968
    Root {
969
        #[command(subcommand)]
970
        cmd: RootCmd,
971
    },
972
    /// Add your root signature to a proposal file; applies it once the threshold is met
973
    Sign {
974
        /// The proposal file to sign
975
        proposal: PathBuf,
976
        /// Where to write the proposal if it still needs signatures (default: in place)
977
        #[arg(long)]
978
        out: Option<PathBuf>,
979
    },
980
}
981
982
#[derive(Subcommand)]
983
enum RootCmd {
984
    /// Make an exported identity a root key
985
    Add {
986
        /// The identity to make a root, exported with `oxy identity export`
987
        identity_file: PathBuf,
988
        /// Where to write the proposal when more root signatures are needed
989
        #[arg(long)]
990
        out: Option<PathBuf>,
991
    },
992
    /// Remove a root key by KeyId prefix
993
    Remove {
994
        /// KeyId prefix of the root key
995
        key: String,
996
        /// Where to write the proposal when more root signatures are needed
997
        #[arg(long)]
998
        out: Option<PathBuf>,
999
    },
1000
    /// Require this many root signatures on policy updates
1001
    Threshold {
1002
        /// Number of root signatures
1003
        n: u32,
1004
        /// Where to write the proposal when more root signatures are needed
1005
        #[arg(long)]
1006
        out: Option<PathBuf>,
1007
    },
1008
}
1009
1010
/// Open the repository containing the current directory.
1011
fn open() -> Result<Repo> {
1012
    let repo = Repo::open(&std::env::current_dir()?).context("opening repository")?;
1013
    match AT_OP.get().and_then(|o| o.as_deref()) {
1014
        Some(op) => Ok(repo.at_op(op)?),
1015
        None => Ok(repo),
1016
    }
1017
}
1018
1019
/// `oxy man`: every page into `dir`, or the top-level page on stdout.
1020
fn man(dir: Option<PathBuf>) -> Result<i32> {
1021
    use clap::CommandFactory;
1022
    let cmd = Cli::command();
1023
    match dir {
1024
        Some(dir) => {
1025
            std::fs::create_dir_all(&dir)?;
1026
            clap_mangen::generate_to(cmd, &dir)?;
1027
            let pages = std::fs::read_dir(&dir)?
1028
                .filter(|e| {
1029
                    e.as_ref()
1030
                        .is_ok_and(|e| e.path().extension().is_some_and(|x| x == "1"))
1031
                })
1032
                .count();
1033
            println!(
1034
                "{}",
1035
                ui::done(format!("Wrote {pages} man pages to {}", dir.display()))
1036
            );
1037
        }
1038
        None => clap_mangen::Man::new(cmd).render(&mut std::io::stdout())?,
1039
    }
1040
    Ok(0)
1041
}
1042
1043
/// Parse arguments and run; returns the process exit code.
1044
pub fn run() -> Result<i32> {
1045
    let cli = Cli::parse();
1046
    ui::init(cli.plain);
1047
    AT_OP.set(cli.at_op.clone()).ok();
1048
    match cli.cmd {
1049
        Cmd::Completions { shell } => {
1050
            use clap::CommandFactory;
1051
            clap_complete::generate(shell, &mut Cli::command(), "oxy", &mut std::io::stdout());
1052
            Ok(0)
1053
        }
1054
        Cmd::Man { dir } => man(dir),
1055
        Cmd::Init { path, bare } => workspace::init(path, bare),
1056
        Cmd::Status { json } => workspace::status(json),
1057
        Cmd::Prompt {
1058
            format,
1059
            glyphs,
1060
            color,
1061
        } => workspace::prompt(&format, glyphs, color.as_deref()),
1062
        Cmd::Commit {
1063
            message,
1064
            file,
1065
            depends_on,
1066
            paths,
1067
            interactive,
1068
            hunks,
1069
        } => {
1070
            let message = match (message, file) {
1071
                (Some(m), _) => m,
1072
                (None, Some(f)) if f.as_os_str() == "-" => {
1073
                    let mut s = String::new();
1074
                    std::io::Read::read_to_string(&mut std::io::stdin(), &mut s)?;
1075
                    s
1076
                }
1077
                (None, Some(f)) => std::fs::read_to_string(&f).map_err(|e| {
1078
                    anyhow::anyhow!("reading the message from {}: {e}", f.display())
1079
                })?,
1080
                (None, None) => anyhow::bail!("a commit message is required (-m or -F)"),
1081
            };
1082
            workspace::commit(&message, &depends_on, &paths, interactive, &hunks)
1083
        }
1084
        Cmd::Cat { path, change } => workspace::cat(&path, change.as_deref()),
1085
        Cmd::Root { json } => workspace::root(json),
1086
        Cmd::Version { json } => workspace::version(json),
1087
        Cmd::Discard { paths, hunks } => workspace::discard(&paths, &hunks),
1088
        Cmd::Hunks { json } => workspace::hunks(json),
1089
        Cmd::Squash { change, into } => workspace::squash(&change, &into),
1090
        Cmd::Rebase {
1091
            change,
1092
            onto,
1093
            into_working_copy,
1094
        } => workspace::rebase(&change, &onto, into_working_copy),
1095
        Cmd::Blame { path, json } => workspace::blame(&path, json),
1096
        Cmd::Bisect { cmd } => match cmd {
1097
            BisectCmd::Start => workspace::bisect_start(),
1098
            BisectCmd::Good { changes } => workspace::bisect_mark(Some(true), &changes),
1099
            BisectCmd::Bad { changes } => workspace::bisect_mark(Some(false), &changes),
1100
            BisectCmd::Skip => workspace::bisect_mark(None, &[]),
1101
            BisectCmd::Run { command } => workspace::bisect_run(&command),
1102
            BisectCmd::Status { json } => workspace::bisect_status(json),
1103
            BisectCmd::Reset => workspace::bisect_reset(),
1104
        },
1105
        Cmd::Log {
1106
            limit,
1107
            verbose,
1108
            path,
1109
            lines,
1110
            json,
1111
        } => workspace::log(limit, verbose, path.as_deref(), lines.as_deref(), json),
1112
        Cmd::Diff { change, json } => workspace::diff(change.as_deref(), json),
1113
        Cmd::Describe {
1114
            message,
1115
            change,
1116
            json,
1117
        } => match message {
1118
            Some(m) => workspace::describe(&m, change.as_deref()),
1119
            None if change.is_some() => {
1120
                anyhow::bail!(
1121
                    "naming works on the working copy; to change a head's message pass -m"
1122
                )
1123
            }
1124
            None => workspace::describe_name(json),
1125
        },
1126
        Cmd::Amend { message, into } => workspace::amend(message.as_deref(), into.as_deref()),
1127
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
1128
        Cmd::Abandon { change } => workspace::abandon(&change),
1129
        Cmd::Show { change, json } => workspace::show(&change, json),
1130
        Cmd::Checkout {
1131
            changes,
1132
            discard,
1133
            carry,
1134
        } => workspace::checkout(&changes, discard, carry),
1135
        Cmd::Resolve { json } => workspace::resolve(json),
1136
        Cmd::Undo => workspace::undo(),
1137
        Cmd::Gc { now } => workspace::gc(now),
1138
        Cmd::Fsck { json } => workspace::fsck(json),
1139
        Cmd::Doctor { offline, json } => workspace::doctor(offline, json),
1140
        Cmd::Checkpoint => workspace::checkpoint(),
1141
        Cmd::Op { cmd } => match cmd {
1142
            OpCmd::Log { limit, json } => workspace::op_log(limit, json),
1143
            OpCmd::Restore { op, discard } => workspace::op_restore(&op, discard),
1144
            OpCmd::Revert { op } => workspace::op_revert(&op),
1145
        },
1146
        Cmd::Clone {
1147
            url,
1148
            dest,
1149
            max_blob,
1150
            sparse,
1151
            depth,
1152
            recursive,
1153
        } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth, recursive),
1154
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
1155
        Cmd::Sparse { paths } => workspace::sparse(&paths),
1156
        Cmd::Lock { path } => workspace::lock(&path),
1157
        Cmd::Unlock { path } => workspace::unlock(&path),
1158
        Cmd::Workspace { cmd } => match cmd {
1159
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
1160
            WorkspaceCmd::List { json } => workspace::workspace_list(json),
1161
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
1162
        },
1163
        Cmd::Bookmark { cmd } => match cmd {
1164
            BookmarkCmd::Create {
1165
                name,
1166
                changes,
1167
                upto,
1168
                only,
1169
            } => workspace::bookmark_create(&name, &changes, upto, only),
1170
            BookmarkCmd::Set {
1171
                name,
1172
                changes,
1173
                upto,
1174
                only,
1175
            } => workspace::bookmark_set(&name, &changes, upto, only),
1176
            BookmarkCmd::Delete { name } => workspace::bookmark_delete(&name),
1177
            BookmarkCmd::List { all, json } => workspace::bookmark_list(all, json),
1178
            BookmarkCmd::Switch {
1179
                name,
1180
                discard,
1181
                carry,
1182
            } => workspace::bookmark_switch(&name, discard, carry),
1183
        },
1184
        Cmd::Merge { bookmark } => workspace::merge(&bookmark),
1185
        Cmd::Auth { cmd } => match cmd {
1186
            AuthCmd::Login {
1187
                hub,
1188
                with_token,
1189
                ca,
1190
            } => auth::login(hub.as_deref(), with_token, ca),
1191
            AuthCmd::Status => auth::status(),
1192
            AuthCmd::Logout { hub } => auth::logout(hub.as_deref()),
1193
        },
1194
        Cmd::Submodule { cmd } => match cmd {
1195
            SubmoduleCmd::Add {
1196
                url,
1197
                path,
1198
                bookmark,
1199
            } => sync::submodule_add(&url, &path, bookmark.as_deref()),
1200
            SubmoduleCmd::Update { init, paths } => sync::submodule_update(init, &paths),
1201
            SubmoduleCmd::Status { json } => sync::submodule_status(json),
1202
            SubmoduleCmd::Remove { path } => sync::submodule_remove(&path),
1203
        },
1204
        Cmd::Receipt { cmd } => match cmd {
1205
            ReceiptCmd::List { rev, json } => receipt::list(rev.as_deref(), json),
1206
            ReceiptCmd::Show {
1207
                receipt: r,
1208
                rev,
1209
                json,
1210
            } => receipt::show(&r, rev.as_deref(), json),
1211
            ReceiptCmd::Export {
1212
                receipt: r,
1213
                file,
1214
                rev,
1215
                format,
1216
            } => receipt::export(&r, &file, rev.as_deref(), &format),
1217
            ReceiptCmd::Verify { file, json } => receipt::verify(&file, json),
1218
            ReceiptCmd::Import { file } => receipt::import(&file),
1219
        },
1220
        Cmd::Tag { cmd } => match cmd {
1221
            TagCmd::Create {
1222
                name,
1223
                changes,
1224
                message,
1225
            } => workspace::tag_create(&name, &changes, &message),
1226
            TagCmd::List { json } => workspace::tag_list(json),
1227
        },
1228
        Cmd::Remote { cmd } => match cmd {
1229
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
1230
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
1231
            RemoteCmd::List { json } => sync::remote_list(json),
1232
        },
1233
        Cmd::Fetch { remote, json } => sync::fetch(remote.as_deref(), json),
1234
        Cmd::Pull { remote, json } => sync::pull(remote.as_deref(), json),
1235
        Cmd::Push {
1236
            remote,
1237
            force,
1238
            bookmark,
1239
            delete,
1240
            json,
1241
        } => match delete {
1242
            Some(name) => sync::push_delete(remote.as_deref(), &name, json),
1243
            None => sync::push(remote.as_deref(), force, bookmark.as_deref(), json),
1244
        },
1245
        Cmd::Serve {
1246
            root,
1247
            bind,
1248
            tokens,
1249
            tls_cert,
1250
            tls_key,
1251
        } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
1252
        Cmd::Identity { cmd } => match cmd {
1253
            IdentityCmd::Create {
1254
                name,
1255
                email,
1256
                local,
1257
                expires,
1258
            } => identity::create(name, email, local, expires),
1259
            IdentityCmd::Show => identity::show(),
1260
            IdentityCmd::Export { path, pem } => identity::export(path, pem),
1261
            IdentityCmd::SignChallenge { challenge, json } => {
1262
                identity::sign_challenge(&challenge, json)
1263
            }
1264
            IdentityCmd::Rotate => identity::rotate(),
1265
            IdentityCmd::Renew { expires } => identity::renew(expires),
1266
            IdentityCmd::Revoke { key, reason } => identity::revoke(key.as_deref(), &reason),
1267
            IdentityCmd::Delegate { cmd } => match cmd {
1268
                DelegateCmd::Add {
1269
                    identity_file,
1270
                    scope,
1271
                    expires,
1272
                } => identity::delegate_add(&identity_file, &scope, expires.as_deref()),
1273
                DelegateCmd::Remove { key } => identity::delegate_remove(&key),
1274
                DelegateCmd::List => identity::delegate_list(),
1275
            },
1276
        },
1277
        Cmd::Git {
1278
            cmd: GitCmd::Export { git_dir, branch },
1279
        } => sync::git_export(&git_dir, branch.as_deref()),
1280
        Cmd::Git {
1281
            cmd: GitCmd::Fetch { url, branch },
1282
        } => sync::git_fetch(&url, &branch),
1283
        Cmd::Git {
1284
            cmd: GitCmd::Push { url, branch },
1285
        } => sync::git_push(&url, &branch),
1286
        Cmd::Git {
1287
            cmd:
1288
                GitCmd::Import {
1289
                    git_dir,
1290
                    dest,
1291
                    branch,
1292
                },
1293
        } => sync::git_import(&git_dir, &dest, &branch),
1294
        Cmd::Git {
1295
            cmd: GitCmd::Verify { git_dir, branch },
1296
        } => sync::git_verify(&git_dir, &branch),
1297
        Cmd::Policy { cmd } => match cmd {
1298
            PolicyCmd::Init => identity::policy_init(),
1299
            PolicyCmd::Show { json } => identity::policy_show(json),
1300
            PolicyCmd::Grant {
1301
                identity_file,
1302
                pattern,
1303
                out,
1304
            } => identity::policy_grant(&identity_file, pattern.as_deref(), out),
1305
            PolicyCmd::GrantSubject {
1306
                subject,
1307
                issuer,
1308
                scope,
1309
                pattern,
1310
                out,
1311
            } => identity::policy_grant_subject(&subject, &issuer, &scope, pattern.as_deref(), out),
1312
            PolicyCmd::Revoke { key, out } => identity::policy_revoke(&key, out),
1313
            PolicyCmd::Root { cmd } => match cmd {
1314
                RootCmd::Add { identity_file, out } => {
1315
                    identity::policy_root_add(&identity_file, out)
1316
                }
1317
                RootCmd::Remove { key, out } => identity::policy_root_remove(&key, out),
1318
                RootCmd::Threshold { n, out } => identity::policy_threshold(n, out),
1319
            },
1320
            PolicyCmd::Sign { proposal, out } => identity::policy_sign(&proposal, out),
1321
        },
1322
        Cmd::Seal {
1323
            flow: Some(url),
1324
            job: Some(job),
1325
            reject,
1326
            json,
1327
            ..
1328
        } => approve::seal(&url, &job, reject, json),
1329
        Cmd::Seal {
1330
            changes,
1331
            proposal: Some(url),
1332
            json,
1333
            ..
1334
        } if changes.is_empty() => proposal::seal(&url, json),
1335
        Cmd::Seal { changes, json, .. } => identity::seal(&changes, json),
1336
        Cmd::Proposal { cmd } => match cmd {
1337
            ProposalCmd::Show { url, json } => proposal::show(&url, json),
1338
            ProposalCmd::Land { url, force } => proposal::land(&url, force),
1339
        },
1340
        Cmd::Verify { change, json } => identity::verify(change.as_deref(), json),
1341
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
1342
    }
1343
}
1344
1345
#[cfg(test)]
1346
mod help_tests {
1347
    use super::Cli;
1348
    use clap::CommandFactory;
1349
1350
    /// Every command and every argument explains itself, so `--help`,
1351
    /// completions and the man pages are complete.
1352
    #[test]
1353
    fn every_command_and_argument_has_help() {
1354
        fn walk(cmd: &clap::Command, path: &str, missing: &mut Vec<String>) {
1355
            let here = format!("{path} {}", cmd.get_name()).trim().to_string();
1356
            if cmd.get_about().is_none() && path != "" {
1357
                missing.push(format!("{here} (command)"));
1358
            }
1359
            for a in cmd.get_arguments() {
1360
                let id = a.get_id().as_str();
1361
                if id == "help" || id == "version" || a.is_global_set() && path != "" {
1362
                    continue;
1363
                }
1364
                if a.get_help().is_none() && a.get_long_help().is_none() {
1365
                    missing.push(format!("{here} {id}"));
1366
                }
1367
            }
1368
            for sub in cmd.get_subcommands() {
1369
                walk(sub, &here, missing);
1370
            }
1371
        }
1372
        let mut missing = Vec::new();
1373
        walk(&Cli::command(), "", &mut missing);
1374
        assert!(
1375
            missing.is_empty(),
1376
            "{} without help:\n{}",
1377
            missing.len(),
1378
            missing.join("\n")
1379
        );
1380
    }
1381
}