parent
54707c1a92
commit
4b4be31e01
20 changed files with 547 additions and 199 deletions
@ -1,28 +1,123 @@ |
|||||||
use structopt::StructOpt; |
use structopt::StructOpt; |
||||||
use u_lib::{ |
use u_lib::{ |
||||||
api::ClientHandler, |
api::ClientHandler, |
||||||
|
models::JobMeta |
||||||
}; |
}; |
||||||
use std::env::args; |
use std::path::PathBuf; |
||||||
|
use uuid::Uuid; |
||||||
|
|
||||||
struct Table; |
const DELIM: &'static str = "*************\n"; |
||||||
|
|
||||||
|
#[derive(StructOpt, Debug)] |
||||||
|
struct Args { |
||||||
|
#[structopt(subcommand)] |
||||||
|
cmd: Cmd |
||||||
|
} |
||||||
|
|
||||||
|
#[derive(StructOpt, Debug)] |
||||||
|
enum Cmd { |
||||||
|
Agents(LD), |
||||||
|
Jobs(JobALD), |
||||||
|
Jobmap(JmALD) |
||||||
|
} |
||||||
|
|
||||||
|
#[derive(StructOpt, Debug)] |
||||||
|
enum JobALD { |
||||||
|
Add { |
||||||
|
#[structopt(long, parse(try_from_str = parse_uuid))] |
||||||
|
agent: Option<Uuid>, |
||||||
|
|
||||||
|
#[structopt(subcommand)] |
||||||
|
cmd: JobCmd |
||||||
|
}, |
||||||
|
#[structopt(flatten)] |
||||||
|
LD(LD) |
||||||
|
} |
||||||
|
|
||||||
|
#[derive(StructOpt, Debug)] |
||||||
|
enum JobCmd { |
||||||
|
#[structopt(external_subcommand)] |
||||||
|
Cmd(Vec<String>) |
||||||
|
} |
||||||
|
|
||||||
|
#[derive(StructOpt, Debug)] |
||||||
|
enum JmALD { |
||||||
|
Add { |
||||||
|
#[structopt(parse(try_from_str = parse_uuid))] |
||||||
|
agent_uid: Uuid, |
||||||
|
|
||||||
|
#[structopt(parse(try_from_str = parse_uuid))] |
||||||
|
job_uids: Vec<Uuid> |
||||||
|
}, |
||||||
|
List { |
||||||
|
#[structopt(parse(try_from_str = parse_uuid))] |
||||||
|
uid: Option<Uuid>, |
||||||
|
|
||||||
|
#[structopt(short, long)] |
||||||
|
results: bool |
||||||
|
}, |
||||||
|
Delete { |
||||||
|
#[structopt(parse(try_from_str = parse_uuid))] |
||||||
|
uid: Uuid |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[derive(StructOpt, Debug)] |
||||||
|
enum LD { |
||||||
|
List { |
||||||
|
#[structopt(parse(try_from_str = parse_uuid))] |
||||||
|
uid: Option<Uuid>, |
||||||
|
}, |
||||||
|
Delete { |
||||||
|
#[structopt(parse(try_from_str = parse_uuid))] |
||||||
|
uid: Uuid |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn parse_uuid(src: &str) -> Result<Uuid, String> { |
||||||
|
Uuid::parse_str(src).map_err(|e| e.to_string()) |
||||||
|
} |
||||||
|
|
||||||
#[tokio::main] |
#[tokio::main] |
||||||
async fn main() -> Result<(), &'static str> { |
async fn main() -> Result<(), &'static str> { |
||||||
let mut raw_args = args(); |
let args: Args = Args::from_args(); |
||||||
let method = match raw_args.nth(1) { |
|
||||||
Some(m) => m, |
|
||||||
None => return Err("Method required") |
|
||||||
}; |
|
||||||
let cli_handler = ClientHandler::new(None) |
let cli_handler = ClientHandler::new(None) |
||||||
.password("123qwe".to_string()); |
.password("123qwe".to_string()); |
||||||
match method.as_str() { |
match args.cmd { |
||||||
"ls" => { |
Cmd::Agents(action) => match action { |
||||||
let result = cli_handler.get_agents().await; |
LD::List {uid} => cli_handler.get_agents(uid.as_ref()) |
||||||
for cli in result.iter() { |
.await.unwrap().into_iter().for_each(|r| println!("{}{}", DELIM, r)), |
||||||
println!("{:?}", cli) |
LD::Delete {uid} => { |
||||||
|
println!("{}", cli_handler.del(Some(&uid)).await.unwrap()); |
||||||
} |
} |
||||||
}, |
}, |
||||||
_ => return Err("Unknown method") |
Cmd::Jobs(action) => match action { |
||||||
}; |
JobALD::Add {cmd: JobCmd::Cmd(cmd), agent} => { |
||||||
|
let job = JobMeta::from_shell(cmd.join(" ")); |
||||||
|
let job_uid = job.id; |
||||||
|
cli_handler.upload_jobs(&vec![job]).await.unwrap(); |
||||||
|
if agent.is_some() { |
||||||
|
cli_handler.set_jobs(&vec![job_uid], agent.as_ref()).await.unwrap() |
||||||
|
} |
||||||
|
}, |
||||||
|
JobALD::LD(LD::List {uid}) => cli_handler.get_jobs(uid.as_ref()) |
||||||
|
.await.unwrap().into_iter().for_each(|r| println!("{}{}", DELIM, r)), |
||||||
|
JobALD::LD(LD::Delete {uid}) => { |
||||||
|
println!("{}", cli_handler.del(Some(&uid)).await.unwrap()) |
||||||
|
} |
||||||
|
} |
||||||
|
Cmd::Jobmap(action) => match action { |
||||||
|
JmALD::Add {agent_uid, job_uids} => cli_handler.set_jobs(&job_uids, Some(&agent_uid)) |
||||||
|
.await.unwrap(), |
||||||
|
JmALD::List {uid, results} => if results { |
||||||
|
cli_handler.get_results(uid.as_ref()) |
||||||
|
.await.unwrap().into_iter().for_each(|r| println!("{}{}", DELIM, r)) |
||||||
|
} else { |
||||||
|
cli_handler.get_agent_jobs(uid.as_ref()) |
||||||
|
.await.unwrap().into_iter().for_each(|r| println!("{}{}", DELIM, r)) |
||||||
|
}, |
||||||
|
JmALD::Delete {uid} => println!("{}", cli_handler.del(Some(&uid)).await.unwrap()) |
||||||
|
} |
||||||
|
} |
||||||
Ok(()) |
Ok(()) |
||||||
} |
} |
||||||
|
@ -1,3 +1,16 @@ |
|||||||
use anyhow::Result as AnyResult; |
use thiserror::Error; |
||||||
|
use diesel::result::Error as DslError; |
||||||
|
|
||||||
pub type USrvResult<T> = AnyResult<T>; |
pub type USrvResult<T> = Result<T, USrvError>; |
||||||
|
|
||||||
|
#[derive(Error, Debug)] |
||||||
|
pub enum USrvError { |
||||||
|
#[error("{0} is not found")] |
||||||
|
NotFound(String), |
||||||
|
|
||||||
|
#[error("Error processing {0}")] |
||||||
|
ProcessingError(String), |
||||||
|
|
||||||
|
#[error(transparent)] |
||||||
|
DBError(#[from] DslError) |
||||||
|
} |
||||||
|
Loading…
Reference in new issue