use crate::store;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "snip", about = "Text snippets on the command line!")]
pub struct Opts {
#[structopt(short = "c", long = "command", default_value = "", index = 1)]
command: String,
#[structopt(short = "t", long = "topic", default_value = "", index = 2)]
topic: String,
#[structopt(short = "k", long = "key", default_value = "", index = 3)]
key: String,
#[structopt(short = "v", long = "value", default_value = "", index = 4)]
value: String,
}
pub fn run_command(opts: Opts) {
let mut store = store::Store::new();
match opts.command.as_str() {
"get" => {
if opts.topic != "" && opts.key != "" {
store.copy_list_entry(&opts.topic, &opts.key);
}
},
"show" | "echo" => {
if opts.topic != "" && opts.key != "" {
store.print_list_entry(&opts.topic, &opts.key);
} else if opts.topic != "" {
if opts.topic == "all" {
store.print_all();
} else {
store.print_list(&opts.topic);
}
} else {
eprintln!("Not able to get", );
}
},
"open" => {
if opts.topic != "" && opts.key != "" {
store.open_list_entry(&opts.topic, &opts.key);
}
}
"put" => {
if opts.topic != "" && opts.key != "" && opts.value != "" {
store.add_list_entry(&opts.topic, &opts.key, &opts.value);
} else if opts.topic != "" {
store.add_list(&opts.topic);
} else {
eprintln!("Not able to put", );
}
},
"del" => {
if opts.topic != "" && opts.key != "" {
store.delete_list_entry(&opts.topic, &opts.key);
} else if opts.topic != "" {
store.delete_list(&opts.topic);
} else {
eprintln!("Not able to del", );
}
},
"nuke" => {
store.nuke();
},
"export" => {
if opts.topic != "" {
store.export(&opts.topic);
} else {
eprintln!("Please provide the path for the exported data file", );
}
}
_ => println!("unrecognized command"),
}
store.write_out();
}