1- use std:: env;
21use std:: error:: Error ;
3- use std:: path:: Path ;
2+ use std:: path:: PathBuf ;
43
5- use clap:: { crate_authors , crate_description , crate_version , App , AppSettings , Arg , SubCommand } ;
4+ use clap:: { Parser , Subcommand } ;
65
76use kickstart:: generation:: Template ;
87use kickstart:: terminal;
98use kickstart:: validate:: validate_file;
109
11- pub fn build_cli ( ) -> App < ' static , ' static > {
12- App :: new ( "kickstart" )
13- . version ( crate_version ! ( ) )
14- . author ( crate_authors ! ( ) )
15- . about ( crate_description ! ( ) )
16- . setting ( AppSettings :: SubcommandsNegateReqs )
17- . arg (
18- Arg :: with_name ( "template" )
19- . required ( true )
20- . help ( "Template to use: a local path or a HTTP url pointing to a Git repository" ) ,
21- )
22- . arg (
23- Arg :: with_name ( "output-dir" )
24- . short ( "o" )
25- . long ( "output-dir" )
26- . takes_value ( true )
27- . help ( "Where to output the project: defaults to the current directory" ) ,
28- )
29- . arg (
30- Arg :: with_name ( "sub-dir" )
31- . short ( "s" )
32- . long ( "sub-dir" )
33- . takes_value ( true )
34- . help ( "A subdirectory of the chosen template to use, to allow nested templates." ) ,
35- )
36- . arg (
37- Arg :: with_name ( "no-input" )
38- . long ( "no-input" )
39- . help ( "Do not prompt for parameters and only use the defaults from template.toml" ) ,
40- )
41- . subcommands ( vec ! [ SubCommand :: with_name( "validate" )
42- . about( "Validates that a template.toml is valid" )
43- . arg( Arg :: with_name( "path" ) . required( true ) . help( "The path to the template.toml" ) ) ] )
10+ #[ derive( Parser ) ]
11+ #[ clap( version, author, about, subcommand_negates_reqs = true ) ]
12+ pub struct Cli {
13+ /// Template to use: a local path or a HTTP url pointing to a Git repository
14+ #[ clap( required = true ) ]
15+ pub template : Option < String > ,
16+
17+ /// Where to output the project: defaults to the current directory
18+ #[ clap( short = 'o' , long, default_value = "." ) ]
19+ pub output_dir : PathBuf ,
20+
21+ /// A subdirectory of the chosen template to use, to allow nested templates.
22+ #[ clap( short = 's' , long) ]
23+ pub sub_dir : Option < String > ,
24+
25+ /// Do not prompt for parameters and only use the defaults from template.toml
26+ #[ clap( long, default_value_t = false ) ]
27+ pub no_input : bool ,
28+
29+ #[ clap( subcommand) ]
30+ pub command : Option < Command > ,
31+ }
32+
33+ #[ derive( Debug , Subcommand ) ]
34+ pub enum Command {
35+ /// Validates that a template.toml is valid
36+ Validate {
37+ /// The path to the template.toml
38+ path : PathBuf ,
39+ } ,
4440}
4541
4642fn bail ( e : & dyn Error ) -> ! {
@@ -54,44 +50,32 @@ fn bail(e: &dyn Error) -> ! {
5450}
5551
5652fn main ( ) {
57- let matches = build_cli ( ) . get_matches ( ) ;
53+ let cli = Cli :: parse ( ) ;
5854
59- match matches. subcommand ( ) {
60- ( "validate" , Some ( matches) ) => {
61- let errs = match validate_file ( matches. value_of ( "path" ) . unwrap ( ) ) {
62- Ok ( e) => e,
63- Err ( e) => bail ( & e) ,
64- } ;
55+ if let Some ( Command :: Validate { path } ) = cli. command {
56+ let errs = match validate_file ( path) {
57+ Ok ( e) => e,
58+ Err ( e) => bail ( & e) ,
59+ } ;
6560
66- if !errs. is_empty ( ) {
67- terminal:: error ( "The template.toml is invalid:\n " ) ;
68- for err in errs {
69- terminal:: error ( & format ! ( "- {}\n " , err) ) ;
70- }
71- :: std:: process:: exit ( 1 ) ;
72- } else {
73- terminal:: success ( "The template.toml file is valid!\n " ) ;
61+ if !errs. is_empty ( ) {
62+ terminal:: error ( "The template.toml is invalid:\n " ) ;
63+ for err in errs {
64+ terminal:: error ( & format ! ( "- {}\n " , err) ) ;
7465 }
66+ :: std:: process:: exit ( 1 ) ;
67+ } else {
68+ terminal:: success ( "The template.toml file is valid!\n " ) ;
7569 }
76- _ => {
77- // The actual generation call
78- let template_path = matches. value_of ( "template" ) . unwrap ( ) ;
79- let output_dir = matches
80- . value_of ( "output-dir" )
81- . map ( |p| Path :: new ( p) . to_path_buf ( ) )
82- . unwrap_or_else ( || env:: current_dir ( ) . unwrap ( ) ) ;
83- let no_input = matches. is_present ( "no-input" ) ;
84- let sub_dir = matches. value_of ( "sub-dir" ) ;
70+ } else {
71+ let template = match Template :: from_input ( & cli. template . unwrap ( ) , cli. sub_dir . as_deref ( ) ) {
72+ Ok ( t) => t,
73+ Err ( e) => bail ( & e) ,
74+ } ;
8575
86- let template = match Template :: from_input ( template_path, sub_dir) {
87- Ok ( t) => t,
88- Err ( e) => bail ( & e) ,
89- } ;
90-
91- match template. generate ( & output_dir, no_input) {
92- Ok ( _) => terminal:: success ( "\n Everything done, ready to go!\n " ) ,
93- Err ( e) => bail ( & e) ,
94- } ;
95- }
76+ match template. generate ( & cli. output_dir , cli. no_input ) {
77+ Ok ( _) => terminal:: success ( "\n Everything done, ready to go!\n " ) ,
78+ Err ( e) => bail ( & e) ,
79+ } ;
9680 }
9781}
0 commit comments