Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow --version and --help to be used even when missing a maskfile #23

Merged
merged 5 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/lib.rs"

[dependencies]
clap = "2.33.0" # https://github.com/clap-rs/clap
colored = "1.8.0" # https://github.com/mackwic/colored
pulldown-cmark = { version = "0.5", default-features = false } # https://github.com/raphlinus/pulldown-cmark

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::Path;
pub fn read_maskfile(maskfile: &Path) -> Result<String, String> {
let file = File::open(maskfile);
if file.is_err() {
return Err("Expected a maskfile.md to exist in the current directory.".to_string());
return Err("failed to open maskfile.md".to_string());
}

let mut file = file.unwrap();
Expand Down Expand Up @@ -47,7 +47,7 @@ mod read_maskfile {

let err = maskfile.unwrap_err();

let expected_err = "Expected a maskfile.md to exist in the current directory.";
let expected_err = "failed to open maskfile.md";
assert_eq!(err, expected_err, "error message was wrong");
}
}
79 changes: 46 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,73 @@ use std::path::Path;
use clap::{
crate_authors, crate_description, crate_name, crate_version, App, Arg, ArgMatches, SubCommand,
};
use colored::*;

use mask::command::Command;

fn main() {
let args: Vec<String> = env::args().collect();

let maybe_maskfile = args.get(1);
let maybe_path = args.get(2);

let maskfile_path = match (maybe_maskfile, maybe_path) {
(Some(a), Some(path)) if a == "--maskfile" => Path::new(path),
_ => Path::new("./maskfile.md"),
};
let cli_app = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(custom_maskfile_path_arg());

let maskfile = mask::loader::read_maskfile(maskfile_path);
let maskfile = find_maskfile();
if maskfile.is_err() {
return eprintln!("ERROR: {}", maskfile.unwrap_err());
println!("{} no maskfile.md found", "WARNING:".yellow());
// If the maskfile can't be found, at least parse for --version or --help
cli_app.get_matches();
return;
}

let root_command = mask::parser::build_command_structure(maskfile.unwrap());

let cli_app = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!());

let matches = build_subcommands(cli_app, &root_command.subcommands).get_matches();

let chosen_cmd = find_command(&matches, &root_command.subcommands);

if chosen_cmd.is_none() {
// TODO: echo --help for root command
println!("Missing SUBCOMMAND");
return;
// Exit with an error
eprintln!("{} missing subcommand", "ERROR:".red());
std::process::exit(1);
}

match mask::executor::execute_command(chosen_cmd.unwrap()) {
Ok(status) => match status.code() {
Some(code) => std::process::exit(code),
None => return,
},
Err(err) => eprintln!("ERROR: {}", err),
Err(err) => eprintln!("{} {}", "ERROR:".red(), err),
}
}

fn find_maskfile() -> Result<String, String> {
let args: Vec<String> = env::args().collect();

let maybe_maskfile = args.get(1);
let maybe_path = args.get(2);

// Check for a custom --maskfile arg
let maskfile_path = match (maybe_maskfile, maybe_path) {
(Some(a), Some(path)) if a == "--maskfile" => Path::new(path),
_ => Path::new("./maskfile.md"),
};

let maskfile = mask::loader::read_maskfile(maskfile_path);

maskfile
}

fn custom_maskfile_path_arg<'a, 'b>() -> Arg<'a, 'b> {
// This is needed to prevent clap from complaining about the custom flag check
// within find_maskfile(). It should be removed once clap 3.x is released.
// See https://github.com/clap-rs/clap/issues/748
let custom_maskfile_path = Arg::with_name("maskfile")
.help("Path to a different maskfile you want to use")
.long("maskfile")
.takes_value(true)
.multiple(false);

custom_maskfile_path
}

fn build_subcommands<'a, 'b>(
mut cli_app: App<'a, 'b>,
subcommands: &'a Vec<Command>,
Expand Down Expand Up @@ -78,16 +100,7 @@ fn build_subcommands<'a, 'b>(
cli_app = cli_app.subcommand(subcmd);
}

// This is needed to prevent clap from complaining. It should be removed once
// clap 3.x is released. See https://github.com/clap-rs/clap/issues/748
let custom_maskfile_path = Arg::with_name("maskfile")
.help("Path to a different maskfile you want to use")
.short("m")
.long("maskfile")
.takes_value(true)
.multiple(false);

cli_app.arg(custom_maskfile_path)
cli_app
}

fn find_command<'a>(matches: &ArgMatches, subcommands: &Vec<Command>) -> Option<Command> {
Expand Down
Loading