Skip to content

Commit

Permalink
feat(Exit Codes): adds feature for custom exit code on new vers
Browse files Browse the repository at this point in the history
Can now pass the `--exit-code <num>` to set a custom exit code when new
versions have been detected.

Closes #23
  • Loading branch information
kbknapp committed May 6, 2016
1 parent dc20e8c commit 61c8bb9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ pub struct Config<'tu> {
pub to_update: Option<Vec<&'tu str>>,
pub depth: i32,
pub verbose: bool,
pub exit_code: i32,
}

impl<'tu> Config<'tu> {
pub fn from_matches(m: &'tu ArgMatches) -> Self {
let depth = match m.value_of("DEPTH") {
let depth = match m.value_of("depth") {
Some(d_str) => {
match d_str.parse::<u8>() {
Ok(num) => num as i32,
Expand All @@ -31,9 +32,13 @@ impl<'tu> Config<'tu> {
};

Config {
to_update: m.values_of("PKG").map(|v| v.collect()),
to_update: m.values_of("package").map(|v| v.collect()),
depth: depth,
verbose: m.is_present("verbose"),
exit_code: {
debugln!("exit-code={:?}", m.value_of("exit-code"));
value_t!(m, "exit-code", i32).unwrap_or(0)
},
}
}
}
23 changes: 15 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mod fmt;
use std::io::{Write, stdout};
#[cfg(feature="debug")]
use std::env;
use std::process;

use clap::{App, AppSettings, Arg, SubCommand};
use tabwriter::TabWriter;
Expand Down Expand Up @@ -138,9 +139,11 @@ fn main() {
.about("Displays information about project dependency versions")
.args_from_usage(
"-p, --package [PKG]... 'Package to inspect for updates'
-v, --verbose 'Print verbose output'
-d, --depth [DEPTH] 'How deep in the dependency chain to search{n}\
(Defaults to all dependencies when omitted)'")
-v, --verbose 'Print verbose output'
-d, --depth [NUM] 'How deep in the dependency chain to search \
(Defaults to all dependencies when omitted)'")
.arg(Arg::from_usage("--exit-code [NUM] 'The exit code to return on new versions found'")
.default_value("0"))
// We separate -R so we can addd a conflicting argument
.arg(Arg::from_usage(
"-R, --root-deps-only 'Only check root dependencies (Equivilant to --depth=1)'")
Expand All @@ -149,13 +152,17 @@ fn main() {

if let Some(m) = m.subcommand_matches("outdated") {
let cfg = Config::from_matches(m);
if let Err(e) = execute(cfg) {
e.exit();
match execute(cfg) {
Ok(code) => {
debugln!("exit_code={}", code);
process::exit(code)
},
Err(e) => e.exit(),
}
}
}

fn execute(cfg: Config) -> CliResult<()> {
fn execute(cfg: Config) -> CliResult<i32> {
debugln!("executing; execute; cfg={:?}", cfg);

verbose!(cfg, "Parsing {}...", Format::Warning("Cargo.lock"));
Expand Down Expand Up @@ -187,11 +194,11 @@ fn execute(cfg: Config) -> CliResult<()> {
String::from_utf8(tw.unwrap())
.unwrap_or_else(|e| panic!("from_utf8 error: {}", e)))
.unwrap_or_else(|e| panic!("write! error: {}", e));
Ok(())
Ok(cfg.exit_code)
}
Ok(None) => {
println!("All dependencies are up to date, yay!");
Ok(())
Ok(0)
}
Err(e) => Err(e),
}
Expand Down

0 comments on commit 61c8bb9

Please sign in to comment.