Skip to content

Commit e4b1077

Browse files
authored
Merge pull request #5 from BenSchZA/feature/root-input-run
Run input from root CLI command
2 parents adaf3e2 + 62e9410 commit e4b1077

2 files changed

Lines changed: 79 additions & 39 deletions

File tree

src/cli.yml

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
name: pier
2-
version: "0.1.0"
2+
version: "0.2.0"
33
author: Benjamin Scholtz <bscholtz.bds@gmail.com>
44
about: A simple Docker script management CLI
55
args:
6+
- INPUT:
7+
help: alias/name for script to run
8+
required: false
9+
index: 1
610
- config:
711
short: c
812
long: config
913
value_name: FILE
1014
help: sets a custom config file (default "$HOME/.pier")
1115
takes_value: true
12-
- accept:
13-
short: y
14-
long: accept
15-
help: answer yes to all questions
16+
# - accept:
17+
# short: y
18+
# long: accept
19+
# help: answer yes to all questions
1620
subcommands:
1721
- add:
1822
about: Add a script using alias
@@ -27,12 +31,22 @@ subcommands:
2731
required: true
2832
help: alias/name for script
2933
takes_value: true
30-
- tags:
31-
short: t
32-
long: tags
33-
help: tags for script
34-
takes_value: true
35-
multiple: true
34+
# - description:
35+
# short: d
36+
# long: description
37+
# help: description for script
38+
# takes_value: true
39+
# - reference:
40+
# short: r
41+
# long: reference
42+
# help: reference for script
43+
# takes_value: true
44+
# - tags:
45+
# short: t
46+
# long: tags
47+
# help: tags for script
48+
# takes_value: true
49+
# multiple: true
3650
- remove:
3751
about: Remove a script using alias
3852
args:
@@ -52,11 +66,11 @@ subcommands:
5266
long: arg
5367
help: pass argument to script
5468
takes_value: true
55-
- dir:
56-
short: d
57-
long: dir
58-
help: run script in directory
59-
takes_value: true
69+
# - dir:
70+
# short: d
71+
# long: dir
72+
# help: run script in directory
73+
# takes_value: true
6074
- list:
6175
about: List all scripts with optional filters
6276
args:
@@ -65,9 +79,9 @@ subcommands:
6579
long: alias
6680
help: alias/name for script
6781
takes_value: true
68-
- tags:
69-
short: t
70-
long: tags
71-
help: tags for script
72-
takes_value: true
73-
multiple: true
82+
# - tags:
83+
# short: t
84+
# long: tags
85+
# help: tags for script
86+
# takes_value: true
87+
# multiple: true

src/main.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ fn main() {
3535

3636
let config = &mut load_config(&matches);
3737

38+
match matches.value_of("INPUT") {
39+
Some(alias) => {
40+
// let arg = match sub_matches.value_of("arg") {
41+
// Some(arg) => String::from(arg),
42+
// None => String::from("")
43+
// };
44+
let arg = String::from("");
45+
46+
match fetch_script(alias, config) {
47+
Some(script) => run_command(alias, &script.command, &arg),
48+
None => println!("Invalid alias, would you like to create a new script?"),
49+
}
50+
},
51+
None => handle_subcommands(&matches, config).expect("No input or subcommands"),
52+
}
53+
}
54+
55+
fn handle_subcommands(matches: &clap::ArgMatches, config: & mut Config) -> Result<(),Error> {
3856
match matches.subcommand() {
3957
("add", Some(sub_matches)) => {
4058
let command = sub_matches.value_of("INPUT").unwrap();
@@ -101,23 +119,9 @@ fn main() {
101119
None => String::from("")
102120
};
103121

104-
println!("Starting script \"{}\"", alias);
105-
println!("-------------------------");
106-
107-
match &config.scripts {
108-
Some(_scripts) => {
109-
match &config.scripts.as_mut().unwrap().get(&alias.to_string()) {
110-
Some(script) => {
111-
let output = cmd!(&format!("{} {}", &script.command, &arg)).stdout_utf8().unwrap();
112-
println!("{}", output);
113-
114-
println!("-------------------------");
115-
println!("Script complete");
116-
},
117-
None => println!("Invalid alias, would you like to create a new script?")
118-
}
119-
},
120-
None => {}
122+
match fetch_script(alias, config) {
123+
Some(script) => run_command(alias, &script.command, &arg),
124+
None => println!("Invalid alias, would you like to create a new script?"),
121125
}
122126
},
123127
("list", Some(_sub_matches)) => {
@@ -138,6 +142,28 @@ fn main() {
138142
("", None) => println!("No subcommand was used"),
139143
_ => unreachable!(),
140144
}
145+
146+
Ok(())
147+
}
148+
149+
fn fetch_script<'a>(alias: &str, config: &'a Config) -> Option<&'a Script> {
150+
return match &config.scripts {
151+
Some(scripts) => {
152+
scripts.get(&alias.to_string())
153+
},
154+
None => None
155+
}
156+
}
157+
158+
fn run_command(alias: &str, command: &str, arg: &str) {
159+
println!("Starting script \"{}\"", alias);
160+
println!("-------------------------");
161+
162+
let output = cmd!(&format!("{} {}", command, arg)).stdout_utf8().unwrap();
163+
println!("{}", output);
164+
165+
println!("-------------------------");
166+
println!("Script complete");
141167
}
142168

143169
fn write_config(matches: &clap::ArgMatches, config: &Config) -> Result<(),Error> {

0 commit comments

Comments
 (0)