Skip to content

Commit 0da5e2c

Browse files
committed
Refuse to overwrite existing config file on create
1 parent 341cc6c commit 0da5e2c

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

crackers/src/bin/crackers/main.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,31 @@ fn main() {
5959
CrackersCommands::New { config, library } => {
6060
init_basic_logging();
6161
event!(Level::INFO, "Creating new config file");
62-
new(
63-
config.clone().unwrap_or(PathBuf::from("./crackers.toml")),
64-
library.clone(),
65-
)
62+
// If the user explicitly provided a config path, refuse to overwrite an existing file.
63+
if let Some(cfg_path) = config {
64+
if cfg_path.exists() {
65+
event!(
66+
Level::WARN,
67+
"Refusing to create new config: file already exists: {}",
68+
cfg_path.display()
69+
);
70+
std::process::exit(1);
71+
}
72+
new(cfg_path.clone(), library.clone())
73+
} else {
74+
// If the user did not specify a config path, check the default file and
75+
// refuse to overwrite it as well.
76+
let default_path = PathBuf::from("./crackers.toml");
77+
if default_path.exists() {
78+
event!(
79+
Level::WARN,
80+
"Refusing to create new config: default file already exists: {}",
81+
default_path.display()
82+
);
83+
std::process::exit(1);
84+
}
85+
new(default_path, library.clone())
86+
}
6687
}
6788
CrackersCommands::Synth { config } => {
6889
// Synth initializes its own logging with config

0 commit comments

Comments
 (0)