Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: add did function for args parsing
Fintan Halpenny committed 3 years ago
commit b57a4606f656a24d7682b5826016b264dd2df2cd
parent efe5ff6ddddeef266708a8a922f018ec7c48152c
1 file changed +14 -0
modified radicle-cli/src/terminal/args.rs
@@ -2,6 +2,8 @@ use std::ffi::OsString;
use std::str::FromStr;

use anyhow::anyhow;
+
use radicle::crypto;
+
use radicle::prelude::Did;

#[derive(thiserror::Error, Debug)]
pub enum Error {
@@ -71,3 +73,15 @@ pub fn finish(unparsed: Vec<OsString>) -> anyhow::Result<()> {
    }
    Ok(())
}
+

+
pub fn did(val: OsString) -> anyhow::Result<Did> {
+
    let val = val.to_string_lossy();
+
    let Ok(peer) = Did::from_str(&val) else {
+
        if crypto::PublicKey::from_str(&val).is_ok() {
+
            return Err(anyhow!("expected DID, did you mean 'did:key:{val}'?"));
+
        } else {
+
            return Err(anyhow!("invalid DID '{}', expected 'did:key'", val));
+
        }
+
    };
+
    Ok(peer)
+
}