From cdfa7aaf13c1b69f359dce6a03d98938e380699c Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 12 Dec 2024 12:54:09 -0600 Subject: [PATCH] Fix spacetime server clear (#2055) --- crates/cli/src/lib.rs | 2 +- crates/cli/src/subcommands/server.rs | 27 ++++++++++++---------- crates/standalone/src/subcommands/start.rs | 9 -------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index d015348b3d..eba684248d 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -64,7 +64,7 @@ pub async fn exec_subcommand( "list" => list::exec(config, args).await, "init" => init::exec(config, args).await, "build" => build::exec(config, args).await.map(drop), - "server" => server::exec(config, args).await, + "server" => server::exec(config, paths, args).await, "subscribe" => subscribe::exec(config, args).await, #[cfg(feature = "standalone")] "start" => start::exec(Some(paths), args).await, diff --git a/crates/cli/src/subcommands/server.rs b/crates/cli/src/subcommands/server.rs index 4df0c7d6d1..c50e47b2b9 100644 --- a/crates/cli/src/subcommands/server.rs +++ b/crates/cli/src/subcommands/server.rs @@ -5,8 +5,7 @@ use crate::{ }; use anyhow::Context; use clap::{Arg, ArgAction, ArgMatches, Command}; -use spacetimedb_standalone::subcommands::start::default_data_dir; -use std::path::PathBuf; +use spacetimedb_paths::{server::ServerDataDir, SpacetimePaths}; use tabled::{ settings::{object::Columns, Alignment, Modify, Style}, Table, Tabled, @@ -104,21 +103,25 @@ fn get_subcommands() -> Vec { .arg( Arg::new("data_dir") .long("data-dir") - .help("The path to the data directory for the database") - .default_value(default_data_dir().into_os_string()) - .value_parser(clap::value_parser!(PathBuf)), + .help("The path to the server data directory to clear [default: that of the selected spacetime instance]") + .value_parser(clap::value_parser!(ServerDataDir)), ) .arg(common_args::yes()), // TODO: set-name, set-protocol, set-host, set-url ] } -pub async fn exec(config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> { +pub async fn exec(config: Config, paths: &SpacetimePaths, args: &ArgMatches) -> Result<(), anyhow::Error> { let (cmd, subcommand_args) = args.subcommand().expect("Subcommand required"); - exec_subcommand(config, cmd, subcommand_args).await + exec_subcommand(config, paths, cmd, subcommand_args).await } -async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Result<(), anyhow::Error> { +async fn exec_subcommand( + config: Config, + paths: &SpacetimePaths, + cmd: &str, + args: &ArgMatches, +) -> Result<(), anyhow::Error> { match cmd { "list" => exec_list(config, args).await, "set-default" => exec_set_default(config, args).await, @@ -127,7 +130,7 @@ async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Result "fingerprint" => exec_fingerprint(config, args).await, "ping" => exec_ping(config, args).await, "edit" => exec_edit(config, args).await, - "clear" => exec_clear(config, args).await, + "clear" => exec_clear(config, paths, args).await, unknown => Err(anyhow::anyhow!("Invalid subcommand: {}", unknown)), } } @@ -357,11 +360,11 @@ pub async fn exec_edit(mut config: Config, args: &ArgMatches) -> Result<(), anyh Ok(()) } -async fn exec_clear(_config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> { +async fn exec_clear(_config: Config, paths: &SpacetimePaths, args: &ArgMatches) -> Result<(), anyhow::Error> { let force = args.get_flag("force"); - let data_dir = args.get_one::("data_dir").unwrap(); + let data_dir = args.get_one::("data_dir").unwrap_or(&paths.data_dir); - if data_dir.exists() { + if data_dir.0.exists() { println!("Database path: {}", data_dir.display()); if !y_or_n( diff --git a/crates/standalone/src/subcommands/start.rs b/crates/standalone/src/subcommands/start.rs index 7c0566e353..f084747bf4 100644 --- a/crates/standalone/src/subcommands/start.rs +++ b/crates/standalone/src/subcommands/start.rs @@ -1,4 +1,3 @@ -use std::path::PathBuf; use std::sync::Arc; use crate::routes::router; @@ -56,14 +55,6 @@ impl ProgramMode { } } -pub fn default_data_dir() -> PathBuf { - dirs::data_local_dir().unwrap().join(if cfg!(windows) { - "SpacetimeDB/data" - } else { - "spacetime/data" - }) -} - pub fn cli(mode: ProgramMode) -> clap::Command { let jwt_pub_key_path_arg = Arg::new("jwt_pub_key_path") .long("jwt-pub-key-path")