cli: Improve `rad node` output

This commit is contained in:
cloudhead 2023-08-30 14:38:13 +02:00
parent df0b5da559
commit 0c43b05066
No known key found for this signature in database
3 changed files with 41 additions and 5 deletions

View File

@ -73,7 +73,7 @@ issue the `rad node stop` command:
```
$ rad node stop
Stopping node...
Node stopped
```
Running the command again gives us an error:

View File

@ -27,7 +27,7 @@ pub const HELP: Help = Help {
Usage
rad node status [<option>...]
rad node start [--foreground] [<option>...] [-- <node-option>...]
rad node start [--foreground] [--verbose] [<option>...] [-- <node-option>...]
rad node stop [<option>...]
rad node logs [-n <lines>]
rad node connect <nid>@<addr> [<option>...]
@ -40,6 +40,7 @@ Usage
Start options
--foreground Start the node in the foreground
--verbose, -v Verbose output
Routing options
@ -83,6 +84,7 @@ pub enum Operation {
},
Start {
foreground: bool,
verbose: bool,
options: Vec<OsString>,
},
Logs {
@ -133,6 +135,7 @@ impl Args for Options {
let mut lines: usize = 10;
let mut count: usize = usize::MAX;
let mut timeout = time::Duration::MAX;
let mut verbose = false;
while let Some(arg) = parser.next()? {
match arg {
@ -183,6 +186,9 @@ impl Args for Options {
Long("foreground") if matches!(op, Some(OperationName::Start)) => {
foreground = true;
}
Long("verbose") | Short('v') if matches!(op, Some(OperationName::Start)) => {
verbose = true;
}
Short('n') if matches!(op, Some(OperationName::Logs)) => {
lines = parser.value()?.parse()?;
}
@ -205,6 +211,7 @@ impl Args for Options {
OperationName::Logs => Operation::Logs { lines },
OperationName::Start => Operation::Start {
foreground,
verbose,
options,
},
OperationName::Status => Operation::Status,
@ -244,8 +251,9 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
Operation::Start {
foreground,
options,
verbose,
} => {
control::start(node, !foreground, options, &profile)?;
control::start(node, !foreground, verbose, options, &profile)?;
}
Operation::Status => {
control::status(&node, &profile)?;

View File

@ -14,9 +14,13 @@ use radicle::{profile, Profile};
use crate::terminal as term;
use crate::terminal::Element as _;
/// How long to wait for the node to start before returning an error.
pub const NODE_START_TIMEOUT: time::Duration = time::Duration::from_secs(6);
pub fn start(
node: Node,
daemon: bool,
verbose: bool,
mut options: Vec<OsString>,
profile: &Profile,
) -> anyhow::Result<()> {
@ -54,7 +58,30 @@ pub fn start(
.stderr(process::Stdio::null())
.spawn()?;
logs(0, Some(time::Duration::from_secs(1)), profile)?;
if verbose {
logs(0, Some(time::Duration::from_secs(1)), profile)?;
} else {
let started = time::Instant::now();
loop {
if node.is_running() {
term::success!("Node started");
term::print(term::format::dim(
"To stay in sync with the network, leave the node running in the background.",
));
term::info!(
"{} {}{}",
term::format::dim("To learn more, run"),
term::format::command("rad node --help"),
term::format::dim("."),
);
break;
} else if started.elapsed() >= NODE_START_TIMEOUT {
anyhow::bail!("node failed to start. Try running in verbose mode with `rad node start --verbose`");
}
thread::sleep(time::Duration::from_millis(60));
}
}
} else {
let mut child = process::Command::new("radicle-node")
.args(options)
@ -68,10 +95,11 @@ pub fn start(
}
pub fn stop(node: Node) -> anyhow::Result<()> {
let spinner = term::spinner("Stopping node...");
let mut spinner = term::spinner("Stopping node...");
if node.shutdown().is_err() {
spinner.error("node is not running");
} else {
spinner.message("Node stopped");
spinner.finish();
}
Ok(())