custom_cli

This commit is contained in:
Aiden McClelland
2021-08-31 22:53:48 -06:00
committed by Aiden McClelland
parent 9fb02c6a1e
commit 064c905bd3
6 changed files with 44 additions and 37 deletions

View File

@@ -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<String>, metadata: Value) -> Result<(), Error> {
// todo!()
// }
async fn cli_login(
ctx: CliContext,
password: Option<String>,
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)
)]

View File

@@ -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(())
}

View File

@@ -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(())
}

View File

@@ -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")]

View File

@@ -14,17 +14,3 @@ impl From<RpcContext> for () {
()
}
}
// TODO: these shouldn't be necessary
impl From<CliContext> for RpcContext {
fn from(_: CliContext) -> Self {
panic!("RPC Context used in CLI Handler")
}
}
impl From<RpcContext> for CliContext {
fn from(_: RpcContext) -> Self {
panic!("CLI Context used in RPC Handler")
}
}

View File

@@ -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;