Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 54 additions & 50 deletions apps/origin-src/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! The `origin` command line: argument parsing. The commands themselves are
//! methods of [`Workspace`], which print their output.
//! The `origin` command line: parses the arguments and runs the command.
//! Each command is a method of [`Workspace`], which prints its output.

use std::convert::Infallible;
use std::env;
Expand All @@ -26,24 +26,24 @@ pub struct Cli {
enum Command {
/// Create a repository, with the targets `policy` and `main`.
Init {
/// Where to create it.
/// The directory to create it in.
#[arg(default_value = ".")]
path: PathBuf,
},
#[command(flatten)]
Workspace(WorkspaceCommand),
}

/// Commands run in the repository containing the current directory. Those
/// that read the working copy snapshot it first. Changes are named by a
/// unique prefix of their id, or `@` for the working copy's.
/// Commands that run in the repository containing the current directory.
/// Commands that read the working copy snapshot it first. A change is
/// named by a unique prefix of its id, or by `@` for the working copy's.
#[derive(Debug, Subcommand)]
enum WorkspaceCommand {
/// Show the working copy's change and the files it changes.
Status,
/// Show the working copy's changes, line by line.
Diff,
/// Show a change and its diff: the working copy's by default.
/// Show a change and its diff (the working copy's change by default).
Show {
/// The change.
change: Option<String>,
Expand All @@ -54,7 +54,7 @@ enum WorkspaceCommand {
#[arg(default_value = "")]
description: String,
},
/// Describe what the current change is for.
/// Set the description of the current change.
Describe {
/// The description.
description: String,
Expand All @@ -64,16 +64,17 @@ enum WorkspaceCommand {
/// The change.
change: String,
},
/// Give a change up, keeping it and the reason for others to see.
/// Abandon a change, recording why. The change stays visible to others.
Abandon {
/// The change: the working copy's by default, which then starts a
/// new change where it started.
/// The change (the working copy's by default). Abandoning the working
/// copy's change starts a new change on the same base.
change: Option<String>,
/// Why it was given up.
/// Why the change was abandoned.
#[arg(long)]
reason: String,
},
/// End a change's divergence, with a revision evolving from every head.
/// End a change's divergence with a new revision that evolves from all
/// its heads.
Converge {
/// The change.
change: String,
Expand All @@ -84,7 +85,8 @@ enum WorkspaceCommand {
#[arg(required = true)]
paths: Vec<RepoPath>,
},
/// Show the working copy's changes and the target they're on.
/// Show the working copy's stack of changes and the history of the
/// target it's on.
Log {
/// Show this target's history instead.
target: Option<TargetName>,
Expand All @@ -103,8 +105,9 @@ enum WorkspaceCommand {
Index(IndexCommand),
/// Issue a statement about a revision, such as a check result or a
/// review, or with `--on`, about another statement, such as a `flaky`
/// mark on a failed check, superseding your previous one of the same
/// kind about it, or withdraw that one.
/// mark on a failed check. It supersedes your previous statement of the
/// same kind about the same subject. `--withdraw` takes that one back
/// instead.
#[command(
override_usage = "origin annotate [OPTIONS] <--pass|--fail|--neutral|--withdraw> \
<REV> <KIND>\n origin annotate [OPTIONS] \
Expand All @@ -118,8 +121,7 @@ enum WorkspaceCommand {
#[arg(default_value = "@")]
rev: String,
},
/// Carry results over to a revision from the records with the same
/// input digest.
/// Copy results to a revision from records with the same input digest.
Promote {
/// The revision: `@`, a change for its head, or a revision.
#[arg(default_value = "@")]
Expand All @@ -129,16 +131,17 @@ enum WorkspaceCommand {

#[derive(Debug, Args)]
struct AnnotateArgs {
/// The revision, `@`, a change for its head, or a revision; with
/// `--on`, the kind instead.
/// The revision (`@`, a change for its head, or a revision), or with
/// `--on`, the kind.
#[arg(value_name = "REV|KIND")]
first: String,
/// What kind of statement, such as `ci.test` or `review`, after a
/// revision.
/// The kind of statement, such as `ci.test` or `review`, when the first
/// argument is a revision.
#[arg(value_name = "KIND")]
kind: Option<String>,
/// The statement it's about instead of a revision, by a unique prefix
/// of its id. It's never promoted, so it declares no inputs.
/// The statement to annotate instead of a revision, by a unique prefix
/// of its id. A statement about a statement is never promoted, so it
/// takes no inputs.
#[arg(
long,
value_name = "STATEMENT",
Expand All @@ -147,42 +150,41 @@ struct AnnotateArgs {
on: Option<String>,
#[command(flatten)]
verdict: VerdictArgs,
/// Instead of a verdict, take back your current statement of this kind
/// about the revision or statement: the details may say why.
/// Take back your current statement of this kind about the subject
/// instead of giving a verdict. The details may say why.
#[arg(
long,
group = "VerdictArgs",
conflicts_with_all = ["inputs", "definition", "toolchain", "external"]
)]
withdraw: bool,
/// The files and directories the claim depended on, which lets it be
/// promoted to revisions where they're the same.
/// The files and directories the result depended on. The statement can
/// then be promoted to revisions where they are the same.
#[arg(long, num_args = 1..)]
inputs: Vec<RepoPath>,
/// The check's command and configuration: text, or the hex digest of
/// it.
/// The check's command and configuration, as text or as its hex digest.
#[arg(long, requires = "inputs")]
definition: Option<DigestArg>,
/// The tools that ran it, such as the output of `rustc -V`: text, or the
/// hex digest of it.
/// The tools that ran the check, such as the output of `rustc -V`, as
/// text or as its hex digest.
#[arg(long, requires = "inputs")]
toolchain: Option<DigestArg>,
/// The claim also depended on things outside the inputs, such as the
/// The result also depended on things outside the inputs, such as the
/// network, so it is never promoted.
#[arg(long, requires = "inputs")]
external: bool,
#[command(flatten)]
payload: PayloadArgs,
}

/// At most one source of details.
/// The statement's details, from at most one source.
#[derive(Debug, Args)]
#[group(multiple = false)]
struct PayloadArgs {
/// Details as text, such as a review's message.
#[arg(short, long)]
message: Option<String>,
/// A file holding details, such as a log, or `-` to read them from
/// A file holding the details, such as a log, or `-` to read them from
/// standard input.
#[arg(long)]
payload: Option<PathBuf>,
Expand All @@ -195,7 +197,7 @@ impl PayloadArgs {
}
}

/// Exactly one verdict.
/// The statement's verdict: exactly one is required.
#[derive(Debug, Args)]
#[group(required = true, multiple = false)]
struct VerdictArgs {
Expand All @@ -205,7 +207,7 @@ struct VerdictArgs {
/// The check failed, or you object.
#[arg(long)]
fail: bool,
/// Neither: informational.
/// Neither: the statement is informational.
#[arg(long)]
neutral: bool,
}
Expand All @@ -221,13 +223,13 @@ impl VerdictArgs {
}

impl AnnotateArgs {
/// What the statement is about, and its kind: the revision, then the
/// kind, or with `--on`, the kind alone.
/// Returns what the statement is about and its kind. Without `--on`, the
/// arguments are the revision, then the kind; with `--on`, just the kind.
///
/// # Errors
///
/// Fails, as parsing does, if the arguments aren't one of those, or the
/// kind isn't a valid one.
/// Returns a clap error if the arguments match neither form, or the kind
/// is invalid.
fn subject(&self) -> Result<(Annotated<'_>, Kind), clap::Error> {
let (annotated, kind) = match (&self.on, &self.kind) {
(Some(on), None) => (Annotated::Statement(on), &self.first),
Expand All @@ -251,13 +253,13 @@ impl AnnotateArgs {
Ok((annotated, kind))
}

/// The verdict, or `None` to withdraw.
/// The verdict, or `None` for `--withdraw`.
fn verdict(&self) -> Option<Verdict> {
(!self.withdraw).then(|| self.verdict.verdict())
}

/// The inputs declared, if any. Without a definition or toolchain, they
/// are the digest of nothing.
/// The declared inputs, or `None` without `--inputs`. A missing
/// definition or toolchain is the digest of empty input.
fn inputs(&self) -> Option<Inputs> {
let digest = |arg: &Option<DigestArg>| arg.map_or_else(|| Digest::of(b""), |arg| arg.0);
(!self.inputs.is_empty()).then(|| Inputs {
Expand All @@ -269,7 +271,8 @@ impl AnnotateArgs {
}
}

/// A digest given as its hex, or as the text it's the digest of.
/// A digest argument: valid hex is parsed as a digest, and any other
/// text is hashed.
#[derive(Clone, Copy, Debug)]
struct DigestArg(Digest);

Expand All @@ -290,13 +293,13 @@ enum IndexCommand {
}

impl Cli {
/// Parses the command line, then checks what parsing can't: an
/// `annotate`'s arguments, whose meaning depends on `--on`. Exits as a
/// parse error does on either, with the usage.
/// Parses the command line, then checks the `annotate` arguments, which
/// clap can't check because their meaning depends on `--on`. On an
/// error, prints it with the usage and exits, as clap does.
///
/// # Panics
///
/// Never: `annotate` is a subcommand.
/// Never: `annotate` is always a subcommand.
#[must_use]
pub fn parse_checked() -> Self {
let cli = Self::parse();
Expand All @@ -316,7 +319,8 @@ impl Cli {
///
/// # Errors
///
/// Fails if the command fails; the error is meant for the user.
/// Fails if the configuration cannot be loaded or the command fails. The
/// error is meant for the user.
pub fn run(self) -> Result<()> {
let config = UserConfig::load()?;
match self.command {
Expand Down
37 changes: 19 additions & 18 deletions apps/origin-src/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! The user's configuration, kept outside every repository in
//! `$ORIGIN_CONFIG_DIR`, or else `origin` in `$XDG_CONFIG_HOME` or
//! `~/.config`:
//! The user's configuration. It lives outside every repository, in
//! `$ORIGIN_CONFIG_DIR`, or else in `origin` under `$XDG_CONFIG_HOME` or
//! `~/.config`. It holds:
//!
//! - `config.toml`: `user = "alice"`, who authors revisions (by default
//! `$USER`), and `node = "laptop"`, this node's name (by default the
//! user's).
//! - `node.key`: this node's secret ed25519 key, created by the first
//! `origin init`, readable only by its owner.
//! - `config.toml`: `user = "alice"`, the user who authors revisions
//! (`$USER` by default), and `node = "laptop"`, this node's name (the
//! user name by default).
//! - `node.key`: this node's secret ed25519 key. The first `origin init`
//! creates it, readable only by its owner.

use std::env;
use std::fs::{self, OpenOptions};
Expand All @@ -27,7 +27,7 @@ pub struct UserConfig {
node: PrincipalName,
}

/// `config.toml`.
/// The contents of `config.toml`.
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct ConfigFile {
Expand All @@ -36,7 +36,7 @@ struct ConfigFile {
}

impl UserConfig {
/// The length of `node.key`: the raw secret key.
/// The length of `node.key`, which holds the raw secret key.
const KEY_LEN: usize = 32;

/// Loads the configuration from its directory.
Expand Down Expand Up @@ -95,12 +95,13 @@ impl UserConfig {
self.dir.join("node.key")
}

/// This node's key, generated and saved durably the first time.
/// Returns this node's key, generating it and saving it durably the
/// first time.
///
/// # Errors
///
/// Fails if the key cannot be read, is malformed, or cannot be
/// generated and saved.
/// Fails if the key cannot be read, is malformed, or cannot be generated
/// and saved.
pub fn node_key(&self) -> Result<NodeKey, ConfigError> {
match self.read_key() {
Err(ConfigError::Io { source, .. }) if source.kind() == io::ErrorKind::NotFound => {
Expand All @@ -122,9 +123,9 @@ impl UserConfig {
Ok(NodeKey::from_bytes(&secret))
}

/// Generates a key and saves it: written and synced under a temporary
/// name, then linked into place, which fails if another process saved
/// one first. That one is used instead.
/// Generates a key and saves it. The key is written and synced under a
/// temporary name, then linked into place. If another process saved a
/// key first, the link fails and that key is returned instead.
fn create_key(&self) -> Result<NodeKey, ConfigError> {
let io_error = |path: &Path| {
let path = path.to_owned();
Expand Down Expand Up @@ -213,10 +214,10 @@ pub enum ConfigError {
/// What is wrong.
source: InvalidName,
},
/// `node.key` isn't a key.
/// `node.key` doesn't hold a valid key.
#[error("{} is not a node key", .0.display())]
MalformedKey(PathBuf),
/// The operating system has no randomness to generate a key with.
/// The operating system cannot supply randomness to generate a key.
#[error("cannot generate a node key")]
Random(#[source] getrandom::Error),
}
Loading
Loading