Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: implement the rad remote add command
Vincenzo Palazzo committed 3 years ago
commit 33547c9c3c13db20ee72c1446c878b3df651179c
parent 0468a1a7576f2534ad99f1b7e7dc680cd7bd63b5
1 file changed +38 -0
added 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<String>,
+
    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<Option<String>> {
+
    let path = profile.home.node().join(TRACKING_DB_FILE);
+
    let storage = Config::reader(path)?;
+
    Ok(storage.node_policy(pubkey)?.and_then(|node| node.alias))
+
}