Skip to content

Commit

Permalink
[aptos-cli] Add assume-no prompt option
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed May 4, 2022
1 parent e1c41ec commit a8bd973
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
9 changes: 6 additions & 3 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,14 @@ impl FromStr for EncodingType {
}

/// An insertable option for use with prompts.
#[derive(Debug, Parser)]
#[derive(Clone, Copy, Debug, Parser)]
pub struct PromptOptions {
/// Assume yes for all yes/no prompts
#[clap(long)]
#[clap(long, group = "prompt_options")]
pub assume_yes: bool,
/// Assume no for all yes/no prompts
#[clap(long, group = "prompt_options")]
pub assume_no: bool,
}

/// An insertable option for use with encodings.
Expand Down Expand Up @@ -407,7 +410,7 @@ pub struct SaveFile {
impl SaveFile {
/// Check if the key file exists already
pub fn check_file(&self) -> CliTypedResult<()> {
check_if_file_exists(self.output_file.as_path(), self.prompt_options.assume_yes)
check_if_file_exists(self.output_file.as_path(), self.prompt_options)
}

/// Save to the `output_file`
Expand Down
26 changes: 14 additions & 12 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
common::types::{CliError, CliTypedResult},
common::types::{CliError, CliTypedResult, PromptOptions},
CliResult,
};
use aptos_crypto::{ed25519::Ed25519PrivateKey, PrivateKey};
Expand Down Expand Up @@ -149,17 +149,19 @@ impl<T> From<CliTypedResult<T>> for ResultWrapper<T> {
}
}

/// Checks if a file exists, being overridden by `--assume-yes`
pub fn check_if_file_exists(file: &Path, assume_yes: bool) -> CliTypedResult<()> {
if file.exists()
&& !assume_yes
&& !prompt_yes(
format!(
"{:?} already exists, are you sure you want to overwrite it?",
file.as_os_str()
)
.as_str(),
)
/// Checks if a file exists, being overridden by `PromptOptions`
pub fn check_if_file_exists(file: &Path, prompt_options: PromptOptions) -> CliTypedResult<()> {
let exists = file.exists();
if (exists && prompt_options.assume_no)
|| (exists
&& !prompt_options.assume_yes
&& !prompt_yes(
format!(
"{:?} already exists, are you sure you want to overwrite it?",
file.as_os_str()
)
.as_str(),
))
{
Err(CliError::AbortedError)
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/aptos/src/move_tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl CliCommand<()> for InitPackage {

async fn execute(self) -> CliTypedResult<()> {
let move_toml = self.package_dir.join(SourcePackageLayout::Manifest.path());
check_if_file_exists(move_toml.as_path(), self.prompt_options.assume_yes)?;
check_if_file_exists(move_toml.as_path(), self.prompt_options)?;
create_dir_all(self.package_dir.join(SourcePackageLayout::Sources.path())).map_err(
|err| {
CliError::IO(
Expand Down
5 changes: 1 addition & 4 deletions crates/aptos/src/op/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ impl SaveKey {
pub fn check_key_file(&self) -> CliTypedResult<()> {
// Check if file already exists
self.file_options.check_file()?;
check_if_file_exists(
&self.public_key_file()?,
self.file_options.prompt_options.assume_yes,
)
check_if_file_exists(&self.public_key_file()?, self.file_options.prompt_options)
}

/// Saves a key to a file encoded in a string
Expand Down

0 comments on commit a8bd973

Please sign in to comment.