Skip to content

Commit c744d87

Browse files
committed
Add force flag to overwrite any existing rcds config on init
Signed-off-by: Robert Detjens <[email protected]>
1 parent 584cc6b commit c744d87

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,8 @@ pub enum Commands {
103103
/// Create a config file with example placeholder values.
104104
#[arg(short = 'p', long)]
105105
placeholders: bool,
106+
/// Force overwrite any existing rcds config file
107+
#[arg(short = 'f', long)]
108+
force: bool,
106109
},
107110
}

src/commands/init.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
use anyhow::{Context, Result};
1+
use anyhow::{bail, Context, Result};
2+
use inquire;
3+
use owo_colors::OwoColorize;
24
use std::fs::File;
35
use std::io::Write;
6+
use std::path::Path;
47
use std::process::exit;
5-
use tracing::{error, warn};
8+
use tracing::{debug, error, warn};
69

710
use crate::init;
811
use crate::{access_handlers::frontend, commands::deploy};
912

10-
pub fn run(_interactive: &bool, placeholders: &bool, blank: &bool) -> Result<()> {
13+
pub fn run(_interactive: &bool, placeholders: &bool, blank: &bool, force: &bool) -> Result<()> {
14+
// Check if the file exists!
15+
if let Ok(true) = Path::new("rcds.yaml").try_exists() {
16+
if *force {
17+
debug!("rcds.yaml config file exists but forcing overwrite");
18+
} else {
19+
match inquire::Confirm::new("An rcds.yaml config already exists! Overwrite?")
20+
.with_default(false)
21+
.prompt()
22+
{
23+
Ok(true) => (), // ok to overwrite
24+
Ok(false) => bail!("Not overwriting"),
25+
Err(err) => bail!("Error prompting user"),
26+
}
27+
}
28+
}
29+
1130
let options = if *blank {
1231
init::blank_init()
1332
} else if *placeholders {

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fn dispatch(cli: cli::Cli) -> anyhow::Result<()> {
9494
interactive,
9595
placeholders,
9696
blank,
97-
} => commands::init::run(interactive, placeholders, blank),
97+
force,
98+
} => commands::init::run(interactive, placeholders, blank, force),
9899
}
99100
}

0 commit comments

Comments
 (0)