Skip to content

Commit

Permalink
refactor: add convenience methods for no-prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
jpopesculian committed Jan 14, 2025
1 parent 66601b7 commit 8d436e5
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 68 deletions.
23 changes: 18 additions & 5 deletions src/commands/global_args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
colors::ColorChoiceExt,
dialog::{Confirm, FuzzySelect},
dirs::{init_venv, opt_init_venv},
error::Result,
graphql_client::graphql_url,
Expand Down Expand Up @@ -46,12 +47,12 @@ pub struct GlobalArgs {
pub dep_link_mode: LinkMode,
#[arg(
short = 'y',
long = "yes",
help = "Skip interactive dialogs and automatically confirm all prompts",
long = "no-prompt",
help = "Skip interactive dialogs and automatically confirm",
default_value_t = false,
global = true
)]
pub yes: bool,
pub no_prompt: bool,
}

impl GlobalArgs {
Expand Down Expand Up @@ -85,7 +86,7 @@ impl GlobalArgs {
self.python.as_ref(),
self.color.forced(),
self.dep_link_mode,
self.yes,
self.no_prompt,
pb,
)
.await
Expand All @@ -98,9 +99,21 @@ impl GlobalArgs {
self.python.as_ref(),
self.color.forced(),
self.dep_link_mode,
self.yes,
self.no_prompt,
pb,
)
.await
}

pub fn confirm(&self) -> Confirm {
Confirm::new()
.with_theme(self.color.dialoguer())
.no_prompt(self.no_prompt)
}

pub fn fuzzy_select(&self) -> FuzzySelect {
FuzzySelect::new()
.with_theme(self.color.dialoguer())
.no_prompt(self.no_prompt)
}
}
9 changes: 4 additions & 5 deletions src/commands/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use tokio::process::Command;
use url::Url;

use crate::{
dialog::AutoConfirmDialog,
dirs::{project_vscode_dir, vscode_settings_path},
error::{self, Result},
process::run_command,
Expand Down Expand Up @@ -116,7 +115,7 @@ async fn handle_vscode_integration(
async fn ask_for_install_vscode_extensions(
allow_vscode_extensions: Option<bool>,
pb: &ProgressBar,
auto_confirm: bool,
global_args: &GlobalArgs,
) -> Result<()> {
let mut vscode_settings = UserVSCodeSettings::load().await?;

Expand Down Expand Up @@ -151,10 +150,10 @@ async fn ask_for_install_vscode_extensions(

let can_install = tokio::task::spawn_blocking({
let pb = pb.clone();
let args = global_args.clone();
move || {
pb.suspend(|| {
AutoConfirmDialog::new()
.auto_confirm(auto_confirm)
args.confirm()
.with_prompt(prompt_message)
.default(true)
.interact()
Expand Down Expand Up @@ -201,7 +200,7 @@ pub async fn lab(args: Lab, global_args: GlobalArgs) -> Result<()> {
.can_install_extensions
.is_none()
{
ask_for_install_vscode_extensions(args.allow_vscode_extensions, &pb, global_args.yes)
ask_for_install_vscode_extensions(args.allow_vscode_extensions, &pb, &global_args)
.await?;
}
handle_vscode_integration(global_args, &env, &pb).await
Expand Down
7 changes: 3 additions & 4 deletions src/commands/login.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
colors::ColorChoiceExt,
commands::GlobalArgs,
credentials::{get_credentials, with_locked_credentials, Credentials},
dialog::AutoConfirmDialog,
error::{self, Result},
progress_bar::default_spinner,
shutdown::shutdown_signal,
Expand Down Expand Up @@ -392,12 +390,13 @@ pub async fn check_login(global: GlobalArgs, multi_progress: &MultiProgress) ->
return Ok(true);
}
let confirmation = multi_progress.suspend(|| {
AutoConfirmDialog::with_theme(global.color.dialoguer().as_ref())
.auto_confirm(global.yes)
global
.confirm()
.with_prompt(
"Your aqora account is not currently connected. Would you like to connect it now?",
)
.default(true)
.no_prompt_value(false)
.interact()
})?;
if confirmation {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/template.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{
colors::ColorChoiceExt,
commands::{
install::{install, Install},
login::check_login,
Expand Down Expand Up @@ -132,9 +131,10 @@ pub async fn template(args: Template, global: GlobalArgs) -> Result<()> {
format!("@{} ({})", org.username.clone(), org.display_name.clone())
}));
Result::Ok(
dialoguer::FuzzySelect::with_theme(global.color.dialoguer().as_ref())
global
.fuzzy_select()
.with_prompt("Would you like to submit with a team? (Press ESC to skip)")
.items(&items)
.items(items)
.interact_opt()
.map_err(|err| {
error::system(
Expand Down
41 changes: 18 additions & 23 deletions src/commands/upload.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
colors::ColorChoiceExt,
commands::{login::check_login, GlobalArgs},
compress::{compress, DEFAULT_ARCH_EXTENSION, DEFAULT_ARCH_MIME_TYPE},
dialog::AutoConfirmDialog,
dirs::{
project_last_run_dir, project_last_run_result, project_use_case_toml_path, pyproject_path,
read_pyproject,
Expand All @@ -18,7 +16,6 @@ use crate::{
upload::upload_project_version_file,
};
use aqora_config::{PyProject, Version};
use aqora_runner::python::ColorChoice;
use clap::Args;
use futures::prelude::*;
use graphql_client::GraphQLQuery;
Expand Down Expand Up @@ -388,17 +385,16 @@ async fn update_project_version(
project_path: impl AsRef<Path>,
last_version: Option<&Version>,
pb: &ProgressBar,
color: ColorChoice,
auto_confirm: bool,
global_args: &GlobalArgs,
) -> Result<Version> {
let mut version = project.version().unwrap();

if let Some(last_version) = last_version {
if last_version >= &version {
let new_version = increment_version(last_version);
let confirmation = pb.suspend(|| {
AutoConfirmDialog::with_theme(color.dialoguer().as_ref())
.auto_confirm(auto_confirm)
global_args
.confirm()
.with_prompt(format!(
r#"Project version must be greater than {last_version}.
Do you want to update the version to {new_version} now?"#
Expand Down Expand Up @@ -493,8 +489,7 @@ pub async fn upload_use_case(
&global.project,
competition.version.as_ref(),
&use_case_pb,
global.color,
global.yes,
&global,
)
.await?;

Expand Down Expand Up @@ -823,8 +818,8 @@ pub async fn upload_submission(
}

let accepts = m.suspend(|| {
let will_review = AutoConfirmDialog::with_theme(global.color.dialoguer().as_ref())
.auto_confirm(global.yes)
let will_review = global
.confirm()
.with_prompt(format!("{message} Would you like to review them now?"))
.default(true)
.interact()
Expand All @@ -836,9 +831,10 @@ pub async fn upload_submission(
if dialoguer::Editor::new().edit(&rules).is_err() {
return false;
}
AutoConfirmDialog::with_theme(global.color.dialoguer().as_ref())
.auto_confirm(global.yes)
global
.confirm()
.with_prompt("Would you like to accept?")
.no_prompt_value(true)
.interact()
.ok()
.unwrap_or_default()
Expand Down Expand Up @@ -879,8 +875,8 @@ pub async fn upload_submission(
let evaluation_path = project_last_run_dir(&global.project);
if !evaluation_path.exists() {
let confirmation = m.suspend(|| {
AutoConfirmDialog::with_theme(global.color.dialoguer().as_ref())
.auto_confirm(global.yes)
global
.confirm()
.with_prompt(
r#"No last run result found.
Would you like to run the tests now?"#,
Expand Down Expand Up @@ -912,8 +908,8 @@ Would you like to run the tests now?"#,
if let Ok(last_run_result) = last_run_result.as_ref() {
if last_run_result.use_case_version.as_ref() != Some(&use_case_version) {
let confirmation = m.suspend(|| {
AutoConfirmDialog::with_theme(global.color.dialoguer().as_ref())
.auto_confirm(global.yes)
global
.confirm()
.with_prompt(
r#"It seems the use case version has changed since the last test run.
It is required to run the tests again.
Expand Down Expand Up @@ -950,8 +946,8 @@ Do you want to run the tests now?"#,
}
if should_run_tests {
let confirmation = m.suspend(|| {
AutoConfirmDialog::with_theme(global.color.dialoguer().as_ref())
.auto_confirm(global.yes)
global
.confirm()
.with_prompt(
r#"It seems you have made some changes since since the last test run.
Those changes may not be reflected in the submission unless you re-run the tests.
Expand All @@ -967,8 +963,8 @@ Do you want to re-run the tests now?"#,
}
} else {
let confirmation = m.suspend(|| {
AutoConfirmDialog::with_theme(global.color.dialoguer().as_ref())
.auto_confirm(global.yes)
global
.confirm()
.with_prompt(
r#"It seems the last test run result is corrupted or missing.
It is required to run the tests again.
Expand All @@ -992,8 +988,7 @@ Do you want to run the tests now?"#,
&global.project,
submission_version.as_ref(),
&use_case_pb,
global.color,
global.yes,
&global,
)
.await?;

Expand Down
Loading

0 comments on commit 8d436e5

Please sign in to comment.