cli: Add `--json` flag to `rad web`

This allows third party integrations to read the obtained sessionId and
the signed payload without having to implement it on their own.

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez 2023-05-30 10:03:03 +02:00 committed by Alexis Sellier
parent 22c7945e7b
commit c2dd778183
No known key found for this signature in database
1 changed files with 27 additions and 10 deletions

View File

@ -22,6 +22,7 @@ Options
--backend, -b httpd to bind to --backend, -b httpd to bind to
--frontend, -f Web interface to bind to --frontend, -f Web interface to bind to
--verbose, -v Verbose output --verbose, -v Verbose output
--json Output as json
--help Print help --help Print help
"#, "#,
}; };
@ -37,6 +38,7 @@ pub struct SessionInfo {
pub struct Options { pub struct Options {
pub backend: String, pub backend: String,
pub frontend: String, pub frontend: String,
pub json: bool,
pub verbose: bool, pub verbose: bool,
} }
@ -47,6 +49,7 @@ impl Args for Options {
let mut parser = lexopt::Parser::from_args(args); let mut parser = lexopt::Parser::from_args(args);
let mut backend = None; let mut backend = None;
let mut frontend = None; let mut frontend = None;
let mut json = false;
let mut verbose = false; let mut verbose = false;
while let Some(arg) = parser.next()? { while let Some(arg) = parser.next()? {
@ -58,6 +61,7 @@ impl Args for Options {
Long("frontend") | Short('f') => { Long("frontend") | Short('f') => {
frontend = Some(parser.value()?.to_string_lossy().to_string()) frontend = Some(parser.value()?.to_string_lossy().to_string())
} }
Long("json") => json = true,
Long("help") => { Long("help") => {
return Err(Error::Help.into()); return Err(Error::Help.into());
} }
@ -69,6 +73,7 @@ impl Args for Options {
Ok(( Ok((
Options { Options {
json,
verbose, verbose,
backend: backend.unwrap_or(String::from("http://0.0.0.0:8080")), backend: backend.unwrap_or(String::from("http://0.0.0.0:8080")),
frontend: frontend.unwrap_or(String::from("https://app.radicle.xyz")), 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 profile = ctx.profile()?;
let signer = profile.signer()?; let signer = profile.signer()?;
let signature = sign(signer, &session)?; let signature = sign(signer, &session)?;
term::blank();
term::info!("Open the following link to authenticate:"); if options.json {
term::info!( println!(
" 👉 {}/session/{}?pk={}&sig={}", "{}",
options.frontend, serde_json::json!({
session.session_id, "sessionId": session.session_id,
session.public_key, "publicKey": session.public_key,
signature, "signature": signature,
); })
term::blank(); )
} 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(()) Ok(())
} }