Skip to content

Commit

Permalink
Auto merge of #4374 - behnam:new-init, r=alexcrichton
Browse files Browse the repository at this point in the history
[cargo_new] Hint to use `cargo init` on existing dir

The `new` command always expects a non-existing path. The `init` command
always expects an existing path. Therefore, it makes sense to hint to
use `init` if the pre-condition to `new` is not satisfied.

Fixes <#4366>
  • Loading branch information
bors committed Aug 8, 2017
2 parents 35bda50 + 484a33a commit 287c028
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/bin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Options {
}

pub const USAGE: &'static str = "
Create a new cargo package in current directory
Create a new cargo package in an existing directory
Usage:
cargo init [options] [<path>]
Expand All @@ -27,8 +27,9 @@ Usage:
Options:
-h, --help Print this message
--vcs VCS Initialize a new repository for the given version
control system (git or hg) or do not initialize any version
control at all (none) overriding a global configuration.
control system (git, hg, pijul, or fossil) or do not
initialize any version control at all (none), overriding
a global configuration.
--bin Use a binary (application) template
--lib Use a library template [default]
--name NAME Set the resulting package name
Expand All @@ -49,11 +50,11 @@ pub fn execute(options: Options, config: &Config) -> CliResult {

let Options { flag_bin, flag_lib, arg_path, flag_name, flag_vcs, .. } = options;

let tmp = &arg_path.unwrap_or(format!("."));
let path = &arg_path.unwrap_or(format!("."));
let opts = ops::NewOptions::new(flag_vcs,
flag_bin,
flag_lib,
tmp,
path,
flag_name.as_ref().map(|s| s.as_ref()));

let opts_lib = opts.lib;
Expand Down
8 changes: 5 additions & 3 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,14 @@ fn plan_new_source_file(bin: bool, project_name: String) -> SourceFileInformatio
pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
let path = config.cwd().join(opts.path);
if fs::metadata(&path).is_ok() {
bail!("destination `{}` already exists",
path.display())
bail!("destination `{}` already exists\n\n\
Use `cargo init` to initialize the directory\
", path.display()
)
}

if opts.lib && opts.bin {
bail!("can't specify both lib and binary outputs");
bail!("can't specify both lib and binary outputs")
}

let name = get_name(&path, &opts, config)?;
Expand Down
3 changes: 2 additions & 1 deletion tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ fn existing() {
fs::create_dir(&dst).unwrap();
assert_that(cargo_process("new").arg("foo"),
execs().with_status(101)
.with_stderr(format!("[ERROR] destination `{}` already exists\n",
.with_stderr(format!("[ERROR] destination `{}` already exists\n\n\
Use `cargo init` to initialize the directory",
dst.display())));
}

Expand Down

0 comments on commit 287c028

Please sign in to comment.