diff --git a/src/cli.yml b/src/cli.yml index e0eab72..09f0c10 100644 --- a/src/cli.yml +++ b/src/cli.yml @@ -1,18 +1,22 @@ name: pier -version: "0.1.0" +version: "0.2.0" author: Benjamin Scholtz about: A simple Docker script management CLI args: + - INPUT: + help: alias/name for script to run + required: false + index: 1 - config: short: c long: config value_name: FILE help: sets a custom config file (default "$HOME/.pier") takes_value: true - - accept: - short: y - long: accept - help: answer yes to all questions + # - accept: + # short: y + # long: accept + # help: answer yes to all questions subcommands: - add: about: Add a script using alias @@ -27,12 +31,22 @@ subcommands: required: true help: alias/name for script takes_value: true - - tags: - short: t - long: tags - help: tags for script - takes_value: true - multiple: true + # - description: + # short: d + # long: description + # help: description for script + # takes_value: true + # - reference: + # short: r + # long: reference + # help: reference for script + # takes_value: true + # - tags: + # short: t + # long: tags + # help: tags for script + # takes_value: true + # multiple: true - remove: about: Remove a script using alias args: @@ -52,11 +66,11 @@ subcommands: long: arg help: pass argument to script takes_value: true - - dir: - short: d - long: dir - help: run script in directory - takes_value: true + # - dir: + # short: d + # long: dir + # help: run script in directory + # takes_value: true - list: about: List all scripts with optional filters args: @@ -65,9 +79,9 @@ subcommands: long: alias help: alias/name for script takes_value: true - - tags: - short: t - long: tags - help: tags for script - takes_value: true - multiple: true + # - tags: + # short: t + # long: tags + # help: tags for script + # takes_value: true + # multiple: true diff --git a/src/main.rs b/src/main.rs index 82390b3..af26aff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,24 @@ fn main() { let config = &mut load_config(&matches); + match matches.value_of("INPUT") { + Some(alias) => { + // let arg = match sub_matches.value_of("arg") { + // Some(arg) => String::from(arg), + // None => String::from("") + // }; + let arg = String::from(""); + + match fetch_script(alias, config) { + Some(script) => run_command(alias, &script.command, &arg), + None => println!("Invalid alias, would you like to create a new script?"), + } + }, + None => handle_subcommands(&matches, config).expect("No input or subcommands"), + } +} + +fn handle_subcommands(matches: &clap::ArgMatches, config: & mut Config) -> Result<(),Error> { match matches.subcommand() { ("add", Some(sub_matches)) => { let command = sub_matches.value_of("INPUT").unwrap(); @@ -101,23 +119,9 @@ fn main() { None => String::from("") }; - println!("Starting script \"{}\"", alias); - println!("-------------------------"); - - match &config.scripts { - Some(_scripts) => { - match &config.scripts.as_mut().unwrap().get(&alias.to_string()) { - Some(script) => { - let output = cmd!(&format!("{} {}", &script.command, &arg)).stdout_utf8().unwrap(); - println!("{}", output); - - println!("-------------------------"); - println!("Script complete"); - }, - None => println!("Invalid alias, would you like to create a new script?") - } - }, - None => {} + match fetch_script(alias, config) { + Some(script) => run_command(alias, &script.command, &arg), + None => println!("Invalid alias, would you like to create a new script?"), } }, ("list", Some(_sub_matches)) => { @@ -138,6 +142,28 @@ fn main() { ("", None) => println!("No subcommand was used"), _ => unreachable!(), } + + Ok(()) +} + +fn fetch_script<'a>(alias: &str, config: &'a Config) -> Option<&'a Script> { + return match &config.scripts { + Some(scripts) => { + scripts.get(&alias.to_string()) + }, + None => None + } +} + +fn run_command(alias: &str, command: &str, arg: &str) { + println!("Starting script \"{}\"", alias); + println!("-------------------------"); + + let output = cmd!(&format!("{} {}", command, arg)).stdout_utf8().unwrap(); + println!("{}", output); + + println!("-------------------------"); + println!("Script complete"); } fn write_config(matches: &clap::ArgMatches, config: &Config) -> Result<(),Error> {