use super::util;
use domo::public::stream::Stream;
use domo::public::Client;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
pub enum StreamCommand {
#[structopt(name = "list")]
List {
#[structopt(short = "l", long = "limit")]
limit: Option<u32>,
#[structopt(short = "o", long = "offset")]
offset: Option<u32>,
},
#[structopt(name = "list-all")]
ListAll {},
#[structopt(name = "create")]
Create {},
#[structopt(name = "retrieve")]
Retrieve { stream_id: String },
#[structopt(name = "update")]
Update { stream_id: String },
#[structopt(name = "delete")]
Delete { stream_id: String },
#[structopt(name = "search-owners")]
SearchOwners { owner_id: String },
#[structopt(name = "search-ids")]
SearchDatasetId { dataset_id: String },
#[structopt(name = "list-executions")]
ListExecutions {
#[structopt(short = "l", long = "limit")]
limit: Option<u32>,
#[structopt(short = "o", long = "offset")]
offset: Option<u32>,
stream_id: String,
},
#[structopt(name = "create-execution")]
CreateExecution { stream_id: String },
#[structopt(name = "retrieve-execution")]
RetrieveExecution {
stream_id: String,
execution_id: String,
},
#[structopt(name = "upload-part")]
UploadPart {
#[structopt(parse(from_os_str))]
file: PathBuf,
stream_id: String,
execution_id: String,
part_id: String,
},
#[structopt(name = "commit-execution")]
CommitExecution {
stream_id: String,
execution_id: String,
},
#[structopt(name = "abort-execution")]
AbortExecution {
stream_id: String,
execution_id: String,
},
}
pub async fn execute(dc: Client, editor: &str, template: Option<String>, command: StreamCommand) {
match command {
StreamCommand::List { limit, offset } => {
let r = dc.get_streams(limit, offset).await.unwrap();
util::vec_obj_template_output(r, template);
}
StreamCommand::ListAll {} => {
let mut offset = 0_u32;
let mut r: Vec<Stream> = Vec::new();
loop {
let mut ret = dc.get_streams(Some(50), Some(offset)).await.unwrap();
let mut b = false;
if ret.len() < 50 {
b = true;
}
r.append(&mut ret);
offset += 50;
if b {
break;
}
}
util::vec_obj_template_output(r, template);
}
StreamCommand::SearchOwners { owner_id } => {
let r = dc
.get_stream_search_dataset_owner_id(&owner_id)
.await
.unwrap();
util::vec_obj_template_output(r, template);
}
StreamCommand::SearchDatasetId { dataset_id } => {
let r = dc.get_stream_search_dataset_id(&dataset_id).await.unwrap();
util::vec_obj_template_output(r, template);
}
StreamCommand::Create {} => {
let r = Stream::template();
let r = util::edit_obj(editor, r, "").unwrap();
let r = dc.post_stream(r).await.unwrap();
util::obj_template_output(r, template);
}
StreamCommand::Retrieve { stream_id } => {
let r = dc.get_stream(&stream_id).await.unwrap();
util::obj_template_output(r, template);
}
StreamCommand::Update { stream_id } => {
let r = dc.get_stream(&stream_id).await.unwrap();
let r = util::edit_obj(editor, r, "").unwrap();
let r = dc.patch_stream(&stream_id, r).await.unwrap();
util::obj_template_output(r, template);
}
StreamCommand::Delete { stream_id } => {
dc.delete_stream(&stream_id).await.unwrap();
}
StreamCommand::ListExecutions {
stream_id,
limit,
offset,
} => {
let r = dc
.get_stream_executions(&stream_id, limit, offset)
.await
.unwrap();
util::vec_obj_template_output(r, template);
}
StreamCommand::CreateExecution { stream_id } => {
let r = dc.post_stream_execution(&stream_id).await.unwrap();
util::obj_template_output(r, template);
}
StreamCommand::RetrieveExecution {
stream_id,
execution_id,
} => {
let r = dc
.get_stream_execution(&stream_id, &execution_id)
.await
.unwrap();
util::obj_template_output(r, template);
}
StreamCommand::UploadPart {
file,
stream_id,
execution_id,
part_id,
} => {
dc.put_stream_execution_part(&stream_id, &execution_id, &part_id, file)
.await
.unwrap();
}
StreamCommand::CommitExecution {
stream_id,
execution_id,
} => {
let r = dc
.put_stream_execution_commit(&stream_id, &execution_id)
.await
.unwrap();
util::obj_template_output(r, template);
}
StreamCommand::AbortExecution {
stream_id,
execution_id,
} => {
dc.put_stream_execution_abort(&stream_id, &execution_id)
.await
.unwrap();
}
}
}