Skip to content

Commit

Permalink
CLI - Unify copies of the confirmation/--force logic (#1740)
Browse files Browse the repository at this point in the history
Co-authored-by: Zeke Foppa <bfops@users.noreply.github.com>
  • Loading branch information
bfops and bfops authored Sep 24, 2024
1 parent 7478737 commit f559f0a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 60 deletions.
14 changes: 2 additions & 12 deletions crates/cli/src/subcommands/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use spacetimedb_schema::def::{ModuleDef, ReducerDef, ScopedTypeName, TableDef, T
use spacetimedb_schema::identifier::Identifier;
use spacetimedb_schema::schema::TableSchema;
use std::fs;
use std::io::Write;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use wasmtime::{Caller, StoreContextMut};

use crate::util::y_or_n;
use crate::Config;

mod code_indenter;
Expand Down Expand Up @@ -190,22 +190,12 @@ pub fn exec(_config: Config, args: &clap::ArgMatches) -> anyhow::Result<()> {
}
}
if !files_to_delete.is_empty() {
let mut input = "y".to_string();
println!("The following files were not generated by this command and will be deleted:");
for path in &files_to_delete {
println!(" {}", path.to_str().unwrap());
}

if !force {
print!("Are you sure you want to delete these files? [y/N] ");
input = "".to_string();
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut input)?;
} else {
println!("Force flag present, deleting files without prompting.");
}

if input.trim().to_lowercase() == "y" || input.trim().to_lowercase() == "yes" {
if y_or_n(force, "Are you sure you want to delete these files?")? {
for path in files_to_delete {
fs::remove_file(path)?;
}
Expand Down
5 changes: 4 additions & 1 deletion crates/cli/src/subcommands/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ async fn exec_remove(mut config: Config, args: &ArgMatches) -> Result<(), anyhow
}

fn should_continue(force: bool, prompt: &str) -> anyhow::Result<bool> {
Ok(force || y_or_n(&format!("Are you sure you want to remove all identities{}?", prompt))?)
y_or_n(
force,
&format!("Are you sure you want to remove all identities{}?", prompt),
)
}

if let Some(identity_or_name) = identity_or_name {
Expand Down
19 changes: 7 additions & 12 deletions crates/cli/src/subcommands/local.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::config::Config;
use crate::util::y_or_n;
use clap::ArgAction::SetTrue;
use clap::ArgMatches;
use clap::{Arg, Command};
use spacetimedb::stdb_path;
use std::io::Write;
use std::path::PathBuf;

pub fn cli() -> Command {
Expand Down Expand Up @@ -60,17 +60,12 @@ async fn exec_clear(_config: Config, args: &ArgMatches) -> Result<(), anyhow::Er
println!("Worker node database path: <not found>");
}

if !force {
print!("Are you sure you want to delete all data from the local database? (y/n) ");
std::io::stdout().flush()?;
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
if input.trim().to_lowercase() != "y" && input.trim().to_lowercase() != "yes" {
println!("Aborting");
return Ok(());
}
} else {
println!("Force flag is present, skipping confirmation");
if !y_or_n(
force,
"Are you sure you want to delete all data from the local database?",
)? {
println!("Aborting");
return Ok(());
}

if control_node_dir.exists() {
Expand Down
35 changes: 15 additions & 20 deletions crates/cli/src/subcommands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use reqwest::{StatusCode, Url};
use spacetimedb_client_api_messages::name::PublishOp;
use spacetimedb_client_api_messages::name::{is_address, parse_domain_name, PublishResult};
use std::fs;
use std::io::Write;
use std::path::PathBuf;

use crate::common_args;
use crate::config::Config;
use crate::util::unauth_error_context;
use crate::util::{add_auth_header_opt, get_auth_header};
use crate::util::{unauth_error_context, y_or_n};

pub fn cli() -> clap::Command {
clap::Command::new("publish")
Expand Down Expand Up @@ -166,25 +165,21 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
);

if clear_database {
if force {
println!("Skipping confirmation due to --force.");
} else {
// Note: `name_or_address` should be set, because it is `required` in the CLI arg config.
println!(
"This will DESTROY the current {} module, and ALL corresponding data.",
// Note: `name_or_address` should be set, because it is `required` in the CLI arg config.
println!(
"This will DESTROY the current {} module, and ALL corresponding data.",
name_or_address.unwrap()
);
if !y_or_n(
force,
format!(
"Are you sure you want to proceed? [deleting {}]",
name_or_address.unwrap()
);
print!(
"Are you sure you want to proceed? (y/N) [deleting {}] ",
name_or_address.unwrap()
);
std::io::stdout().flush()?;
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
if input.trim().to_lowercase() != "y" && input.trim().to_lowercase() != "yes" {
println!("Aborting");
return Ok(());
}
)
.as_str(),
)? {
println!("Aborting");
return Ok(());
}
query_params.push(("clear", "true"));
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/subcommands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub async fn exec_remove(mut config: Config, args: &ArgMatches) -> Result<(), an
for id in deleted_ids {
println!("{}", id.identity);
}
if !(force || y_or_n("Continue?")?) {
if !y_or_n(force, "Continue?")? {
anyhow::bail!("Aborted");
}

Expand Down Expand Up @@ -339,7 +339,7 @@ pub async fn exec_fingerprint(mut config: Config, args: &ArgMatches) -> Result<(
let force = args.get_flag("force");

if update_server_fingerprint(&mut config, server, delete_identities).await? {
if !(force || y_or_n("Continue?")?) {
if !y_or_n(force, "Continue?")? {
anyhow::bail!("Aborted");
}

Expand Down Expand Up @@ -412,7 +412,7 @@ pub async fn exec_edit(mut config: Config, args: &ArgMatches) -> Result<(), anyh
}
}

if !(force || y_or_n("Continue?")?) {
if !y_or_n(force, "Continue?")? {
anyhow::bail!("Aborted");
}

Expand Down
12 changes: 4 additions & 8 deletions crates/cli/src/subcommands/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{env, fs};

extern crate regex;

use crate::util::y_or_n;
use crate::{version, Config};
use clap::{Arg, ArgMatches};
use flate2::read::GzDecoder;
Expand Down Expand Up @@ -152,10 +153,8 @@ pub async fn exec(_config: Config, args: &ArgMatches) -> Result<(), anyhow::Erro

if release_version == version::CLI_VERSION {
println!("You're already running the latest version: {}", version::CLI_VERSION);
if !force {
if !y_or_n(force, "Do you want to reinstall? ")? {
return Ok(());
} else {
println!("Force flag is set, continuing with upgrade.");
}
}

Expand All @@ -177,11 +176,8 @@ pub async fn exec(_config: Config, args: &ArgMatches) -> Result<(), anyhow::Erro
"This will replace the current executable at {}.",
current_exe_path.display()
);
print!("Do you want to continue? [y/N] ");
std::io::stdout().flush()?;
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
if input.trim().to_lowercase() != "y" && input.trim().to_lowercase() != "yes" {

if !y_or_n(force, "Do you want to continue?")? {
println!("Aborting upgrade.");
return Ok(());
}
Expand Down
12 changes: 8 additions & 4 deletions crates/cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,17 @@ pub fn host_or_url_to_host_and_protocol(host_or_url: &str) -> (&str, Option<&str
/// Prompt the user for `y` or `n` from stdin.
///
/// Return `false` unless the input is `y`.
pub fn y_or_n(prompt: &str) -> anyhow::Result<bool> {
pub fn y_or_n(force: bool, prompt: &str) -> anyhow::Result<bool> {
if force {
println!("Force flag is present, skipping confirmation");
return Ok(true);
}
let mut input = String::new();
print!("{} (y/n)", prompt);
print!("{} [y/N]", prompt);
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut input)?;

Ok(input.trim() == "y")
let input = input.trim().to_lowercase();
Ok(input == "y" || input == "yes")
}

pub fn unauth_error_context<T>(res: anyhow::Result<T>, identity: &str, server: &str) -> anyhow::Result<T> {
Expand Down

2 comments on commit f559f0a

@github-actions
Copy link

@github-actions github-actions bot commented on f559f0a Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

@github-actions github-actions bot commented on f559f0a Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callgrind benchmark results

Callgrind Benchmark Report

These benchmarks were run using callgrind,
an instruction-level profiler. They allow comparisons between sqlite (sqlite), SpacetimeDB running through a module (stdb_module), and the underlying SpacetimeDB data storage engine (stdb_raw). Callgrind emulates a CPU to collect the below estimates.

Measurement changes larger than five percent are in bold.

In-memory benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 5380 5380 0.00% 5414 5414 0.00%
sqlite 5555 5555 0.00% 5971 5971 0.00%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 2 string 119003 119003 0.00% 119609 119609 0.00%
stdb_raw u32_u64_str no_index 64 128 1 u64 75374 75374 0.00% 75856 75856 0.00%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 22627 22611 0.07% 23133 23125 0.03%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 21578 21578 0.00% 22024 22020 0.02%
sqlite u32_u64_str no_index 64 128 2 string 144677 144677 0.00% 146229 146225 0.00%
sqlite u32_u64_str no_index 64 128 1 u64 124036 124036 0.00% 125350 125354 -0.00%
sqlite u32_u64_str btree_each_column 64 128 2 string 134476 134476 0.00% 136060 136072 -0.01%
sqlite u32_u64_str btree_each_column 64 128 1 u64 131343 131343 0.00% 132759 132751 0.01%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 898602 898789 -0.02% 952190 952345 -0.02%
stdb_raw u32_u64_str btree_each_column 64 128 1053582 1054374 -0.08% 1120106 1120610 -0.04%
sqlite u32_u64_str unique_0 64 128 398292 398292 0.00% 414946 414946 0.00%
sqlite u32_u64_str btree_each_column 64 128 983609 983609 0.00% 1023485 1023477 0.00%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 152676 152676 0.00% 152806 152806 0.00%
stdb_raw u32_u64_str unique_0 64 15701 15701 0.00% 15827 15827 0.00%
sqlite u32_u64_str unique_0 1024 1068223 1068223 0.00% 1071455 1071455 0.00%
sqlite u32_u64_str unique_0 64 76209 76209 0.00% 77179 77179 0.00%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47182 47182 0.00% 49800 49800 0.00%
64 bsatn 25716 25716 0.00% 27994 27994 0.00%
16 json 12078 12078 0.00% 13948 13948 0.00%
16 bsatn 8117 8117 0.00% 9477 9477 0.00%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 20576401 20570835 0.03% 21398631 21393741 0.02%
stdb_raw u32_u64_str unique_0 64 128 1296651 1296801 -0.01% 1383943 1384173 -0.02%
sqlite u32_u64_str unique_0 1024 1024 1802024 1802006 0.00% 1811006 1810976 0.00%
sqlite u32_u64_str unique_0 64 128 128352 128352 0.00% 131010 131010 0.00%
On-disk benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 5390 5390 0.00% 5428 5428 0.00%
sqlite 5613 5613 0.00% 6157 6157 0.00%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 2 string 117924 117924 0.00% 118574 118574 0.00%
stdb_raw u32_u64_str no_index 64 128 1 u64 75384 75384 0.00% 75894 75894 0.00%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 22625 22621 0.02% 23131 23127 0.02%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 21588 21588 0.00% 22054 22054 0.00%
sqlite u32_u64_str no_index 64 128 2 string 146598 146598 0.00% 148426 148430 -0.00%
sqlite u32_u64_str no_index 64 128 1 u64 125947 125947 0.00% 127499 127495 0.00%
sqlite u32_u64_str btree_each_column 64 128 2 string 136598 136598 0.00% 138780 138784 -0.00%
sqlite u32_u64_str btree_each_column 64 128 1 u64 133439 133439 0.00% 135353 135357 -0.00%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 847934 847766 0.02% 870056 869924 0.02%
stdb_raw u32_u64_str btree_each_column 64 128 997750 995067 0.27% 1063638 1029549 3.31%
sqlite u32_u64_str unique_0 64 128 415829 415829 0.00% 431839 431839 0.00%
sqlite u32_u64_str btree_each_column 64 128 1021870 1021870 0.00% 1061162 1061170 -0.00%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 152686 152686 0.00% 152812 152812 0.00%
stdb_raw u32_u64_str unique_0 64 15711 15711 0.00% 15837 15837 0.00%
sqlite u32_u64_str unique_0 1024 1071291 1071291 0.00% 1075017 1075017 0.00%
sqlite u32_u64_str unique_0 64 77981 77981 0.00% 79295 79295 0.00%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47182 47182 0.00% 49800 49800 0.00%
64 bsatn 25716 25716 0.00% 27994 27994 0.00%
16 json 12078 12078 0.00% 13948 13948 0.00%
16 bsatn 8117 8117 0.00% 9477 9477 0.00%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 19295738 19295576 0.00% 20177176 20177022 0.00%
stdb_raw u32_u64_str unique_0 64 128 1253239 1253354 -0.01% 1340315 1340484 -0.01%
sqlite u32_u64_str unique_0 1024 1024 1809567 1809567 0.00% 1818225 1818225 0.00%
sqlite u32_u64_str unique_0 64 128 132478 132517 -0.03% 135432 135475 -0.03%

Please sign in to comment.