Skip to content

Commit 9bb9161

Browse files
committed
Support cargo new/init --force
1 parent 4da06f5 commit 9bb9161

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct NewOptions {
3232
pub path: PathBuf,
3333
pub name: Option<String>,
3434
pub edition: Option<String>,
35+
pub force: bool,
3536
pub registry: Option<String>,
3637
}
3738

@@ -81,6 +82,7 @@ impl NewOptions {
8182
path: PathBuf,
8283
name: Option<String>,
8384
edition: Option<String>,
85+
force: bool,
8486
registry: Option<String>,
8587
) -> CargoResult<NewOptions> {
8688
let kind = match (bin, lib) {
@@ -96,6 +98,7 @@ impl NewOptions {
9698
path,
9799
name,
98100
edition,
101+
force,
99102
registry,
100103
};
101104
Ok(opts)
@@ -301,7 +304,7 @@ fn plan_new_source_file(bin: bool, package_name: String) -> SourceFileInformatio
301304

302305
pub fn new<'a>(opts: &'a NewOptions, config: &Config) -> CargoResult<&'a str> {
303306
let path = &opts.path;
304-
if fs::metadata(path).is_ok() {
307+
if fs::metadata(path).is_ok() && !opts.force {
305308
failure::bail!(
306309
"destination `{}` already exists\n\n\
307310
Use `cargo init` to initialize the directory",
@@ -335,7 +338,7 @@ pub fn new<'a>(opts: &'a NewOptions, config: &Config) -> CargoResult<&'a str> {
335338
pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
336339
let path = &opts.path;
337340

338-
if fs::metadata(&path.join("Cargo.toml")).is_ok() {
341+
if fs::metadata(&path.join("Cargo.toml")).is_ok() && !opts.force {
339342
failure::bail!("`cargo init` cannot be run on existing Cargo packages")
340343
}
341344

src/cargo/util/command_prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ pub trait AppExt: Sized {
171171
)
172172
.value_name("NAME"),
173173
)
174+
._arg(opt("force", "Force initialising over existing files").short("f"))
174175
._arg(opt("registry", "Registry to use").value_name("REGISTRY"))
175176
}
176177

@@ -381,6 +382,7 @@ pub trait ArgMatchesExt {
381382
self.value_of_path("path", config).unwrap(),
382383
self._value_of("name").map(|s| s.to_string()),
383384
self._value_of("edition").map(|s| s.to_string()),
385+
self._is_present("force"),
384386
self.registry(config)?,
385387
)
386388
}

tests/testsuite/init.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,16 @@ fn auto_git() {
276276
assert!(paths::root().join(".gitignore").is_file());
277277
}
278278

279+
#[test]
280+
fn force() {
281+
cargo_process("init").run();
282+
cargo_process("init")
283+
.with_status(101)
284+
.with_stderr("[ERROR] `cargo init` cannot be run on existing Cargo packages")
285+
.run();
286+
cargo_process("init --force").run();
287+
}
288+
279289
#[test]
280290
fn invalid_dir_name() {
281291
let foo = &paths::root().join("foo.bar");

tests/testsuite/new.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ fn existing() {
120120
.run();
121121
}
122122

123+
#[test]
124+
fn force() {
125+
fs::create_dir(paths::root().join("foo")).unwrap();
126+
cargo_process("new --force foo").run();
127+
}
128+
123129
#[test]
124130
fn invalid_characters() {
125131
cargo_process("new foo.rs")

0 commit comments

Comments
 (0)