Skip to content

Commit

Permalink
Merge pull request #10 from thash/desc_options
Browse files Browse the repository at this point in the history
desc command supports both `dy desc mytbl` and `dy desc -t mytbl`
  • Loading branch information
Takuya Hashimoto authored Nov 21, 2020
2 parents cd79a97 + c588a91 commit 77617bf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub enum Sub {
/// Show detailed information of a table. [API: DescribeTable]
#[structopt(aliases = &["show", "describe", "info"])]
Desc {
/// Target table name. Optionally you may specify the target table by --table (-t) option.
target_table_to_desc: Option<String>,

/// Show details of all tables in the region
#[structopt(long)]
all_tables: bool,
Expand Down Expand Up @@ -362,6 +365,9 @@ pub enum AdminSub {
/// Show detailed information of a table. [API: DescribeTable]
#[structopt(aliases = &["show", "describe", "info"])]
Desc {
/// Target table name. Optionally you may specify the target table by --table (-t) option.
target_table_to_desc: Option<String>,

/// Show details of all tables in the region
#[structopt(long)]
all_tables: bool,
Expand Down
21 changes: 14 additions & 7 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,33 @@ pub async fn list_tables(cx: app::Context) {
/// Note that `describe_table` function calls are executed in parallel (async + join_all).
pub async fn describe_all_tables(cx: app::Context) {
let table_names = list_tables_api(cx.clone()).await;
join_all(table_names.iter().map(|t| describe_table(cx.clone().with_table(t)) )).await;
join_all(table_names.into_iter().map(|t| describe_table(cx.clone(), Some(t)) )).await;
}


/// Executed when you call `$ dy desc (table)`. Retrieve TableDescription via describe_table_api function,
/// then print them in convenient way using print_table_description function (default/yaml).
pub async fn describe_table(cx: app::Context) {
pub async fn describe_table(cx: app::Context, target_table_to_desc: Option<String>) {
debug!("context: {:#?}", &cx);
let desc: TableDescription = app::describe_table_api(&cx.effective_region(), cx.effective_table_name()).await;
debug!("Retrieved table to describe is: '{}' table in '{}' region.", &desc.clone().table_name.unwrap(), &cx.effective_region().name());
debug!("positional arg table name: {:?}", &target_table_to_desc);
let new_context = if let Some(t) = target_table_to_desc {
cx.with_table(t.as_str())
} else {
cx
};

let desc: TableDescription = app::describe_table_api(&new_context.effective_region(), new_context.effective_table_name()).await;
debug!("Retrieved table to describe is: '{}' table in '{}' region.", &new_context.effective_table_name(), &new_context.effective_region().name());

// save described table info into cache for future use.
// Note that when this functiono is called from describe_all_tables, not all tables would be cached as calls are parallel.
match app::insert_to_table_cache(&cx, desc.clone()) {
match app::insert_to_table_cache(&new_context, desc.clone()) {
Ok(_) => { debug!("Described table schema was written to the cache file.") },
Err(e) => println!("Failed to write table schema to the cache with follwoing error: {:?}", e),
};

match cx.clone().output.as_deref() {
None | Some("yaml") => print_table_description(cx.effective_region(), desc),
match new_context.clone().output.as_deref() {
None | Some("yaml") => print_table_description(new_context.effective_region(), desc),
// Some("raw") => println!("{:#?}", desc),
Some(_) => { println!("ERROR: unsupported output type."); std::process::exit(1); },
}
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
if all_regions { control::list_tables_all_regions(context).await }
else { control::list_tables(context).await }
},
cmd::AdminSub::Desc { all_tables, output } => {
cmd::AdminSub::Desc { target_table_to_desc, all_tables, output } => {
context.output = output;
if all_tables { control::describe_all_tables(context).await }
else { control::describe_table(context).await }
else { control::describe_table(context, target_table_to_desc).await }
},
cmd::AdminSub::Create { target_type } => match target_type {
cmd::CreateSub::Table { new_table_name, keys } => control::create_table(context, new_table_name, keys).await,
Expand Down Expand Up @@ -105,10 +105,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
if all_regions { control::list_tables_all_regions(context).await }
else { control::list_tables(context).await }
},
cmd::Sub::Desc { all_tables, output } => {
cmd::Sub::Desc { target_table_to_desc, all_tables, output } => {
context.output = output;
if all_tables { control::describe_all_tables(context).await }
else { control::describe_table(context).await }
else { control::describe_table(context, target_table_to_desc).await }
},
cmd::Sub::Use { target_table_to_use } => app::use_table(&context, target_table_to_use).await?,
cmd::Sub::Config { grandchild } => match grandchild {
Expand Down

0 comments on commit 77617bf

Please sign in to comment.