Skip to main content

vgi_cli/
cli.rs

1//! Command-line arguments.
2
3use std::path::PathBuf;
4
5use clap::{Args, Parser, Subcommand, ValueEnum};
6use url::Url;
7use vgi_forge::DEFAULT_REQUIRED_CHECK;
8
9/// `vgi`: VGI tools for the people who hold a repository.
10#[derive(Debug, Parser)]
11#[command(name = "vgi", version, about, long_about = None)]
12pub struct Cli {
13    /// What to do.
14    #[command(subcommand)]
15    pub command: Command,
16}
17
18/// Top-level commands.
19#[derive(Debug, Subcommand)]
20pub enum Command {
21    /// Repository commands.
22    #[command(subcommand)]
23    Repo(RepoCommand),
24}
25
26/// `vgi repo …`.
27#[derive(Debug, Subcommand)]
28pub enum RepoCommand {
29    /// Turn VGI commit trust on for a repository, as its admin.
30    Init(InitArgs),
31}
32
33/// Which forge API to talk to.
34/// `--verify-trust-transport`.
35#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
36pub enum TransportChoice {
37    /// TSP, then DIDComm, then HTTPS.
38    Auto,
39    /// TSP only.
40    Tsp,
41    /// DIDComm only.
42    Didcomm,
43    /// HTTPS only.
44    Https,
45}
46
47impl From<TransportChoice> for vgi_forge::VerifyTransport {
48    fn from(c: TransportChoice) -> Self {
49        match c {
50            TransportChoice::Auto => vgi_forge::VerifyTransport::Auto,
51            TransportChoice::Tsp => vgi_forge::VerifyTransport::Tsp,
52            TransportChoice::Didcomm => vgi_forge::VerifyTransport::Didcomm,
53            TransportChoice::Https => vgi_forge::VerifyTransport::Https,
54        }
55    }
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
59pub enum ForgeChoice {
60    /// GitHub for `github.com/…`; any other host is taken for Forgejo once
61    /// it answers, without the token, as Forgejo or Gitea.
62    Auto,
63    /// GitHub or GitHub Enterprise Server, through `gh`.
64    Github,
65    /// Forgejo or Gitea, with `FORGEJO_TOKEN`.
66    Forgejo,
67}
68
69const INIT_ABOUT: &str = "\
70Turn VGI commit trust on for a repository in a manual or personal-account git \
71namespace: the same bootstrap the community's bridge runs, done as you.
72
73On GitHub it acts through your own `gh` login (`gh auth login`; you need admin \
74on the repository). It commits `.github/workflows/verify-trust.yml` with the \
75registry and VTC DIDs written in as literals, and the exempt web-flow keyring; \
76removes stale TRUST_REGISTRY_DID / VTC_DID repository variables; and converges \
77the \"VGI commit trust\" ruleset on the default branch: pull request required, \
78\"Verify commit trust\" required and pinned to GitHub Actions, no force-push, no \
79deletion, no bypass actors. The owners are the person running this (on a \
80personal repository, the account holder) plus each --code-owner, counted once \
81each. With two or more owners it also writes a managed `.github/CODEOWNERS` \
82block and the ruleset requires a code owner's review, so no pull request can \
83change the check it is judged by without another owner's approval. With one \
84owner, only the check is required. On an organisation repository that is \
85refused unless you pass --solo: anyone else in the organisation with write \
86access could edit the workflow in the very pull request it judges.
87
88These are per-repository guards. They are not the organisation's required \
89workflow, which only the community's bridge sets.
90
91On Forgejo it acts with FORGEJO_TOKEN (a token of yours with write:repository \
92and read:user), sent only to the repository's own host and only after \
93GET /api/v1/version, asked without it, answers as Forgejo or Gitea. It \
94allows fast-forward-only merges, commits \
95`.forgejo/workflows/verify-trust.yml` with the DIDs written in, and \
96converges the default branch's protection: no pushes, the check's status \
97context required, the workflow directories protected, applying to admins.
98
99Every step is check-then-apply: a re-run changes nothing, and --dry-run \
100prints each change (with the file or request body) without making it.
101
102It does not tell the VTC the repository exists. That is `git-ns/repo/adopt`, \
103a Trust Task signed with a VTA session, which this tool does not hold; it \
104prints the `cnm git adopt` command to run instead.";
105
106/// `vgi repo init`.
107#[derive(Debug, Args)]
108#[command(long_about = INIT_ABOUT)]
109pub struct InitArgs {
110    /// The DID of the community's VTC — the only authority the check trusts.
111    #[arg(long, value_name = "DID")]
112    pub vtc: String,
113
114    /// The repository, forge-qualified: `github.com/acme/widgets`. Default:
115    /// the `origin` remote of the clone this runs in.
116    #[arg(long, value_name = "HOST/OWNER/REPO")]
117    pub resource: Option<String>,
118
119    /// The Trust Registry's DID. Default: the `TrustRegistry` referral in
120    /// the VTC's DID document.
121    #[arg(long, value_name = "DID")]
122    pub registry: Option<String>,
123
124    /// A repository owner's DID, for the `cnm git adopt` command printed at
125    /// the end. Repeat for several.
126    #[arg(long = "owner", value_name = "DID")]
127    pub owners: Vec<String>,
128
129    /// Which forge API to use.
130    #[arg(long, value_enum, default_value_t = ForgeChoice::Auto)]
131    pub forge: ForgeChoice,
132
133    /// GitHub: another owner's login, who may approve workflow changes.
134    /// Repeat for several. The owners are you (the account holder, on a
135    /// personal repository) plus these, counted once each; with two or more,
136    /// workflow changes need a code owner's review.
137    #[arg(long = "code-owner", value_name = "LOGIN")]
138    pub code_owners: Vec<String>,
139
140    /// GitHub: accept a single owner on an organisation repository. Only
141    /// the check is required then, and no one has to review workflow
142    /// changes: other members with write access could edit the workflow in
143    /// the pull request it judges. Name a second owner with --code-owner
144    /// instead where you can.
145    #[arg(long, conflicts_with = "code_owners")]
146    pub solo: bool,
147
148    /// GitHub: the armored `web-flow` key for the exempt keyring. Default:
149    /// downloaded from `https://github.com/web-flow.gpg` (github.com only).
150    #[arg(long, value_name = "FILE")]
151    pub platform_keyring: Option<PathBuf>,
152
153    /// The verify-trust action, pinned to a commit
154    /// (`OpenVTC/verifiable-git-infrastructure/.github/actions/verify-trust@<sha>`).
155    /// Default: the commit of the --verify-trust-version tag.
156    #[arg(long, value_name = "REF")]
157    pub verify_trust_action: Option<String>,
158
159    /// The VGI release the check downloads.
160    #[arg(long, value_name = "TAG", default_value = concat!("v", env!("CARGO_PKG_VERSION")))]
161    pub verify_trust_version: String,
162
163    /// Forgejo: SHA-256 of `verify-trust-x86_64-unknown-linux-gnu.tar.gz` in
164    /// that release, pinned in the workflow. Default: the checksum the
165    /// release publishes (trust on first use; say so to reviewers).
166    #[arg(long, value_name = "HEX")]
167    pub verify_trust_sha256: Option<String>,
168
169    /// The Trust Registry binding the written workflow uses (the action's
170    /// `transport` input). `auto` (default: TSP, then DIDComm, then HTTPS,
171    /// no fallback) writes no input; use `https` while the registry's
172    /// mediator does not admit a CI run's throwaway DID.
173    #[arg(long, value_enum, value_name = "BINDING", default_value_t = TransportChoice::Auto)]
174    pub verify_trust_transport: TransportChoice,
175
176    /// The required check's name (the verify-trust job's name).
177    #[arg(long, value_name = "NAME", default_value = DEFAULT_REQUIRED_CHECK)]
178    pub required_check: String,
179
180    /// Forgejo: the instance's root URL. Default: `https://<host>/`.
181    #[arg(long, value_name = "URL")]
182    pub forgejo_url: Option<Url>,
183
184    /// Forgejo: the runner label the job asks for.
185    #[arg(long, value_name = "LABEL", default_value = vgi_forge_forgejo::DEFAULT_RUNS_ON)]
186    pub runs_on: String,
187
188    /// Print every change, with its contents, and make none.
189    #[arg(long)]
190    pub dry_run: bool,
191}