diff --git a/CHANGELOG.md b/CHANGELOG.md index cd659377..5b7db4f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `rad init --setup-signing` now works on bare repositories. - `git-remote-rad` now correctly reports the default branch to Git by listing the symbolic reference `HEAD`. +- `rad status` learned a new option `--only nid` for printing the Node ID. ## Fixed Bugs diff --git a/crates/radicle-cli/examples/rad-self.md b/crates/radicle-cli/examples/rad-self.md index 25cd57e3..02c4cb1b 100644 --- a/crates/radicle-cli/examples/rad-self.md +++ b/crates/radicle-cli/examples/rad-self.md @@ -26,7 +26,7 @@ did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi ``` $ rad self --nid -! Warning: The option `--nid` is deprecated, please use `rad node status` instead. +! Warning: The option `--nid` is deprecated, please use `rad node status --only nid` instead. z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi ``` diff --git a/crates/radicle-cli/src/commands/node.rs b/crates/radicle-cli/src/commands/node.rs index 209f7715..1b217f7d 100644 --- a/crates/radicle-cli/src/commands/node.rs +++ b/crates/radicle-cli/src/commands/node.rs @@ -1,7 +1,7 @@ use std::ffi::OsString; use std::path::PathBuf; use std::str::FromStr; -use std::time; +use std::{process, time}; use anyhow::anyhow; @@ -64,6 +64,11 @@ Events options --timeout How long to wait to receive an event before giving up --count, -n Exit after events +Status options + + --only nid If node is running, only print the Node ID and exit, + otherwise exit with a non-zero exit status. + General options --help Print help @@ -127,7 +132,9 @@ pub enum Operation { Logs { lines: usize, }, - Status, + Status { + only_nid: bool, + }, Inventory { nid: Option, }, @@ -171,6 +178,7 @@ impl Args for Options { let mut addresses = false; let mut path = None; let mut verbose = false; + let mut only_nid = false; while let Some(arg) = parser.next()? { match arg { @@ -207,6 +215,13 @@ impl Args for Options { let val = parser.value()?; nid = term::args::nid(&val).ok(); } + Long("only") if matches!(op, Some(OperationName::Status)) => { + if &parser.value()? == "nid" { + only_nid = true; + } else { + anyhow::bail!("unknown argument to --only"); + } + } Long("json") if matches!(op, Some(OperationName::Routing)) => json = true, Long("timeout") if op == Some(OperationName::Events) || op == Some(OperationName::Connect) => @@ -263,7 +278,7 @@ impl Args for Options { path: path.unwrap_or(PathBuf::from("radicle-node")), }, OperationName::Inventory => Operation::Inventory { nid }, - OperationName::Status => Operation::Status, + OperationName::Status => Operation::Status { only_nid }, OperationName::Debug => Operation::Debug, OperationName::Sessions => Operation::Sessions, OperationName::Stop => Operation::Stop, @@ -333,9 +348,16 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { println!("{}", term::format::tertiary(rid)); } } - Operation::Status => { + Operation::Status { only_nid: false } => { control::status(&node, &profile)?; } + Operation::Status { only_nid: true } => { + if node.is_running() { + term::print(term::format::node_id_human(&node.nid()?)); + } else { + process::exit(2); + } + } Operation::Stop => { control::stop(node, &profile); } diff --git a/crates/radicle-cli/src/commands/self.rs b/crates/radicle-cli/src/commands/self.rs index cfc62081..dbd82b06 100644 --- a/crates/radicle-cli/src/commands/self.rs +++ b/crates/radicle-cli/src/commands/self.rs @@ -101,7 +101,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { } Show::NodeId => { term::warning( - "The option `--nid` is deprecated, please use `rad node status` instead.", + "The option `--nid` is deprecated, please use `rad node status --only nid` instead.", ); term::print( Node::new(profile.socket())