Skip to content

Commit

Permalink
Auto merge of #3614 - matklad:simpler-cli-2, r=alexcrichton
Browse files Browse the repository at this point in the history
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.

`CliResult` is now just a `Result<(), CliError>`.
  • Loading branch information
bors committed Jan 31, 2017
2 parents 1aa510d + 0c7e73c commit 546212c
Show file tree
Hide file tree
Showing 34 changed files with 100 additions and 128 deletions.
4 changes: 2 additions & 2 deletions src/bin/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<()>> {
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,
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
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),
Expand Down
4 changes: 2 additions & 2 deletions src/bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
debug!("executing; cmd=cargo-build; args={:?}",
env::args().collect::<Vec<_>>());
config.configure(options.flag_verbose,
Expand Down Expand Up @@ -110,5 +110,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {

let ws = Workspace::new(&root, config)?;
ops::compile(&ws, &opts)?;
Ok(None)
Ok(())
}
21 changes: 10 additions & 11 deletions src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ fn main() {

match result {
Err(e) => cargo::handle_cli_error(e, &mut *config.shell()),
Ok(None) => {},
Ok(Some(())) => unreachable!(),
Ok(()) => {},
}
}

Expand Down Expand Up @@ -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<Option<()>> {
fn execute(flags: Flags, config: &Config) -> CliResult {
config.configure(flags.flag_verbose,
flags.flag_quiet,
&flags.flag_color,
Expand All @@ -162,21 +161,21 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
}
}
}
return Ok(None)
return Ok(())
}

if flags.flag_list {
println!("Installed Commands:");
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[..] {
Expand All @@ -189,7 +188,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
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
Expand Down Expand Up @@ -221,7 +220,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
};

if try_execute(&config, &args) {
return Ok(None)
return Ok(())
}

let alias_list = aliased_command(&config, &args[1])?;
Expand All @@ -233,15 +232,15 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
.map(|s| s.to_string())
.collect::<Vec<_>>();
if try_execute(&config, &chain) {
return Ok(None)
return Ok(())
} else {
chain
}
}
None => args,
};
execute_subcommand(config, &args[1], &args)?;
Ok(None)
Ok(())
}

fn try_execute(config: &Config, args: &[String]) -> bool {
Expand Down Expand Up @@ -298,7 +297,7 @@ fn find_closest(config: &Config, cmd: &str) -> Option<String> {

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()
Expand Down
4 changes: 2 additions & 2 deletions src/bin/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct Options {
flag_frozen: bool,
}

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
debug!("executing; cmd=cargo-check; args={:?}",
env::args().collect::<Vec<_>>());

Expand Down Expand Up @@ -100,5 +100,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
};

ops::compile(&ws, &opts)?;
Ok(None)
Ok(())
}
4 changes: 2 additions & 2 deletions src/bin/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
debug!("executing; cmd=cargo-clean; args={:?}", env::args().collect::<Vec<_>>());
config.configure(options.flag_verbose,
options.flag_quiet,
Expand All @@ -59,5 +59,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
};
let ws = Workspace::new(&root, config)?;
ops::clean(&ws, &opts)?;
Ok(None)
Ok(())
}
4 changes: 2 additions & 2 deletions src/bin/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand Down Expand Up @@ -109,5 +109,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {

let ws = Workspace::new(&root, config)?;
ops::doc(&ws, &doc_opts)?;
Ok(None)
Ok(())
}
4 changes: 2 additions & 2 deletions src/bin/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand All @@ -47,6 +47,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
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(())
}

4 changes: 2 additions & 2 deletions src/bin/generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Options:
--locked Require Cargo.lock is up to date
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
debug!("executing; cmd=cargo-generate-lockfile; args={:?}", env::args().collect::<Vec<_>>());
config.configure(options.flag_verbose,
options.flag_quiet,
Expand All @@ -42,5 +42,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {

let ws = Workspace::new(&root, config)?;
ops::generate_lockfile(&ws)?;
Ok(None)
Ok(())
}
4 changes: 2 additions & 2 deletions src/bin/git_checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Options:
--locked Require Cargo.lock is up to date
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand All @@ -46,5 +46,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {

source.update()?;

Ok(None)
Ok(())
}
2 changes: 1 addition & 1 deletion src/bin/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Options:
-h, --help Print this message
";

pub fn execute(_: Options, _: &Config) -> CliResult<Option<()>> {
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.
Expand Down
4 changes: 2 additions & 2 deletions src/bin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Options:
--locked Require Cargo.lock is up to date
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
debug!("executing; cmd=cargo-init; args={:?}", env::args().collect::<Vec<_>>());
config.configure(options.flag_verbose,
options.flag_quiet,
Expand All @@ -63,6 +63,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
if opts_lib { "library" }
else {"binary (application)"}))?;

Ok(None)
Ok(())
}

4 changes: 2 additions & 2 deletions src/bin/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand Down Expand Up @@ -147,5 +147,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
} else {
ops::install(root, krate, &source, vers, &compile_opts, options.flag_force)?;
}
Ok(None)
Ok(())
}
7 changes: 5 additions & 2 deletions src/bin/locate_project.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cargo;
use cargo::util::{CliResult, CliError, human, ChainError, Config};
use cargo::util::important_paths::{find_root_manifest_for_wd};

Expand All @@ -23,7 +24,7 @@ pub struct ProjectLocation {
}

pub fn execute(flags: LocateProjectFlags,
config: &Config) -> CliResult<Option<ProjectLocation>> {
config: &Config) -> CliResult {
let root = find_root_manifest_for_wd(flags.flag_manifest_path, config.cwd())?;

let string = root.to_str()
Expand All @@ -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(())
}
4 changes: 2 additions & 2 deletions src/bin/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Options:
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand All @@ -60,6 +60,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {

let token = token.trim().to_string();
ops::registry_login(config, token)?;
Ok(None)
Ok(())
}

8 changes: 5 additions & 3 deletions src/bin/metadata.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -42,7 +43,7 @@ Options:
--locked Require Cargo.lock is up to date
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<ExportInfo>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand All @@ -60,5 +61,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<ExportInfo

let ws = Workspace::new(&manifest, config)?;
let result = output_metadata(&ws, &options)?;
Ok(Some(result))
cargo::print_json(&result);
Ok(())
}
4 changes: 2 additions & 2 deletions src/bin/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Options:
--locked Require Cargo.lock is up to date
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
debug!("executing; cmd=cargo-new; args={:?}", env::args().collect::<Vec<_>>());
config.configure(options.flag_verbose,
options.flag_quiet,
Expand All @@ -63,6 +63,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
else {"binary (application)"},
arg_path))?;

Ok(None)
Ok(())
}

4 changes: 2 additions & 2 deletions src/bin/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand All @@ -59,6 +59,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
list: options.flag_list,
};
ops::modify_owners(config, &opts)?;
Ok(None)
Ok(())
}

4 changes: 2 additions & 2 deletions src/bin/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Options:
--locked Require Cargo.lock is up to date
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
pub fn execute(options: Options, config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand All @@ -55,5 +55,5 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
allow_dirty: options.flag_allow_dirty,
jobs: options.flag_jobs,
})?;
Ok(None)
Ok(())
}
4 changes: 2 additions & 2 deletions src/bin/pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Example Package IDs
";

pub fn execute(options: Options,
config: &Config) -> CliResult<Option<()>> {
config: &Config) -> CliResult {
config.configure(options.flag_verbose,
options.flag_quiet,
&options.flag_color,
Expand All @@ -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(())
}

Loading

0 comments on commit 546212c

Please sign in to comment.