Skip to content

Commit

Permalink
Fix --yes flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiimk committed Oct 2, 2024
1 parent d6c12b0 commit 36deeaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/app/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ pub async fn run(workspace_layout: WorkspaceLayout, args: cli::Cli) -> Result<()
.add_value(dependencies_graph_repository)
.bind::<dyn DependencyGraphRepository, DependencyGraphRepositoryInMemory>();

base_catalog_builder.add_value(Interact::new(args.yes));

let output_config = configure_output_format(&args, &workspace_svc);
base_catalog_builder.add_value(output_config.clone());
base_catalog_builder.add_value(Interact::new(args.yes, output_config.is_tty));

let guards = configure_logging(&output_config, &workspace_layout, args.no_color);

Expand Down
16 changes: 14 additions & 2 deletions src/app/cli/src/output/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@ use crate::CLIError;
pub struct Interact {
/// Don't ask user for confirmation and assume 'yes'
pub assume_yes: bool,

/// Whether the current session supports interactive input from the user
pub is_tty: bool,
}

impl Interact {
pub fn new(assume_yes: bool) -> Self {
Self { assume_yes }
pub fn new(assume_yes: bool, is_tty: bool) -> Self {
Self { assume_yes, is_tty }
}

pub fn require_confirmation(&self, prompt: impl std::fmt::Display) -> Result<(), CLIError> {
use read_input::prelude::*;

let prompt = format!("{prompt}\nDo you wish to continue? [y/N]: ");

if !self.is_tty {
return if self.assume_yes {
Ok(())
} else {
eprintln!("{prompt} Assuming 'no' because --yes flag was not provided");
Err(CLIError::Aborted)
};
}

let answer: String = input()
.repeat_msg(prompt)
.default("n".to_owned())
Expand Down

0 comments on commit 36deeaf

Please sign in to comment.