use super::util;
use domo::public::dataset::{DataSet, Policy};
use domo::public::Client;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
pub enum DataSetCommand {
#[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 { id: String },
#[structopt(name = "update")]
Update { id: String },
#[structopt(name = "delete")]
Delete { id: String },
#[structopt(name = "import")]
Import {
#[structopt(parse(from_os_str))]
file: PathBuf,
id: String,
},
#[structopt(name = "export")]
Export { id: String },
#[structopt(name = "query")]
Query { id: String, sql: String },
ListPolicies { id: String },
CreatePolicy { id: String },
RetrievePolicy { id: String, policy_id: u32 },
UpdatePolicy { id: String, policy_id: u32 },
DeletePolicy { id: String, policy_id: u32 },
}
pub async fn execute(dc: Client, editor: &str, template: Option<String>, command: DataSetCommand) {
match command {
DataSetCommand::List { limit, offset } => {
let r = dc.get_datasets(limit, offset).await.unwrap();
util::vec_obj_template_output(r, template);
}
DataSetCommand::ListAll {} => {
let mut offset = 0_u32;
let mut r: Vec<DataSet> = Vec::new();
loop {
let mut ret = dc.get_datasets(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);
}
DataSetCommand::Create {} => {
let r = DataSet::template();
let r = util::edit_obj(editor, r, "").unwrap();
let r = dc.post_dataset(r).await.unwrap();
util::obj_template_output(r, template);
}
DataSetCommand::Retrieve { id } => {
let r = dc.get_dataset(&id).await.unwrap();
util::obj_template_output(r, template);
}
DataSetCommand::Update { id } => {
let r = dc.get_dataset(&id).await.unwrap();
let r = util::edit_obj(editor, r, "").unwrap();
let r = dc.put_dataset(&id, r).await.unwrap();
util::obj_template_output(r, template);
}
DataSetCommand::Delete { id } => {
dc.delete_dataset(&id).await.unwrap();
}
DataSetCommand::Import { file, id } => {
dc.put_dataset_data(&id, file).await.unwrap();
}
DataSetCommand::Export { id } => {
let r = dc.get_dataset_data(&id).await.unwrap();
util::csv_template_output(r, template);
}
DataSetCommand::Query { id, sql } => {
let r = dc.post_dataset_query(&id, &sql).await.unwrap();
util::query_template_output(r, template);
}
DataSetCommand::ListPolicies { id } => {
let r = dc.get_dataset_policies(&id).await.unwrap();
util::vec_obj_template_output(r, template);
}
DataSetCommand::CreatePolicy { id } => {
let r = Policy::template();
let r = util::edit_obj(editor, r, "").unwrap();
let r = dc.post_dataset_policy(&id, r).await.unwrap();
util::obj_template_output(r, template);
}
DataSetCommand::RetrievePolicy { id, policy_id } => {
let r = dc.get_dataset_policy(&id, policy_id).await.unwrap();
util::obj_template_output(r, template);
}
DataSetCommand::UpdatePolicy { id, policy_id } => {
let r = dc.get_dataset_policy(&id, policy_id).await.unwrap();
let r = util::edit_obj(editor, r, "").unwrap();
let r = dc.put_dataset_policy(&id, policy_id, r).await.unwrap();
util::obj_template_output(r, template);
}
DataSetCommand::DeletePolicy { id, policy_id } => {
dc.delete_dataset_policy(&id, policy_id).await.unwrap();
}
}
}