From 33547c9c3c13db20ee72c1446c878b3df651179c Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Thu, 20 Apr 2023 21:06:08 +0200 Subject: [PATCH] cli: implement the rad remote add command implementing the `rad remote add` command in order to be able to add rad remotes from the command line. Signed-off-by: Vincenzo Palazzo --- radicle-cli/src/commands/remote/add.rs | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 radicle-cli/src/commands/remote/add.rs diff --git a/radicle-cli/src/commands/remote/add.rs b/radicle-cli/src/commands/remote/add.rs new file mode 100644 index 00000000..350e0807 --- /dev/null +++ b/radicle-cli/src/commands/remote/add.rs @@ -0,0 +1,38 @@ +use radicle::node::tracking::store::Config; +use radicle::{git::Url, node::TRACKING_DB_FILE, prelude::Id, Profile}; +use radicle_crypto::PublicKey; + +use crate::git::add_remote; +use crate::{git, terminal as term}; + +pub fn run( + repository: &git::Repository, + profile: &Profile, + pubkey: &PublicKey, + name: Option, + id: Id, +) -> anyhow::Result<()> { + let name = match name { + Some(name) => name, + _ => get_remote_name(profile, pubkey)? + .ok_or(anyhow::anyhow!("a `name` needs to be specified"))?, + }; + if git::is_remote(repository, &name)? { + anyhow::bail!("remote `{name}` already exists"); + } + + let url = Url::from(id).with_namespace(*pubkey); + let remote = add_remote(repository, &name, &url)?; + term::success!( + "Remote {} added with {url}", + remote.name, + ); + Ok(()) +} + +/// Get the `git remote` name from the command `Options` and `pubkey`. +fn get_remote_name(profile: &Profile, pubkey: &PublicKey) -> anyhow::Result> { + let path = profile.home.node().join(TRACKING_DB_FILE); + let storage = Config::reader(path)?; + Ok(storage.node_policy(pubkey)?.and_then(|node| node.alias)) +}