From 2ab75ef03aa6c4501d321e45ef78249ed990ba1d Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 5 Oct 2016 16:03:11 -0700 Subject: [PATCH] Add --show command line flag --- notes | 3 ++- src/lib.rs | 16 +++++++++++++++- src/main.rs | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/notes b/notes index 3914fb9..430b5da 100644 --- a/notes +++ b/notes @@ -1,6 +1,8 @@ notes ----- +- fix --show to not print final newline, add a helper that iterates over + (item, first, last) - look through all justfiles for features of make that I use. so far: . phony . SHELL := zsh @@ -11,7 +13,6 @@ notes command line arguments: - --show recipe: print recipe information -- --list recipes if a bad recipe given execution: - indent for line continuation diff --git a/src/lib.rs b/src/lib.rs index 8f17aae..f907654 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ fn re(pattern: &str) -> Regex { Regex::new(pattern).unwrap() } -struct Recipe<'a> { +pub struct Recipe<'a> { line: usize, name: &'a str, leading_whitespace: &'a str, @@ -49,6 +49,16 @@ struct Recipe<'a> { dependencies: BTreeSet<&'a str>, } +impl<'a> Display for Recipe<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + try!(writeln!(f, "{}:", self.name)); + for command in self.commands.iter() { + try!(writeln!(f, " {}", command)); + } + Ok(()) + } +} + #[cfg(unix)] fn error_from_signal<'a>(recipe: &'a str, exit_status: process::ExitStatus) -> RunError<'a> { use std::os::unix::process::ExitStatusExt; @@ -300,6 +310,10 @@ impl<'a> Justfile<'a> { } Ok(()) } + + pub fn get(&self, name: &str) -> Option<&Recipe<'a>> { + self.recipes.get(name) + } } #[derive(Debug)] diff --git a/src/main.rs b/src/main.rs index 590fd16..443bd5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,11 @@ fn main() { .short("l") .long("list") .help("Lists available recipes")) + .arg(Arg::with_name("show") + .short("s") + .long("show") + .takes_value(true) + .help("Show information about a recipe")) .arg(Arg::with_name("recipe") .multiple(true) .help("recipe(s) to run, defaults to the first recipe in the justfile")) @@ -70,6 +75,16 @@ fn main() { std::process::exit(0); } + if let Some(name) = matches.value_of("show") { + match justfile.get(name) { + Some(recipe) => { + warn!("{}", recipe); + std::process::exit(0); + } + None => die!("justfile contains no recipe \"{}\"", name) + } + } + let names = if let Some(names) = matches.values_of("recipe") { names.collect::>() } else if let Some(name) = justfile.first() {