From 0c7e73c22c2b6393f19111d8dcea79f30d19153c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jan 2017 22:53:05 +0300 Subject: [PATCH] Remove generic infrastrucure for printing json Only a couple of command really use this features, so it's clearer just to print json right on the spot rather then return it though the call stack. --- src/bin/bench.rs | 4 ++-- src/bin/build.rs | 4 ++-- src/bin/cargo.rs | 21 ++++++++--------- src/bin/check.rs | 4 ++-- src/bin/clean.rs | 4 ++-- src/bin/doc.rs | 4 ++-- src/bin/fetch.rs | 4 ++-- src/bin/generate_lockfile.rs | 4 ++-- src/bin/git_checkout.rs | 4 ++-- src/bin/help.rs | 2 +- src/bin/init.rs | 4 ++-- src/bin/install.rs | 4 ++-- src/bin/locate_project.rs | 7 ++++-- src/bin/login.rs | 4 ++-- src/bin/metadata.rs | 8 ++++--- src/bin/new.rs | 4 ++-- src/bin/owner.rs | 4 ++-- src/bin/package.rs | 4 ++-- src/bin/pkgid.rs | 4 ++-- src/bin/publish.rs | 4 ++-- src/bin/read_manifest.rs | 6 +++-- src/bin/run.rs | 4 ++-- src/bin/rustc.rs | 4 ++-- src/bin/rustdoc.rs | 4 ++-- src/bin/search.rs | 4 ++-- src/bin/test.rs | 4 ++-- src/bin/uninstall.rs | 4 ++-- src/bin/update.rs | 4 ++-- src/bin/verify_project.rs | 8 +++---- src/bin/version.rs | 4 ++-- src/bin/yank.rs | 4 ++-- src/cargo/lib.rs | 45 ++++++++++++++++-------------------- src/cargo/util/errors.rs | 2 +- tests/version.rs | 29 ----------------------- 34 files changed, 100 insertions(+), 128 deletions(-) diff --git a/src/bin/bench.rs b/src/bin/bench.rs index 5f03bf9119f..ffca42abed0 100644 --- a/src/bin/bench.rs +++ b/src/bin/bench.rs @@ -70,7 +70,7 @@ not affect how many jobs are used when running the benchmarks. Compilation can be customized with the `bench` profile in the manifest. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?; config.configure(options.flag_verbose, options.flag_quiet, @@ -105,7 +105,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; let err = ops::run_benches(&ws, &ops, &options.arg_args)?; match err { - None => Ok(None), + None => Ok(()), Some(err) => { Err(match err.exit.as_ref().and_then(|e| e.code()) { Some(i) => CliError::new(human("bench failed"), i), diff --git a/src/bin/build.rs b/src/bin/build.rs index 9b7ffc7c0bb..13639c1aa28 100644 --- a/src/bin/build.rs +++ b/src/bin/build.rs @@ -71,7 +71,7 @@ the manifest. The default profile for this command is `dev`, but passing the --release flag will use the `release` profile instead. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-build; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, @@ -110,5 +110,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; ops::compile(&ws, &opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 3fc95ffe4d8..e417cf5683c 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -89,8 +89,7 @@ fn main() { match result { Err(e) => cargo::handle_cli_error(e, &mut *config.shell()), - Ok(None) => {}, - Ok(Some(())) => unreachable!(), + Ok(()) => {}, } } @@ -139,7 +138,7 @@ each_subcommand!(declare_mod); because they are fundamental (and intertwined). Other commands can rely on this top-level information. */ -fn execute(flags: Flags, config: &Config) -> CliResult> { +fn execute(flags: Flags, config: &Config) -> CliResult { config.configure(flags.flag_verbose, flags.flag_quiet, &flags.flag_color, @@ -162,7 +161,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { } } } - return Ok(None) + return Ok(()) } if flags.flag_list { @@ -170,13 +169,13 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { for command in list_commands(config) { println!(" {}", command); }; - return Ok(None) + return Ok(()) } if let Some(ref code) = flags.flag_explain { let mut procss = config.rustc()?.process(); procss.arg("--explain").arg(code).exec().map_err(human)?; - return Ok(None) + return Ok(()) } let args = match &flags.arg_command[..] { @@ -189,7 +188,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { let r = cargo::call_main_without_stdin(execute, config, USAGE, args, false); cargo::process_executed(r, &mut config.shell()); - return Ok(None) + return Ok(()) } // For `cargo help -h` and `cargo help --help`, print out the help @@ -221,7 +220,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { }; if try_execute(&config, &args) { - return Ok(None) + return Ok(()) } let alias_list = aliased_command(&config, &args[1])?; @@ -233,7 +232,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { .map(|s| s.to_string()) .collect::>(); if try_execute(&config, &chain) { - return Ok(None) + return Ok(()) } else { chain } @@ -241,7 +240,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { None => args, }; execute_subcommand(config, &args[1], &args)?; - Ok(None) + Ok(()) } fn try_execute(config: &Config, args: &[String]) -> bool { @@ -298,7 +297,7 @@ fn find_closest(config: &Config, cmd: &str) -> Option { fn execute_subcommand(config: &Config, cmd: &str, - args: &[String]) -> CliResult<()> { + args: &[String]) -> CliResult { let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX); let path = search_directories(config) .iter() diff --git a/src/bin/check.rs b/src/bin/check.rs index d8c4a7027ef..de8a9c6040c 100644 --- a/src/bin/check.rs +++ b/src/bin/check.rs @@ -66,7 +66,7 @@ pub struct Options { flag_frozen: bool, } -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-check; args={:?}", env::args().collect::>()); @@ -100,5 +100,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { }; ops::compile(&ws, &opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/clean.rs b/src/bin/clean.rs index 482e891978c..8903f890312 100644 --- a/src/bin/clean.rs +++ b/src/bin/clean.rs @@ -42,7 +42,7 @@ given, then all packages' artifacts are removed. For more information on SPEC and its format, see the `cargo help pkgid` command. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-clean; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, @@ -59,5 +59,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { }; let ws = Workspace::new(&root, config)?; ops::clean(&ws, &opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/doc.rs b/src/bin/doc.rs index 5323b4adb0b..edbb9b2eccd 100644 --- a/src/bin/doc.rs +++ b/src/bin/doc.rs @@ -66,7 +66,7 @@ current package is documented. For more information on SPEC and its format, see the `cargo help pkgid` command. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -109,5 +109,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; ops::doc(&ws, &doc_opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/fetch.rs b/src/bin/fetch.rs index 01bca5f5154..80aba98db92 100644 --- a/src/bin/fetch.rs +++ b/src/bin/fetch.rs @@ -38,7 +38,7 @@ If the lockfile is not available, then this is the equivalent of all updated. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -47,6 +47,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?; let ws = Workspace::new(&root, config)?; ops::fetch(&ws)?; - Ok(None) + Ok(()) } diff --git a/src/bin/generate_lockfile.rs b/src/bin/generate_lockfile.rs index 70365505fe4..f545b1e26f7 100644 --- a/src/bin/generate_lockfile.rs +++ b/src/bin/generate_lockfile.rs @@ -31,7 +31,7 @@ Options: --locked Require Cargo.lock is up to date "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-generate-lockfile; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, @@ -42,5 +42,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; ops::generate_lockfile(&ws)?; - Ok(None) + Ok(()) } diff --git a/src/bin/git_checkout.rs b/src/bin/git_checkout.rs index f2e9f609159..4309f0163fe 100644 --- a/src/bin/git_checkout.rs +++ b/src/bin/git_checkout.rs @@ -29,7 +29,7 @@ Options: --locked Require Cargo.lock is up to date "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -46,5 +46,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { source.update()?; - Ok(None) + Ok(()) } diff --git a/src/bin/help.rs b/src/bin/help.rs index 8597bd59439..a068b0a2c63 100644 --- a/src/bin/help.rs +++ b/src/bin/help.rs @@ -14,7 +14,7 @@ Options: -h, --help Print this message "; -pub fn execute(_: Options, _: &Config) -> CliResult> { +pub fn execute(_: Options, _: &Config) -> CliResult { // This is a dummy command just so that `cargo help help` works. // The actual delegation of help flag to subcommands is handled by the // cargo command. diff --git a/src/bin/init.rs b/src/bin/init.rs index d585ce03dfa..336ebe387cb 100644 --- a/src/bin/init.rs +++ b/src/bin/init.rs @@ -39,7 +39,7 @@ Options: --locked Require Cargo.lock is up to date "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-init; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, @@ -63,6 +63,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { if opts_lib { "library" } else {"binary (application)"}))?; - Ok(None) + Ok(()) } diff --git a/src/bin/install.rs b/src/bin/install.rs index 93bfe631998..41781a5a51d 100644 --- a/src/bin/install.rs +++ b/src/bin/install.rs @@ -94,7 +94,7 @@ the more explicit `install --path .`. The `--list` option will list all installed packages (and their versions). "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -147,5 +147,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { } else { ops::install(root, krate, &source, vers, &compile_opts, options.flag_force)?; } - Ok(None) + Ok(()) } diff --git a/src/bin/locate_project.rs b/src/bin/locate_project.rs index c9a352453f2..d89f6526920 100644 --- a/src/bin/locate_project.rs +++ b/src/bin/locate_project.rs @@ -1,3 +1,4 @@ +use cargo; use cargo::util::{CliResult, CliError, human, ChainError, Config}; use cargo::util::important_paths::{find_root_manifest_for_wd}; @@ -23,7 +24,7 @@ pub struct ProjectLocation { } pub fn execute(flags: LocateProjectFlags, - config: &Config) -> CliResult> { + config: &Config) -> CliResult { let root = find_root_manifest_for_wd(flags.flag_manifest_path, config.cwd())?; let string = root.to_str() @@ -32,5 +33,7 @@ pub fn execute(flags: LocateProjectFlags, Unicode")) .map_err(|e| CliError::new(e, 1))?; - Ok(Some(ProjectLocation { root: string.to_string() })) + let location = ProjectLocation { root: string.to_string() }; + cargo::print_json(&location); + Ok(()) } diff --git a/src/bin/login.rs b/src/bin/login.rs index 1202c82aaa8..7e575f47df4 100644 --- a/src/bin/login.rs +++ b/src/bin/login.rs @@ -34,7 +34,7 @@ Options: "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -60,6 +60,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let token = token.trim().to_string(); ops::registry_login(config, token)?; - Ok(None) + Ok(()) } diff --git a/src/bin/metadata.rs b/src/bin/metadata.rs index f7be16c575f..b96e641e29a 100644 --- a/src/bin/metadata.rs +++ b/src/bin/metadata.rs @@ -1,5 +1,6 @@ +use cargo; use cargo::core::Workspace; -use cargo::ops::{output_metadata, OutputMetadataOptions, ExportInfo}; +use cargo::ops::{output_metadata, OutputMetadataOptions}; use cargo::util::important_paths::find_root_manifest_for_wd; use cargo::util::{CliResult, Config}; @@ -42,7 +43,7 @@ Options: --locked Require Cargo.lock is up to date "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -60,5 +61,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-new; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, @@ -63,6 +63,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { else {"binary (application)"}, arg_path))?; - Ok(None) + Ok(()) } diff --git a/src/bin/owner.rs b/src/bin/owner.rs index 10fd84f8c2e..288d13e302c 100644 --- a/src/bin/owner.rs +++ b/src/bin/owner.rs @@ -44,7 +44,7 @@ See http://doc.crates.io/crates-io.html#cargo-owner for detailed documentation and troubleshooting. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -59,6 +59,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { list: options.flag_list, }; ops::modify_owners(config, &opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/package.rs b/src/bin/package.rs index 0292bb7ba99..df342fbf8bf 100644 --- a/src/bin/package.rs +++ b/src/bin/package.rs @@ -39,7 +39,7 @@ Options: --locked Require Cargo.lock is up to date "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -55,5 +55,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { allow_dirty: options.flag_allow_dirty, jobs: options.flag_jobs, })?; - Ok(None) + Ok(()) } diff --git a/src/bin/pkgid.rs b/src/bin/pkgid.rs index 536cfa19906..ea573361d9e 100644 --- a/src/bin/pkgid.rs +++ b/src/bin/pkgid.rs @@ -53,7 +53,7 @@ Example Package IDs "; pub fn execute(options: Options, - config: &Config) -> CliResult> { + config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -72,6 +72,6 @@ pub fn execute(options: Options, let spec = spec.as_ref().map(|s| &s[..]); let spec = ops::pkgid(&ws, spec)?; println!("{}", spec); - Ok(None) + Ok(()) } diff --git a/src/bin/publish.rs b/src/bin/publish.rs index 1228b294e14..dcba4df07e2 100644 --- a/src/bin/publish.rs +++ b/src/bin/publish.rs @@ -42,7 +42,7 @@ Options: "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -70,5 +70,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { jobs: jobs, dry_run: dry_run, })?; - Ok(None) + Ok(()) } diff --git a/src/bin/read_manifest.rs b/src/bin/read_manifest.rs index a663cda4a7a..07c4a5f2c45 100644 --- a/src/bin/read_manifest.rs +++ b/src/bin/read_manifest.rs @@ -1,5 +1,6 @@ use std::env; +use cargo; use cargo::core::Package; use cargo::util::{CliResult, Config}; use cargo::util::important_paths::{find_root_manifest_for_wd}; @@ -25,7 +26,7 @@ Options: --color WHEN Coloring: auto, always, never "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-read-manifest; args={:?}", env::args().collect::>()); config.shell().set_color_config(options.flag_color.as_ref().map(|s| &s[..]))?; @@ -33,5 +34,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?; let pkg = Package::for_path(&root, config)?; - Ok(Some(pkg)) + cargo::print_json(&pkg); + Ok(()) } diff --git a/src/bin/run.rs b/src/bin/run.rs index 0bb293d9d4f..79a0a326f1a 100644 --- a/src/bin/run.rs +++ b/src/bin/run.rs @@ -57,7 +57,7 @@ arguments to both Cargo and the binary, the ones after `--` go to the binary, the ones before go to Cargo. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -99,7 +99,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; match ops::run(&ws, &compile_opts, &options.arg_args)? { - None => Ok(None), + None => Ok(()), Some(err) => { // If we never actually spawned the process then that sounds pretty // bad and we always want to forward that up. diff --git a/src/bin/rustc.rs b/src/bin/rustc.rs index d812e5eb5b2..bff80feceec 100644 --- a/src/bin/rustc.rs +++ b/src/bin/rustc.rs @@ -73,7 +73,7 @@ processes spawned by Cargo, use the $RUSTFLAGS environment variable or the `build.rustflags` configuration option. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-rustc; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, @@ -120,6 +120,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; ops::compile(&ws, &opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/rustdoc.rs b/src/bin/rustdoc.rs index 7df9a27abcc..d82bc3e7e9c 100644 --- a/src/bin/rustdoc.rs +++ b/src/bin/rustdoc.rs @@ -70,7 +70,7 @@ current package is documented. For more information on SPEC and its format, see the `cargo help pkgid` command. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -108,5 +108,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; ops::doc(&ws, &doc_opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/search.rs b/src/bin/search.rs index 5bc518f659f..47879cae582 100644 --- a/src/bin/search.rs +++ b/src/bin/search.rs @@ -33,7 +33,7 @@ Options: --locked Require Cargo.lock is up to date "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -47,5 +47,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { } = options; ops::search(&query.join("+"), config, host, cmp::min(100, limit.unwrap_or(10)) as u8)?; - Ok(None) + Ok(()) } diff --git a/src/bin/test.rs b/src/bin/test.rs index 59bf0a09d1f..5f7f9eb8cf7 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -93,7 +93,7 @@ To get the list of all options available for the test binaries use this: cargo test -- --help "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -146,7 +146,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; let err = ops::run_tests(&ws, &ops, &options.arg_args)?; match err { - None => Ok(None), + None => Ok(()), Some(err) => { Err(match err.exit.as_ref().and_then(|e| e.code()) { Some(i) => CliError::new(human("test failed"), i), diff --git a/src/bin/uninstall.rs b/src/bin/uninstall.rs index 9f3c33af121..725452251e1 100644 --- a/src/bin/uninstall.rs +++ b/src/bin/uninstall.rs @@ -37,7 +37,7 @@ uninstalled for a crate but the `--bin` and `--example` flags can be used to only uninstall particular binaries. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -46,6 +46,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let root = options.flag_root.as_ref().map(|s| &s[..]); ops::uninstall(root, &options.arg_spec, &options.flag_bin, config)?; - Ok(None) + Ok(()) } diff --git a/src/bin/update.rs b/src/bin/update.rs index ede94bb0f79..e8da2bd5cbc 100644 --- a/src/bin/update.rs +++ b/src/bin/update.rs @@ -57,7 +57,7 @@ updated. For more information about package id specifications, see `cargo help pkgid`. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { debug!("executing; cmd=cargo-update; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, @@ -75,5 +75,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ws = Workspace::new(&root, config)?; ops::update_lockfile(&ws, &update_opts)?; - Ok(None) + Ok(()) } diff --git a/src/bin/verify_project.rs b/src/bin/verify_project.rs index be498942a1b..882873d1f1e 100644 --- a/src/bin/verify_project.rs +++ b/src/bin/verify_project.rs @@ -3,13 +3,12 @@ use std::fs::File; use std::io::prelude::*; use std::process; +use cargo; use cargo::util::important_paths::{find_root_manifest_for_wd}; use cargo::util::{CliResult, Config}; use rustc_serialize::json; use toml; -pub type Error = HashMap; - #[derive(RustcDecodable)] pub struct Flags { flag_manifest_path: Option, @@ -37,7 +36,7 @@ Options: --locked Require Cargo.lock is up to date "; -pub fn execute(args: Flags, config: &Config) -> CliResult> { +pub fn execute(args: Flags, config: &Config) -> CliResult { config.configure(args.flag_verbose, args.flag_quiet, &args.flag_color, @@ -63,7 +62,8 @@ pub fn execute(args: Flags, config: &Config) -> CliResult> { let mut h = HashMap::new(); h.insert("success".to_string(), "true".to_string()); - Ok(Some(h)) + cargo::print_json(&h); + Ok(()) } fn fail(reason: &str, value: &str) -> ! { diff --git a/src/bin/version.rs b/src/bin/version.rs index d809d1ddd94..9724481036a 100644 --- a/src/bin/version.rs +++ b/src/bin/version.rs @@ -18,10 +18,10 @@ Options: --color WHEN Coloring: auto, always, never "; -pub fn execute(_: Options, _: &Config) -> CliResult> { +pub fn execute(_: Options, _: &Config) -> CliResult { debug!("executing; cmd=cargo-version; args={:?}", env::args().collect::>()); println!("{}", cargo::version()); - Ok(None) + Ok(()) } diff --git a/src/bin/yank.rs b/src/bin/yank.rs index d7fdf775297..9e5663684ab 100644 --- a/src/bin/yank.rs +++ b/src/bin/yank.rs @@ -42,7 +42,7 @@ download the yanked version to use it. Cargo will, however, not allow any new crates to be locked to any yanked version. "; -pub fn execute(options: Options, config: &Config) -> CliResult> { +pub fn execute(options: Options, config: &Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, @@ -54,6 +54,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { options.flag_token, options.flag_index, options.flag_undo)?; - Ok(None) + Ok(()) } diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 920fb4185ed..822b668e27d 100755 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -104,32 +104,40 @@ impl fmt::Display for VersionInfo { } } -pub fn call_main_without_stdin( - exec: fn(T, &Config) -> CliResult>, +pub fn call_main_without_stdin( + exec: fn(Flags, &Config) -> CliResult, config: &Config, usage: &str, args: &[String], - options_first: bool) -> CliResult> - where V: Encodable, T: Decodable + options_first: bool) -> CliResult { - let flags = flags_from_args::(usage, args, options_first)?; + let docopt = Docopt::new(usage).unwrap() + .options_first(options_first) + .argv(args.iter().map(|s| &s[..])) + .help(true); + + let flags = docopt.decode().map_err(|e| { + let code = if e.fatal() {1} else {0}; + CliError::new(human(e.to_string()), code) + })?; + exec(flags, config) } // This will diverge if `result` is an `Err` and return otherwise. -pub fn process_executed(result: CliResult>, shell: &mut MultiShell) - where T: Encodable +pub fn process_executed(result: CliResult, shell: &mut MultiShell) { match result { Err(e) => handle_cli_error(e, shell), - Ok(Some(encodable)) => { - let encoded = json::encode(&encodable).unwrap(); - println!("{}", encoded); - } - Ok(None) => {} + Ok(()) => {} } } +pub fn print_json(obj: &T) { + let encoded = json::encode(&obj).unwrap(); + println!("{}", encoded); +} + pub fn shell(verbosity: Verbosity, color_config: ColorConfig) -> MultiShell { enum Output { Stdout, @@ -275,16 +283,3 @@ pub fn version() -> VersionInfo { } } } - -fn flags_from_args(usage: &str, args: &[String], options_first: bool) -> CliResult - where T: Decodable -{ - let docopt = Docopt::new(usage).unwrap() - .options_first(options_first) - .argv(args.iter().map(|s| &s[..])) - .help(true); - docopt.decode().map_err(|e| { - let code = if e.fatal() {1} else {0}; - CliError::new(human(e.to_string()), code) - }) -} diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index 853b8bef9d5..c54199f5b15 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -243,7 +243,7 @@ impl CargoError for Human { // ============================================================================= // CLI errors -pub type CliResult = Result; +pub type CliResult = Result<(), CliError>; #[derive(Debug)] pub struct CliError { diff --git a/tests/version.rs b/tests/version.rs index 2171cef5e13..5ace3fa2276 100644 --- a/tests/version.rs +++ b/tests/version.rs @@ -20,35 +20,6 @@ fn simple() { } -#[derive(RustcDecodable)] -struct FooFlags { - flag_version: bool, -} - -fn real_main(flags: FooFlags, _config: &cargo::Config) -> - cargo::CliResult> { - if flags.flag_version { - Ok(Some("foo ".to_string())) - } else { - Ok(None) - } -} - -#[test] -fn subcommand_with_version_using_exec_main_without_stdin() { - let usage = " -Usage: cargo foo [--version] - -Options: - -V, --version Print version info -"; - let args: Vec = vec!["cargo", "foo", "--version"] - .into_iter().map(|s| s.to_string()).collect(); - let result = cargo::call_main_without_stdin( - real_main, &cargo::Config::default().unwrap(), - usage, &args, false); - assert_eq!(result.unwrap(), Some("foo ".to_string())); -} #[test] #[cfg_attr(target_os = "windows", ignore)]