Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Add `--json` flag to `rad web`
Sebastian Martinez committed 2 years ago
commit c2dd778183717d4b7e009ea4210996168df4fec2
parent 22c7945e7be7de5f5dde45fc7a66bc49fd076f18
1 file changed +27 -10
modified radicle-cli/src/commands/web.rs
@@ -22,6 +22,7 @@ Options
    --backend, -b          httpd to bind to
    --frontend, -f         Web interface to bind to
    --verbose, -v          Verbose output
+
    --json                 Output as json
    --help                 Print help
"#,
};
@@ -37,6 +38,7 @@ pub struct SessionInfo {
pub struct Options {
    pub backend: String,
    pub frontend: String,
+
    pub json: bool,
    pub verbose: bool,
}

@@ -47,6 +49,7 @@ impl Args for Options {
        let mut parser = lexopt::Parser::from_args(args);
        let mut backend = None;
        let mut frontend = None;
+
        let mut json = false;
        let mut verbose = false;

        while let Some(arg) = parser.next()? {
@@ -58,6 +61,7 @@ impl Args for Options {
                Long("frontend") | Short('f') => {
                    frontend = Some(parser.value()?.to_string_lossy().to_string())
                }
+
                Long("json") => json = true,
                Long("help") => {
                    return Err(Error::Help.into());
                }
@@ -69,6 +73,7 @@ impl Args for Options {

        Ok((
            Options {
+
                json,
                verbose,
                backend: backend.unwrap_or(String::from("http://0.0.0.0:8080")),
                frontend: frontend.unwrap_or(String::from("https://app.radicle.xyz")),
@@ -91,16 +96,28 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let signer = profile.signer()?;
    let signature = sign(signer, &session)?;
-
    term::blank();
-
    term::info!("Open the following link to authenticate:");
-
    term::info!(
-
        "  👉 {}/session/{}?pk={}&sig={}",
-
        options.frontend,
-
        session.session_id,
-
        session.public_key,
-
        signature,
-
    );
-
    term::blank();
+

+
    if options.json {
+
        println!(
+
            "{}",
+
            serde_json::json!({
+
                "sessionId": session.session_id,
+
                "publicKey": session.public_key,
+
                "signature": signature,
+
            })
+
        )
+
    } else {
+
        term::blank();
+
        term::info!("Open the following link to authenticate:");
+
        term::info!(
+
            "  👉 {}/session/{}?pk={}&sig={}",
+
            options.frontend,
+
            session.session_id,
+
            session.public_key,
+
            signature,
+
        );
+
        term::blank();
+
    }

    Ok(())
}