complete CLI overrides to allow consumers to modify commands and execution (#399)
This commit is contained in:
parent
10dc4cafc6
commit
94db6ff74f
|
@ -19,7 +19,6 @@ use crate::{
|
|||
struct CliOperation {
|
||||
cli_fn: TokenStream,
|
||||
execute_fn: TokenStream,
|
||||
cli_trait: TokenStream,
|
||||
execute_trait: TokenStream,
|
||||
}
|
||||
|
||||
|
@ -85,33 +84,9 @@ impl Generator {
|
|||
.map(|method| self.cli_method(method))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let ops = methods.iter().map(
|
||||
|CliOperation {
|
||||
cli_fn,
|
||||
execute_fn,
|
||||
cli_trait: _,
|
||||
execute_trait: _,
|
||||
}| {
|
||||
quote! {
|
||||
#cli_fn
|
||||
#execute_fn
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let trait_ops = methods.iter().map(
|
||||
|CliOperation {
|
||||
cli_fn: _,
|
||||
execute_fn: _,
|
||||
cli_trait,
|
||||
execute_trait,
|
||||
}| {
|
||||
quote! {
|
||||
#cli_trait
|
||||
#execute_trait
|
||||
}
|
||||
},
|
||||
);
|
||||
let cli_ops = methods.iter().map(|op| &op.cli_fn);
|
||||
let execute_ops = methods.iter().map(|op| &op.execute_fn);
|
||||
let trait_ops = methods.iter().map(|op| &op.execute_trait);
|
||||
|
||||
let cli_fns = raw_methods
|
||||
.iter()
|
||||
|
@ -145,16 +120,15 @@ impl Generator {
|
|||
let crate_ident = format_ident!("{}", crate_name);
|
||||
|
||||
let code = quote! {
|
||||
pub struct Cli {
|
||||
pub struct Cli<T: CliOverride = ()> {
|
||||
client: #crate_ident::Client,
|
||||
over: T,
|
||||
}
|
||||
impl Cli {
|
||||
pub fn new(client: #crate_ident::Client) -> Self {
|
||||
Self { client }
|
||||
Self { client, over: () }
|
||||
}
|
||||
|
||||
#(#ops)*
|
||||
|
||||
pub fn get_command(cmd: CliCommand) -> clap::Command {
|
||||
match cmd {
|
||||
#(
|
||||
|
@ -163,26 +137,41 @@ impl Generator {
|
|||
}
|
||||
}
|
||||
|
||||
#(#cli_ops)*
|
||||
}
|
||||
|
||||
impl<T: CliOverride> Cli<T> {
|
||||
pub fn new_with_override(
|
||||
client: #crate_ident::Client,
|
||||
over: T,
|
||||
) -> Self {
|
||||
Self { client, over }
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
&self,
|
||||
cmd: CliCommand,
|
||||
matches: &clap::ArgMatches,
|
||||
) {
|
||||
let _ = match cmd {
|
||||
match cmd {
|
||||
#(
|
||||
CliCommand::#cli_variants => {
|
||||
// TODO ... do something with output
|
||||
self.#execute_fns(matches).await;
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#(#execute_ops)*
|
||||
}
|
||||
|
||||
pub trait CliOverride {
|
||||
#(#trait_ops)*
|
||||
}
|
||||
|
||||
impl CliOverride for () {}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum CliCommand {
|
||||
#(#cli_variants,)*
|
||||
|
@ -348,12 +337,6 @@ impl Generator {
|
|||
}
|
||||
};
|
||||
|
||||
let cli_trait = quote! {
|
||||
fn #fn_name(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
};
|
||||
|
||||
let op_name = format_ident!("{}", &method.operation_id);
|
||||
|
||||
let fn_name = format_ident!("execute_{}", &method.operation_id);
|
||||
|
@ -404,7 +387,6 @@ impl Generator {
|
|||
};
|
||||
|
||||
let body_type = self.type_space.get_type(type_id).unwrap();
|
||||
let body_type_ident = body_type.ident();
|
||||
|
||||
let maybe_body_args = match body_type.details() {
|
||||
typify::TypeDetails::Struct(s) => {
|
||||
|
@ -439,10 +421,10 @@ impl Generator {
|
|||
)
|
||||
{
|
||||
// clone here in case the arg type
|
||||
// doesn't impl From<&T>
|
||||
body = body.#prop_fn(
|
||||
value.clone(),
|
||||
);
|
||||
// doesn't impl TryFrom<&T>
|
||||
request = request.body_map(|body| {
|
||||
body.#prop_fn(value.clone())
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -453,13 +435,10 @@ impl Generator {
|
|||
_ => None,
|
||||
};
|
||||
|
||||
// TODO rework this.
|
||||
maybe_body_args.map(|body_args| {
|
||||
quote! {
|
||||
let request = request.body({
|
||||
let mut body = #body_type_ident::builder();
|
||||
#( #body_args )*
|
||||
body
|
||||
});
|
||||
#( #body_args )*
|
||||
}
|
||||
})
|
||||
});
|
||||
|
@ -504,6 +483,11 @@ impl Generator {
|
|||
#( #args )*
|
||||
#body_arg
|
||||
|
||||
// TODO don't want to unwrap.
|
||||
self.over
|
||||
.#fn_name(matches, &mut request)
|
||||
.unwrap();
|
||||
|
||||
let result = request.send().await;
|
||||
|
||||
match result {
|
||||
|
@ -517,12 +501,15 @@ impl Generator {
|
|||
}
|
||||
};
|
||||
|
||||
// TODO this is copy-pasted--unwisely?
|
||||
let struct_name = sanitize(&method.operation_id, Case::Pascal);
|
||||
let struct_ident = format_ident!("{}", struct_name);
|
||||
|
||||
let execute_trait = quote! {
|
||||
fn #fn_name(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut()
|
||||
request: &mut builder :: #struct_ident,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
@ -531,7 +518,6 @@ impl Generator {
|
|||
CliOperation {
|
||||
cli_fn,
|
||||
execute_fn,
|
||||
cli_trait,
|
||||
execute_trait,
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,10 +1,22 @@
|
|||
pub struct Cli {
|
||||
pub struct Cli<T: CliOverride = ()> {
|
||||
client: sdk::Client,
|
||||
over: T,
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
pub fn new(client: sdk::Client) -> Self {
|
||||
Self { client }
|
||||
Self { client, over: () }
|
||||
}
|
||||
|
||||
pub fn get_command(cmd: CliCommand) -> clap::Command {
|
||||
match cmd {
|
||||
CliCommand::Enrol => Self::cli_enrol(),
|
||||
CliCommand::GlobalJobs => Self::cli_global_jobs(),
|
||||
CliCommand::Ping => Self::cli_ping(),
|
||||
CliCommand::ReportFinish => Self::cli_report_finish(),
|
||||
CliCommand::ReportOutput => Self::cli_report_output(),
|
||||
CliCommand::ReportStart => Self::cli_report_start(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_enrol() -> clap::Command {
|
||||
|
@ -30,33 +42,6 @@ impl Cli {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn execute_enrol(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.enrol();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
let request = request.body({
|
||||
let mut body = types::EnrolBody::builder();
|
||||
if let Some(value) = matches.get_one::<String>("host") {
|
||||
body = body.host(value.clone());
|
||||
}
|
||||
if let Some(value) = matches.get_one::<String>("key") {
|
||||
body = body.key(value.clone());
|
||||
}
|
||||
body
|
||||
});
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_global_jobs() -> clap::Command {
|
||||
clap::Command::new("").arg(
|
||||
clap::Arg::new("authorization")
|
||||
|
@ -67,23 +52,6 @@ impl Cli {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn execute_global_jobs(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.global_jobs();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_ping() -> clap::Command {
|
||||
clap::Command::new("").arg(
|
||||
clap::Arg::new("authorization")
|
||||
|
@ -94,23 +62,6 @@ impl Cli {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn execute_ping(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.ping();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_report_finish() -> clap::Command {
|
||||
clap::Command::new("")
|
||||
.arg(
|
||||
|
@ -140,38 +91,6 @@ impl Cli {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn execute_report_finish(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.report_finish();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
let request = request.body({
|
||||
let mut body = types::ReportFinishBody::builder();
|
||||
if let Some(value) = matches.get_one::<i32>("duration-millis") {
|
||||
body = body.duration_millis(value.clone());
|
||||
}
|
||||
if let Some(value) =
|
||||
matches.get_one::<chrono::DateTime<chrono::offset::Utc>>("end-time")
|
||||
{
|
||||
body = body.end_time(value.clone());
|
||||
}
|
||||
if let Some(value) = matches.get_one::<i32>("exit-status") {
|
||||
body = body.exit_status(value.clone());
|
||||
}
|
||||
body
|
||||
});
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_report_output() -> clap::Command {
|
||||
clap::Command::new("").arg(
|
||||
clap::Arg::new("authorization")
|
||||
|
@ -182,27 +101,6 @@ impl Cli {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn execute_report_output(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.report_output();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
let request = request.body({
|
||||
let mut body = types::ReportOutputBody::builder();
|
||||
body
|
||||
});
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_report_start() -> clap::Command {
|
||||
clap::Command::new("")
|
||||
.arg(
|
||||
|
@ -225,49 +123,15 @@ impl Cli {
|
|||
.value_parser(clap::value_parser!(chrono::DateTime<chrono::offset::Utc>)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_report_start(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.report_start();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
let request = request.body({
|
||||
let mut body = types::ReportStartBody::builder();
|
||||
if let Some(value) = matches.get_one::<String>("script") {
|
||||
body = body.script(value.clone());
|
||||
}
|
||||
if let Some(value) =
|
||||
matches.get_one::<chrono::DateTime<chrono::offset::Utc>>("start-time")
|
||||
{
|
||||
body = body.start_time(value.clone());
|
||||
}
|
||||
body
|
||||
});
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_command(cmd: CliCommand) -> clap::Command {
|
||||
match cmd {
|
||||
CliCommand::Enrol => Self::cli_enrol(),
|
||||
CliCommand::GlobalJobs => Self::cli_global_jobs(),
|
||||
CliCommand::Ping => Self::cli_ping(),
|
||||
CliCommand::ReportFinish => Self::cli_report_finish(),
|
||||
CliCommand::ReportOutput => Self::cli_report_output(),
|
||||
CliCommand::ReportStart => Self::cli_report_start(),
|
||||
}
|
||||
impl<T: CliOverride> Cli<T> {
|
||||
pub fn new_with_override(client: sdk::Client, over: T) -> Self {
|
||||
Self { client, over }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: CliCommand, matches: &clap::ArgMatches) {
|
||||
let _ = match cmd {
|
||||
match cmd {
|
||||
CliCommand::Enrol => {
|
||||
self.execute_enrol(matches).await;
|
||||
}
|
||||
|
@ -286,90 +150,207 @@ impl Cli {
|
|||
CliCommand::ReportStart => {
|
||||
self.execute_report_start(matches).await;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_enrol(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.enrol();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<String>("host") {
|
||||
request = request.body_map(|body| body.host(value.clone()))
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<String>("key") {
|
||||
request = request.body_map(|body| body.key(value.clone()))
|
||||
}
|
||||
|
||||
self.over.execute_enrol(matches, &mut request).unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_global_jobs(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.global_jobs();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
self.over
|
||||
.execute_global_jobs(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_ping(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.ping();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
self.over.execute_ping(matches, &mut request).unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_report_finish(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.report_finish();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<i32>("duration-millis") {
|
||||
request = request.body_map(|body| body.duration_millis(value.clone()))
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<chrono::DateTime<chrono::offset::Utc>>("end-time") {
|
||||
request = request.body_map(|body| body.end_time(value.clone()))
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<i32>("exit-status") {
|
||||
request = request.body_map(|body| body.exit_status(value.clone()))
|
||||
}
|
||||
|
||||
self.over
|
||||
.execute_report_finish(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_report_output(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.report_output();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
self.over
|
||||
.execute_report_output(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_report_start(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.report_start();
|
||||
if let Some(value) = matches.get_one::<String>("authorization") {
|
||||
request = request.authorization(value.clone());
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<String>("script") {
|
||||
request = request.body_map(|body| body.script(value.clone()))
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<chrono::DateTime<chrono::offset::Utc>>("start-time")
|
||||
{
|
||||
request = request.body_map(|body| body.start_time(value.clone()))
|
||||
}
|
||||
|
||||
self.over
|
||||
.execute_report_start(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CliOverride {
|
||||
fn cli_enrol(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_enrol(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::Enrol,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_global_jobs(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_global_jobs(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::GlobalJobs,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_ping(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_ping(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::Ping,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_report_finish(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_report_finish(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::ReportFinish,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_report_output(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_report_output(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::ReportOutput,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_report_start(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_report_start(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::ReportStart,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl CliOverride for () {}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum CliCommand {
|
||||
Enrol,
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,10 +1,17 @@
|
|||
pub struct Cli {
|
||||
pub struct Cli<T: CliOverride = ()> {
|
||||
client: sdk::Client,
|
||||
over: T,
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
pub fn new(client: sdk::Client) -> Self {
|
||||
Self { client }
|
||||
Self { client, over: () }
|
||||
}
|
||||
|
||||
pub fn get_command(cmd: CliCommand) -> clap::Command {
|
||||
match cmd {
|
||||
CliCommand::KeyGet => Self::cli_key_get(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_key_get() -> clap::Command {
|
||||
|
@ -24,6 +31,20 @@ impl Cli {
|
|||
.help("A key parameter that will not be overridden by the path spec"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: CliOverride> Cli<T> {
|
||||
pub fn new_with_override(client: sdk::Client, over: T) -> Self {
|
||||
Self { client, over }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: CliCommand, matches: &clap::ArgMatches) {
|
||||
match cmd {
|
||||
CliCommand::KeyGet => {
|
||||
self.execute_key_get(matches).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_key_get(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.key_get();
|
||||
|
@ -35,6 +56,7 @@ impl Cli {
|
|||
request = request.unique_key(value.clone());
|
||||
}
|
||||
|
||||
self.over.execute_key_get(matches, &mut request).unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
|
@ -45,37 +67,20 @@ impl Cli {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_command(cmd: CliCommand) -> clap::Command {
|
||||
match cmd {
|
||||
CliCommand::KeyGet => Self::cli_key_get(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: CliCommand, matches: &clap::ArgMatches) {
|
||||
let _ = match cmd {
|
||||
CliCommand::KeyGet => {
|
||||
self.execute_key_get(matches).await;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CliOverride {
|
||||
fn cli_key_get(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_key_get(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::KeyGet,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl CliOverride for () {}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum CliCommand {
|
||||
KeyGet,
|
||||
|
|
|
@ -1,50 +1,35 @@
|
|||
pub struct Cli {
|
||||
pub struct Cli<T: CliOverride = ()> {
|
||||
client: sdk::Client,
|
||||
over: T,
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
pub fn new(client: sdk::Client) -> Self {
|
||||
Self { client }
|
||||
Self { client, over: () }
|
||||
}
|
||||
|
||||
pub fn get_command(cmd: CliCommand) -> clap::Command {
|
||||
match cmd {
|
||||
CliCommand::InstanceGet => Self::cli_instance_get(),
|
||||
CliCommand::InstanceEnsure => Self::cli_instance_ensure(),
|
||||
CliCommand::InstanceIssueCrucibleSnapshotRequest => {
|
||||
Self::cli_instance_issue_crucible_snapshot_request()
|
||||
}
|
||||
CliCommand::InstanceMigrateStatus => Self::cli_instance_migrate_status(),
|
||||
CliCommand::InstanceSerial => Self::cli_instance_serial(),
|
||||
CliCommand::InstanceStatePut => Self::cli_instance_state_put(),
|
||||
CliCommand::InstanceStateMonitor => Self::cli_instance_state_monitor(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_instance_get() -> clap::Command {
|
||||
clap::Command::new("")
|
||||
}
|
||||
|
||||
pub async fn execute_instance_get(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_get();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_instance_ensure() -> clap::Command {
|
||||
clap::Command::new("")
|
||||
}
|
||||
|
||||
pub async fn execute_instance_ensure(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_ensure();
|
||||
let request = request.body({
|
||||
let mut body = types::InstanceEnsureRequest::builder();
|
||||
body
|
||||
});
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_instance_issue_crucible_snapshot_request() -> clap::Command {
|
||||
clap::Command::new("")
|
||||
.arg(
|
||||
|
@ -62,30 +47,6 @@ impl Cli {
|
|||
.about("Issue a snapshot request to a crucible backend")
|
||||
}
|
||||
|
||||
pub async fn execute_instance_issue_crucible_snapshot_request(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
) {
|
||||
let mut request = self.client.instance_issue_crucible_snapshot_request();
|
||||
if let Some(value) = matches.get_one::<uuid::Uuid>("id") {
|
||||
request = request.id(value.clone());
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<uuid::Uuid>("snapshot-id") {
|
||||
request = request.snapshot_id(value.clone());
|
||||
}
|
||||
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_instance_migrate_status() -> clap::Command {
|
||||
clap::Command::new("").arg(
|
||||
clap::Arg::new("migration-id")
|
||||
|
@ -95,60 +56,14 @@ impl Cli {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn execute_instance_migrate_status(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_migrate_status();
|
||||
let request = request.body({
|
||||
let mut body = types::InstanceMigrateStatusRequest::builder();
|
||||
if let Some(value) = matches.get_one::<uuid::Uuid>("migration-id") {
|
||||
body = body.migration_id(value.clone());
|
||||
}
|
||||
body
|
||||
});
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_instance_serial() -> clap::Command {
|
||||
clap::Command::new("")
|
||||
}
|
||||
|
||||
pub async fn execute_instance_serial(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_serial();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
todo!()
|
||||
}
|
||||
Err(r) => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_instance_state_put() -> clap::Command {
|
||||
clap::Command::new("")
|
||||
}
|
||||
|
||||
pub async fn execute_instance_state_put(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_state_put();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cli_instance_state_monitor() -> clap::Command {
|
||||
clap::Command::new("").arg(
|
||||
clap::Arg::new("gen")
|
||||
|
@ -157,43 +72,15 @@ impl Cli {
|
|||
.value_parser(clap::value_parser!(u64)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_state_monitor(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_state_monitor();
|
||||
let request = request.body({
|
||||
let mut body = types::InstanceStateMonitorRequest::builder();
|
||||
if let Some(value) = matches.get_one::<u64>("gen") {
|
||||
body = body.gen(value.clone());
|
||||
}
|
||||
body
|
||||
});
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_command(cmd: CliCommand) -> clap::Command {
|
||||
match cmd {
|
||||
CliCommand::InstanceGet => Self::cli_instance_get(),
|
||||
CliCommand::InstanceEnsure => Self::cli_instance_ensure(),
|
||||
CliCommand::InstanceIssueCrucibleSnapshotRequest => {
|
||||
Self::cli_instance_issue_crucible_snapshot_request()
|
||||
}
|
||||
CliCommand::InstanceMigrateStatus => Self::cli_instance_migrate_status(),
|
||||
CliCommand::InstanceSerial => Self::cli_instance_serial(),
|
||||
CliCommand::InstanceStatePut => Self::cli_instance_state_put(),
|
||||
CliCommand::InstanceStateMonitor => Self::cli_instance_state_monitor(),
|
||||
}
|
||||
impl<T: CliOverride> Cli<T> {
|
||||
pub fn new_with_override(client: sdk::Client, over: T) -> Self {
|
||||
Self { client, over }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: CliCommand, matches: &clap::ArgMatches) {
|
||||
let _ = match cmd {
|
||||
match cmd {
|
||||
CliCommand::InstanceGet => {
|
||||
self.execute_instance_get(matches).await;
|
||||
}
|
||||
|
@ -216,103 +103,201 @@ impl Cli {
|
|||
CliCommand::InstanceStateMonitor => {
|
||||
self.execute_instance_state_monitor(matches).await;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_get(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_get();
|
||||
self.over
|
||||
.execute_instance_get(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_ensure(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_ensure();
|
||||
self.over
|
||||
.execute_instance_ensure(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_issue_crucible_snapshot_request(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
) {
|
||||
let mut request = self.client.instance_issue_crucible_snapshot_request();
|
||||
if let Some(value) = matches.get_one::<uuid::Uuid>("id") {
|
||||
request = request.id(value.clone());
|
||||
}
|
||||
|
||||
if let Some(value) = matches.get_one::<uuid::Uuid>("snapshot-id") {
|
||||
request = request.snapshot_id(value.clone());
|
||||
}
|
||||
|
||||
self.over
|
||||
.execute_instance_issue_crucible_snapshot_request(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_migrate_status(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_migrate_status();
|
||||
if let Some(value) = matches.get_one::<uuid::Uuid>("migration-id") {
|
||||
request = request.body_map(|body| body.migration_id(value.clone()))
|
||||
}
|
||||
|
||||
self.over
|
||||
.execute_instance_migrate_status(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_serial(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_serial();
|
||||
self.over
|
||||
.execute_instance_serial(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
todo!()
|
||||
}
|
||||
Err(r) => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_state_put(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_state_put();
|
||||
self.over
|
||||
.execute_instance_state_put(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute_instance_state_monitor(&self, matches: &clap::ArgMatches) {
|
||||
let mut request = self.client.instance_state_monitor();
|
||||
if let Some(value) = matches.get_one::<u64>("gen") {
|
||||
request = request.body_map(|body| body.gen(value.clone()))
|
||||
}
|
||||
|
||||
self.over
|
||||
.execute_instance_state_monitor(matches, &mut request)
|
||||
.unwrap();
|
||||
let result = request.send().await;
|
||||
match result {
|
||||
Ok(r) => {
|
||||
println!("success\n{:#?}", r)
|
||||
}
|
||||
Err(r) => {
|
||||
println!("error\n{:#?}", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CliOverride {
|
||||
fn cli_instance_get(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_instance_get(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::InstanceGet,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_instance_ensure(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_instance_ensure(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::InstanceEnsure,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_instance_issue_crucible_snapshot_request(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_instance_issue_crucible_snapshot_request(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::InstanceIssueCrucibleSnapshotRequest,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_instance_migrate_status(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_instance_migrate_status(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::InstanceMigrateStatus,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_instance_serial(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_instance_serial(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::InstanceSerial,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_instance_state_put(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_instance_state_put(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::InstanceStatePut,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cli_instance_state_monitor(cmd: clap::Command) -> clap::Command {
|
||||
cmd
|
||||
}
|
||||
|
||||
fn execute_instance_state_monitor(
|
||||
&self,
|
||||
matches: &clap::ArgMatches,
|
||||
request: &mut (),
|
||||
body: &mut (),
|
||||
request: &mut builder::InstanceStateMonitor,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl CliOverride for () {}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum CliCommand {
|
||||
InstanceGet,
|
||||
|
|
Loading…
Reference in New Issue