Skip to content

Commit

Permalink
[Cli] Add default_prompt_response option to set_global_config com…
Browse files Browse the repository at this point in the history
…mand (aptos-labs#5614)

* [cli] add default_prompt_response option to set_global_config

* update options

* update global config struct

* add prompt response getter

* apply default prompt option config

* update prompt func

* update prompt func

* fix lint issue

* fix lint error
  • Loading branch information
fgrust authored and areshand committed Dec 17, 2022
1 parent e2cd084 commit cc27e67
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ impl FromStr for EncodingType {
}

/// An insertable option for use with prompts.
#[derive(Clone, Copy, Debug, Default, Parser)]
#[derive(Clone, Copy, Debug, Default, Parser, PartialEq, Eq)]
pub struct PromptOptions {
/// Assume yes for all yes/no prompts
#[clap(long, group = "prompt_options")]
Expand Down
17 changes: 15 additions & 2 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::{
common::types::{CliError, CliTypedResult, PromptOptions},
config::GlobalConfig,
CliResult,
};
use aptos_build_info::build_information;
Expand Down Expand Up @@ -154,10 +155,22 @@ pub fn check_if_file_exists(file: &Path, prompt_options: PromptOptions) -> CliTy
}

pub fn prompt_yes_with_override(prompt: &str, prompt_options: PromptOptions) -> CliTypedResult<()> {
if prompt_options.assume_no || (!prompt_options.assume_yes && !prompt_yes(prompt)) {
Err(CliError::AbortedError)
if prompt_options.assume_no {
return Err(CliError::AbortedError);
} else if prompt_options.assume_yes {
return Ok(());
}

let is_yes = if let Some(response) = GlobalConfig::load()?.get_default_prompt_response() {
response
} else {
prompt_yes(prompt)
};

if is_yes {
Ok(())
} else {
Err(CliError::AbortedError)
}
}

Expand Down
71 changes: 71 additions & 0 deletions crates/aptos/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ pub struct SetGlobalConfig {
/// `Global` will put the `.aptos/` folder in your home directory
#[clap(long)]
config_type: Option<ConfigType>,
/// A configuration for how to expect the prompt response
///
/// Option can be one of ["yes", "no", "prompt"], "yes" runs cli with "--assume-yes", where
/// "no" runs cli with "--assume-no", default: "prompt"
#[clap(long)]
default_prompt_response: Option<PromptResponseType>,
}

#[async_trait]
Expand All @@ -105,6 +111,10 @@ impl CliCommand<GlobalConfig> for SetGlobalConfig {
config.config_type = Some(config_type);
}

if let Some(default_prompt_response) = self.default_prompt_response {
config.default_prompt_response = default_prompt_response;
}

config.save()?;
config.display()
}
Expand Down Expand Up @@ -174,6 +184,9 @@ pub struct GlobalConfig {
/// Whether to be using Global or Workspace mode
#[serde(skip_serializing_if = "Option::is_none")]
pub config_type: Option<ConfigType>,
/// Prompt response type
#[serde(default)]
pub default_prompt_response: PromptResponseType,
}

impl GlobalConfig {
Expand Down Expand Up @@ -204,6 +217,15 @@ impl GlobalConfig {
}
}

/// Get the prompt options from global config
pub fn get_default_prompt_response(&self) -> Option<bool> {
match self.default_prompt_response {
PromptResponseType::Prompt => None, // prompt
PromptResponseType::Yes => Some(true), // assume_yes
PromptResponseType::No => Some(false), // assume_no
}
}

fn save(&self) -> CliTypedResult<()> {
let global_folder = global_folder()?;
create_dir_if_not_exist(global_folder.as_path())?;
Expand Down Expand Up @@ -291,3 +313,52 @@ impl FromStr for ConfigType {
}
}
}

const PROMPT: &str = "prompt";
const ASSUME_YES: &str = "yes";
const ASSUME_NO: &str = "no";

/// A configuration for how to expect the prompt response
///
/// Option can be one of ["yes", "no", "prompt"], "yes" runs cli with "--assume-yes", where
/// "no" runs cli with "--assume-no", default: "prompt"
#[derive(Debug, Copy, Clone, Serialize, Deserialize, ArgEnum)]
pub enum PromptResponseType {
/// normal prompt
Prompt,
/// `--assume-yes`
Yes,
/// `--assume-no`
No,
}

impl Default for PromptResponseType {
fn default() -> Self {
Self::Prompt
}
}

impl std::fmt::Display for PromptResponseType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
PromptResponseType::Prompt => PROMPT,
PromptResponseType::Yes => ASSUME_YES,
PromptResponseType::No => ASSUME_NO,
})
}
}

impl FromStr for PromptResponseType {
type Err = CliError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().trim() {
PROMPT => Ok(Self::Prompt),
ASSUME_YES => Ok(Self::Yes),
ASSUME_NO => Ok(Self::No),
_ => Err(CliError::CommandArgumentError(
"Invalid prompt response type, must be one of [yes, no, prompt]".to_string(),
)),
}
}
}

0 comments on commit cc27e67

Please sign in to comment.