From 1f46ea2909f9e1e9af85c4337141236eb8496fd4 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Fri, 24 Apr 2020 20:55:55 +0300 Subject: [PATCH] feat(cli): Split CLI into function for subcommands and validate args --- src/main.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index e019c16e..4971a889 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,15 @@ use structopt::StructOpt; +use structopt::clap::AppSettings; +use std::path; +use std::vec; +use std::fs; +use std::io; /// The command line interface to the CaSILE toolkit, a book publishing /// workflow employing SILE and other wizardry #[derive(StructOpt)] #[structopt(version = env!("VERGEN_SEMVER"))] +#[structopt(setting = AppSettings::InferSubcommands)] struct Cli { /// Activate debug mode @@ -15,7 +21,7 @@ struct Cli { verbose: bool, #[structopt(subcommand)] - command: Subcommand, + subcommand: Subcommand, } @@ -24,15 +30,19 @@ enum Subcommand { /// Executes a make target Make { + /// Target as defined in CaSILE makefile - target: String + target: Vec, + }, /// Configure a book repository Setup { - #[structopt(default_value = "./")] + /// Path to project repository - path: String + #[structopt(parse(from_os_str), default_value = "./")] + path: path::PathBuf, + }, /// Pass through other commands to shell @@ -41,9 +51,55 @@ enum Subcommand { } -fn main() { +fn main () -> io::Result<()> { let args = Cli::from_args(); - println!("Insert magic potion! (Unimplemented)"); + if args.debug { + println!("User requested debug mode") + } + + if args.verbose { + println!("User requested verbose output") + } + + return match args.subcommand { + Subcommand::Make{ target } => make(target), + Subcommand::Setup{ path } => setup(path), + _a => shell(), + } + +} + +fn make (_target: vec::Vec) -> io::Result<()> { + + println!("Make make make sense or I’ll make you make makefiles."); + + Ok(()) +} + +fn setup (path: path::PathBuf) -> io::Result<()> { + + let pathmeta = fs::metadata(path); + + match pathmeta { + Ok(file) => { + if file.is_dir() { + println!("Run setup, “They said you were this great colossus!”") + } else { + println!("Not a dir, Frank!") + } + } + Err(error) => { return Err(error.into()) }, + }; + + Ok(()) + +} + +fn shell () -> io::Result<()> { + + println!("Ship all this off to the shell, maybe they can handle it."); + + Ok(()) }