pub struct Cli { client: sdk::Client, } impl Cli { pub fn new(client: sdk::Client) -> Self { Self { client } } pub fn cli_enrol() -> clap::Command { clap::Command::new("") .arg( clap::Arg::new("authorization") .long("authorization") .required(true) .value_parser(clap::value_parser!(String)) .help("Authorization header (bearer token)"), ) .arg( clap::Arg::new("host") .long("host") .required(true) .value_parser(clap::value_parser!(String)), ) .arg( clap::Arg::new("key") .long("key") .required(true) .value_parser(clap::value_parser!(String)), ) } pub async fn execute_enrol(&self, matches: &clap::ArgMatches) { let mut request = self.client.enrol(); if let Some(value) = matches.get_one::("authorization") { request = request.authorization(value.clone()); } let request = request.body({ let mut body = types::EnrolBody::builder(); if let Some(value) = matches.get_one::("host") { body = body.host(value.clone()); } if let Some(value) = matches.get_one::("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") .long("authorization") .required(true) .value_parser(clap::value_parser!(String)) .help("Authorization header (bearer token)"), ) } pub async fn execute_global_jobs(&self, matches: &clap::ArgMatches) { let mut request = self.client.global_jobs(); if let Some(value) = matches.get_one::("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") .long("authorization") .required(true) .value_parser(clap::value_parser!(String)) .help("Authorization header (bearer token)"), ) } pub async fn execute_ping(&self, matches: &clap::ArgMatches) { let mut request = self.client.ping(); if let Some(value) = matches.get_one::("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( clap::Arg::new("authorization") .long("authorization") .required(true) .value_parser(clap::value_parser!(String)) .help("Authorization header (bearer token)"), ) .arg( clap::Arg::new("duration-millis") .long("duration-millis") .required(true) .value_parser(clap::value_parser!(i32)), ) .arg( clap::Arg::new("end-time") .long("end-time") .required(true) .value_parser(clap::value_parser!(chrono::DateTime)), ) .arg( clap::Arg::new("exit-status") .long("exit-status") .required(true) .value_parser(clap::value_parser!(i32)), ) } pub async fn execute_report_finish(&self, matches: &clap::ArgMatches) { let mut request = self.client.report_finish(); if let Some(value) = matches.get_one::("authorization") { request = request.authorization(value.clone()); } let request = request.body({ let mut body = types::ReportFinishBody::builder(); if let Some(value) = matches.get_one::("duration-millis") { body = body.duration_millis(value.clone()); } if let Some(value) = matches.get_one::>("end-time") { body = body.end_time(value.clone()); } if let Some(value) = matches.get_one::("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") .long("authorization") .required(true) .value_parser(clap::value_parser!(String)) .help("Authorization header (bearer token)"), ) } pub async fn execute_report_output(&self, matches: &clap::ArgMatches) { let mut request = self.client.report_output(); if let Some(value) = matches.get_one::("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( clap::Arg::new("authorization") .long("authorization") .required(true) .value_parser(clap::value_parser!(String)) .help("Authorization header (bearer token)"), ) .arg( clap::Arg::new("script") .long("script") .required(true) .value_parser(clap::value_parser!(String)), ) .arg( clap::Arg::new("start-time") .long("start-time") .required(true) .value_parser(clap::value_parser!(chrono::DateTime)), ) } pub async fn execute_report_start(&self, matches: &clap::ArgMatches) { let mut request = self.client.report_start(); if let Some(value) = matches.get_one::("authorization") { request = request.authorization(value.clone()); } let request = request.body({ let mut body = types::ReportStartBody::builder(); if let Some(value) = matches.get_one::("script") { body = body.script(value.clone()); } if let Some(value) = matches.get_one::>("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(), } } pub async fn execute(&self, cmd: CliCommand, matches: &clap::ArgMatches) { let _ = match cmd { CliCommand::Enrol => { self.execute_enrol(matches).await; } CliCommand::GlobalJobs => { self.execute_global_jobs(matches).await; } CliCommand::Ping => { self.execute_ping(matches).await; } CliCommand::ReportFinish => { self.execute_report_finish(matches).await; } CliCommand::ReportOutput => { self.execute_report_output(matches).await; } CliCommand::ReportStart => { self.execute_report_start(matches).await; } }; } } pub trait CliOverride { fn cli_enrol(cmd: clap::Command) -> clap::Command { cmd } fn execute_enrol( &self, matches: &clap::ArgMatches, request: &mut (), body: &mut (), ) -> 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 (), ) -> Result<(), String> { Ok(()) } fn cli_ping(cmd: clap::Command) -> clap::Command { cmd } fn execute_ping( &self, matches: &clap::ArgMatches, request: &mut (), body: &mut (), ) -> 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 (), ) -> 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 (), ) -> 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 (), ) -> Result<(), String> { Ok(()) } } #[derive(Copy, Clone, Debug)] pub enum CliCommand { Enrol, GlobalJobs, Ping, ReportFinish, ReportOutput, ReportStart, } impl CliCommand { pub fn iter() -> impl Iterator { vec![ CliCommand::Enrol, CliCommand::GlobalJobs, CliCommand::Ping, CliCommand::ReportFinish, CliCommand::ReportOutput, CliCommand::ReportStart, ] .into_iter() } }