Skip to content

Commit 150f9fa

Browse files
committed
feat: add ability to say "no" in auto confirmation
1 parent b085d3b commit 150f9fa

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

src/cli.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Parse the command-line arguments
22
33
use clap::{
4-
CommandFactory as _, Parser, Subcommand,
4+
CommandFactory as _, Parser, Subcommand, ValueEnum,
55
builder::styling::{AnsiColor, Effects},
66
};
77

@@ -27,19 +27,28 @@ pub struct Cli {
2727
pub use_gh_cli: bool,
2828
}
2929

30+
/// Overwrite existing patchy config file if it exists
31+
#[derive(ValueEnum, Clone, Debug, Copy)]
32+
pub enum Confirm {
33+
/// Overwrite
34+
Yes,
35+
/// Do not overwrite
36+
No,
37+
}
38+
3039
#[derive(Subcommand, Debug)]
3140
pub enum Command {
3241
/// Create example config file
3342
Init {
34-
/// Do not prompt when overwriting local-branch specified in the config
43+
/// Do not ask for confirmation when overwriting existing config file
3544
#[arg(short, long)]
36-
yes: bool,
45+
confirm: Option<Confirm>,
3746
},
3847
/// Invoke patchy
3948
Run {
40-
/// Do not prompt when overwriting local-branch specified in the config
49+
/// Do not ask for confirmation when overwriting the specified branch
4150
#[arg(short, long)]
42-
yes: bool,
51+
confirm: Option<Confirm>,
4352
},
4453
/// Generate a .patch file from a commit hash
4554
GenPatch {
@@ -94,8 +103,10 @@ impl Command {
94103
/// Execute the command
95104
pub async fn execute(self, use_gh_cli: bool) -> anyhow::Result<()> {
96105
match self {
97-
Self::Init { yes } => commands::init(yes)?,
98-
Self::Run { yes } => commands::run(yes, use_gh_cli).await?,
106+
Self::Init {
107+
confirm: overwrite_file_if_exists,
108+
} => commands::init(overwrite_file_if_exists)?,
109+
Self::Run { confirm } => commands::run(confirm, use_gh_cli).await?,
99110
Self::GenPatch { commit, filename } => {
100111
commands::gen_patch(commit, filename)?;
101112
}

src/commands/init.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ use std::io::Write as _;
66
use anyhow::bail;
77
use colored::Colorize as _;
88

9+
use crate::cli::Confirm;
910
use crate::{config, confirm_prompt};
1011

1112
/// Initialize the Patchy config file
12-
pub fn init(yes: bool) -> anyhow::Result<()> {
13-
if config::FILE_PATH.exists()
14-
&& !yes
15-
&& !confirm_prompt!(
16-
"File {} already exists. Overwrite it?",
17-
config::FILE_PATH.to_string_lossy().bright_blue(),
18-
)
19-
{
20-
bail!("Did not overwrite {}", config::FILE_PATH.display());
13+
pub fn init(overwrite: Option<Confirm>) -> anyhow::Result<()> {
14+
if config::FILE_PATH.exists() {
15+
let overwrite_if_exists = match overwrite {
16+
Some(Confirm::Yes) => true,
17+
Some(Confirm::No) => false,
18+
None => confirm_prompt!(
19+
"File {} already exists. Overwrite it?",
20+
config::FILE_PATH.to_string_lossy().bright_blue(),
21+
),
22+
};
23+
24+
if !overwrite_if_exists {
25+
bail!("Did not overwrite {}", config::FILE_PATH.display());
26+
}
2127
}
2228

2329
fs::create_dir_all(&*config::PATH)?;

src/commands/run.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! `run` subcommand
22
3+
use crate::cli::Confirm;
34
use crate::config::{self, BranchName, Config, PrNumber, PullRequest};
45
use anyhow::Result;
56
use std::fs;
@@ -12,7 +13,7 @@ use crate::utils::{format_pr, format_url, with_uuid};
1213
use crate::{commands, confirm_prompt, git};
1314

1415
/// Run patchy, if `yes` then there will be no prompt
15-
pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
16+
pub async fn run(confirm: Option<Confirm>, use_gh_cli: bool) -> Result<()> {
1617
let root = config::ROOT.as_str();
1718

1819
let Ok(config_string) = fs::read_to_string(&*config::FILE_PATH) else {
@@ -21,14 +22,16 @@ pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
2122
config::FILE
2223
);
2324

24-
// We don't want to have *any* sort of prompt when using the -y flag since that
25-
// would be problematic in scripts
26-
if !yes && confirm_prompt!("Would you like us to run `patchy init` to initialize it?",) {
27-
commands::init(false)?;
28-
} else if yes {
29-
log::info!("You can create it with `patchy init`",);
25+
let init_file = match confirm {
26+
Some(Confirm::Yes) => true,
27+
Some(Confirm::No) => false,
28+
None => confirm_prompt!("Would you like us to run `patchy init` to initialize it?",),
29+
};
30+
31+
if init_file {
32+
commands::init(Some(Confirm::Yes))?;
3033
} else {
31-
// user said "no" in the prompt, so we don't do any initializing
34+
log::info!("You can create it with `patchy init`",);
3235
}
3336

3437
// We don't want to read the default configuration file as config_string. Since
@@ -243,20 +246,17 @@ pub async fn run(yes: bool, use_gh_cli: bool) -> Result<()> {
243246
&info.branch.local_branch_name,
244247
)?;
245248

246-
if yes
247-
|| confirm_prompt!(
249+
let overwrite_branch = match confirm {
250+
Some(Confirm::Yes) => true,
251+
Some(Confirm::No) => false,
252+
None => confirm_prompt!(
248253
"Overwrite branch {}? This is irreversible.",
249254
config.local_branch.as_ref().cyan()
250-
)
251-
{
255+
),
256+
};
257+
258+
if overwrite_branch {
252259
git::rename_branch(&temporary_branch, config.local_branch.as_ref())?;
253-
if yes {
254-
log::info!(
255-
"Automatically overwrote branch {} since you supplied the {} flag",
256-
config.local_branch.as_ref().cyan(),
257-
"--yes".bright_magenta()
258-
);
259-
}
260260
log::info!("Success!");
261261
return Ok(());
262262
}

0 commit comments

Comments
 (0)