From e593191ae2f1261743bab2c548792d643394d3c8 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Sat, 15 Jul 2023 12:52:52 +0200 Subject: [PATCH] cli: Automatically remove control socket Instead of requiring that `--force` be used manually, we check to make sure the node is not running, and if so, pass `--force` to the node process. --- radicle-cli/src/commands/node.rs | 13 +++++-------- radicle-cli/src/commands/node/control.rs | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/radicle-cli/src/commands/node.rs b/radicle-cli/src/commands/node.rs index 497ad909..81f3c15f 100644 --- a/radicle-cli/src/commands/node.rs +++ b/radicle-cli/src/commands/node.rs @@ -186,14 +186,11 @@ impl Args for Options { pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { let profile = ctx.profile()?; + let mut node = Node::new(profile.socket()); match options.op { - Operation::Connect { addr } => { - let mut node = Node::new(profile.socket()); - control::connect(&mut node, addr.id, addr.addr)? - } + Operation::Connect { addr } => control::connect(&mut node, addr.id, addr.addr)?, Operation::Events => { - let node = Node::new(profile.socket()); events::run(node)?; } Operation::Routing { rid, nid, json } => { @@ -202,13 +199,13 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { routing::run(&store, rid, nid, json)?; } Operation::Logs { lines } => control::logs(lines, Some(time::Duration::MAX), &profile)?, - Operation::Start { daemon, options } => control::start(daemon, options, &profile)?, + Operation::Start { daemon, options } => { + control::start(node, daemon, options, &profile)?; + } Operation::Status => { - let node = Node::new(profile.socket()); control::status(&node, &profile)?; } Operation::Stop => { - let node = Node::new(profile.socket()); control::stop(node)?; } Operation::Tracking { mode } => { diff --git a/radicle-cli/src/commands/node/control.rs b/radicle-cli/src/commands/node/control.rs index 86cbf440..56df0399 100644 --- a/radicle-cli/src/commands/node/control.rs +++ b/radicle-cli/src/commands/node/control.rs @@ -14,12 +14,26 @@ use radicle::{profile, Profile}; use crate::terminal as term; use crate::terminal::Element as _; -pub fn start(daemon: bool, options: Vec, profile: &Profile) -> anyhow::Result<()> { +pub fn start( + node: Node, + daemon: bool, + mut options: Vec, + profile: &Profile, +) -> anyhow::Result<()> { + if node.is_running() { + term::success!("Node is already running"); + return Ok(()); + } // Ask passphrase here, otherwise it'll be a fatal error when running the daemon // without `RAD_PASSPHRASE`. To keep things consistent, we also use this in foreground mode. let passphrase = term::io::passphrase(profile::env::RAD_PASSPHRASE) .context(format!("`{}` must be set", profile::env::RAD_PASSPHRASE))?; + // Since we checked that the node is not running, it's safe to use `--force` + // here. + if !options.contains(&OsString::from("--force")) { + options.push(OsString::from("--force")); + } if daemon { let log = OpenOptions::new() .append(true)