@@ -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
143169fn write_config ( matches : & clap:: ArgMatches , config : & Config ) -> Result < ( ) , Error > {
0 commit comments