use domo::public::account::Account;
use domo::public::Client;
use std::collections::HashMap;
use structopt::StructOpt;
use super::util;
#[derive(StructOpt, Debug)]
pub enum AccountCommand {
#[structopt(name = "list")]
List {
#[structopt(short = "l", long = "limit")]
limit: Option<u32>,
#[structopt(short = "o", long = "offset")]
offset: Option<u32>,
},
#[structopt(name = "create")]
Create {
account_type: String,
},
#[structopt(name = "retrieve")]
Retrieve { id: String },
#[structopt(name = "update")]
Update { id: String },
#[structopt(name = "delete")]
Delete { id: String },
#[structopt(name = "share")]
Share { account_id: String, user_id: u64 },
#[structopt(name = "list-types")]
ListTypes {
#[structopt(short = "l", long = "limit")]
limit: Option<u32>,
#[structopt(short = "o", long = "offset")]
offset: Option<u32>,
},
#[structopt(name = "retrieve-type")]
RetrieveType { id: String },
}
pub async fn execute(dc: Client, editor: &str, template: Option<String>, command: AccountCommand) {
match command {
AccountCommand::List { limit, offset } => {
let r = dc.get_accounts(limit, offset).await.unwrap();
util::vec_obj_template_output(r, template);
}
AccountCommand::Create { account_type } => {
let mut r = Account::template();
let mut at = dc.get_account_type(&account_type).await.unwrap();
if let Some(ref hm) = at.templates {
if hm.contains_key("default") {
let mut properties: HashMap<String, String> = HashMap::new();
for p in hm.get("default").unwrap().properties.as_ref().unwrap() {
properties.insert(
String::from(p.name.as_ref().unwrap()),
format!("TODO: {}", p.prompt.as_ref().unwrap()),
);
}
at.properties = Some(properties);
}
}
r.account_type = Some(at);
let r = util::edit_obj(editor, r, "").unwrap();
let r = dc.post_account(r).await.unwrap();
util::obj_template_output(r, template);
}
AccountCommand::Retrieve { id } => {
let r = dc.get_account(&id).await.unwrap();
util::obj_template_output(r, template);
}
AccountCommand::Update { id } => {
let r = dc.get_account(&id).await.unwrap();
let r = util::edit_obj(editor, r, "").unwrap();
dc.patch_account(&id, r).await.unwrap();
}
AccountCommand::Delete { id } => {
dc.delete_account(&id).await.unwrap();
}
AccountCommand::Share {
account_id,
user_id,
} => {
dc.post_account_share(&account_id, user_id).await.unwrap();
}
AccountCommand::ListTypes { limit, offset } => {
let r = dc.get_account_types(limit, offset).await.unwrap();
util::vec_obj_template_output(r, template);
}
AccountCommand::RetrieveType { id } => {
let r = dc.get_account_type(&id).await.unwrap();
util::obj_template_output(r, template);
}
}
}