Skip to content

Commit

Permalink
Merge pull request #5 from BenSchZA/feature/root-input-run
Browse files Browse the repository at this point in the history
Run input from root CLI command
  • Loading branch information
BenSchZA authored May 15, 2019
2 parents adaf3e2 + 62e9410 commit e4b1077
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 39 deletions.
58 changes: 36 additions & 22 deletions src/cli.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
name: pier
version: "0.1.0"
version: "0.2.0"
author: Benjamin Scholtz <[email protected]>
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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
60 changes: 43 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)) => {
Expand All @@ -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> {
Expand Down

0 comments on commit e4b1077

Please sign in to comment.