diff --git a/appmgr/src/auth.rs b/appmgr/src/auth.rs index f6519431c..2edc406d7 100644 --- a/appmgr/src/auth.rs +++ b/appmgr/src/auth.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use anyhow::anyhow; use basic_cookies::Cookie; use chrono::{DateTime, Utc}; @@ -40,12 +42,31 @@ fn gen_pwd() { ) } -// fn cli_login(ctx: CliContext, password: Option, metadata: Value) -> Result<(), Error> { -// todo!() -// } +async fn cli_login( + ctx: CliContext, + password: Option, + metadata: Value, +) -> Result<(), RpcError> { + let password = if let Some(password) = password { + password + } else { + rpassword::prompt_password_stdout("Password: ")? + }; + + rpc_toolkit::command_helpers::call_remote( + ctx, + "auth.login", + serde_json::json!({ "password": password, "metadata": metadata }), + PhantomData::<()>, + ) + .await? + .result?; + + Ok(()) +} #[command( - // custom_cli(cli_login), + custom_cli(cli_login(async, context(CliContext))), display(display_none), metadata(authenticated = false) )] diff --git a/appmgr/src/bin/embassy-cli.rs b/appmgr/src/bin/embassy-cli.rs index 5779c513e..7e27cf1d6 100644 --- a/appmgr/src/bin/embassy-cli.rs +++ b/appmgr/src/bin/embassy-cli.rs @@ -6,9 +6,9 @@ use rpc_toolkit::yajrc::RpcError; use serde_json::Value; fn inner_main() -> Result<(), Error> { - run_cli!( - embassy::main_api, - app => app + run_cli!({ + command: embassy::main_api, + app: app => app .name("Embassy CLI") .arg( clap::Arg::with_name("config") @@ -24,7 +24,7 @@ fn inner_main() -> Result<(), Error> { ) .arg(Arg::with_name("host").long("host").short("h").takes_value(true)) .arg(Arg::with_name("port").long("port").short("p").takes_value(true)), - matches => { + context: matches => { simple_logging::log_to_stderr(match matches.occurrences_of("verbosity") { 0 => log::LevelFilter::Off, 1 => log::LevelFilter::Error, @@ -33,10 +33,9 @@ fn inner_main() -> Result<(), Error> { 4 => log::LevelFilter::Debug, _ => log::LevelFilter::Trace, }); - EitherContext::Cli(CliContext::init(matches)?) + CliContext::init(matches)? }, - (), - |e: RpcError| { + exit: |e: RpcError| { match e.data { Some(Value::String(s)) => eprintln!("{}: {}", e.message, s), Some(Value::Object(o)) => if let Some(Value::String(s)) = o.get("details") { @@ -48,7 +47,7 @@ fn inner_main() -> Result<(), Error> { std::process::exit(e.code); } - ); + }); Ok(()) } diff --git a/appmgr/src/bin/embassy-sdk.rs b/appmgr/src/bin/embassy-sdk.rs index de668f8f3..ff748629b 100644 --- a/appmgr/src/bin/embassy-sdk.rs +++ b/appmgr/src/bin/embassy-sdk.rs @@ -1,13 +1,13 @@ -use embassy::context::{CliContext, EitherContext}; +use embassy::context::CliContext; use embassy::Error; use rpc_toolkit::run_cli; use rpc_toolkit::yajrc::RpcError; use serde_json::Value; fn inner_main() -> Result<(), Error> { - run_cli!( - embassy::portable_api, - app => app + run_cli!({ + command: embassy::portable_api, + app: app => app .name("Embassy SDK") .arg( clap::Arg::with_name("config") @@ -21,7 +21,7 @@ fn inner_main() -> Result<(), Error> { .multiple(true) .takes_value(false), ), - matches => { + context: matches => { simple_logging::log_to_stderr(match matches.occurrences_of("verbosity") { 0 => log::LevelFilter::Off, 1 => log::LevelFilter::Error, @@ -30,9 +30,9 @@ fn inner_main() -> Result<(), Error> { 4 => log::LevelFilter::Debug, _ => log::LevelFilter::Trace, }); - EitherContext::Cli(CliContext::init(matches)?) + CliContext::init(matches)? }, - |e: RpcError| { + exit: |e: RpcError| { match e.data { Some(Value::String(s)) => eprintln!("{}: {}", e.message, s), Some(Value::Object(o)) => if let Some(Value::String(s)) = o.get("details") { @@ -43,7 +43,7 @@ fn inner_main() -> Result<(), Error> { } std::process::exit(e.code); } - ); + }); Ok(()) } diff --git a/appmgr/src/config/mod.rs b/appmgr/src/config/mod.rs index 5aed869d8..1b2d4b362 100644 --- a/appmgr/src/config/mod.rs +++ b/appmgr/src/config/mod.rs @@ -175,9 +175,11 @@ pub async fn get( action.get(&id, &*version, &*volumes).await } -#[command(subcommands(self(set_impl(async)), set_dry), display(display_none))] +#[command( + subcommands(self(set_impl(async, context(RpcContext))), set_dry), + display(display_none) +)] pub fn set( - #[context] ctx: RpcContext, #[parent_data] id: PackageId, #[allow(unused_variables)] #[arg(long = "format")] diff --git a/appmgr/src/context/mod.rs b/appmgr/src/context/mod.rs index 8d7c3dd6a..d2a6ba3da 100644 --- a/appmgr/src/context/mod.rs +++ b/appmgr/src/context/mod.rs @@ -14,17 +14,3 @@ impl From for () { () } } - -// TODO: these shouldn't be necessary - -impl From for RpcContext { - fn from(_: CliContext) -> Self { - panic!("RPC Context used in CLI Handler") - } -} - -impl From for CliContext { - fn from(_: RpcContext) -> Self { - panic!("CLI Context used in RPC Handler") - } -} diff --git a/appmgr/src/lib.rs b/appmgr/src/lib.rs index 083f665d6..bd435d3f1 100644 --- a/appmgr/src/lib.rs +++ b/appmgr/src/lib.rs @@ -46,7 +46,6 @@ pub mod version; pub mod volume; pub use config::Config; -use context::CliContext; pub use error::{Error, ErrorKind, ResultExt}; use rpc_toolkit::command; use rpc_toolkit::yajrc::RpcError;