diff --git a/lib/cli/src/commands/app/create.rs b/lib/cli/src/commands/app/create.rs index dbaf1dd941c..6c635cc3ec6 100644 --- a/lib/cli/src/commands/app/create.rs +++ b/lib/cli/src/commands/app/create.rs @@ -32,13 +32,17 @@ async fn write_app_config(app_config: &AppConfigV1, dir: Option) -> any None => std::env::current_dir()?, }; + tokio::fs::create_dir_all(&app_dir).await?; + let app_config_path = app_dir.join(AppConfigV1::CANONICAL_FILE_NAME); - std::fs::write(&app_config_path, raw_app_config).with_context(|| { - format!( - "could not write app config to '{}'", - app_config_path.display() - ) - }) + tokio::fs::write(&app_config_path, raw_app_config) + .await + .with_context(|| { + format!( + "could not write app config to '{}'", + app_config_path.display() + ) + }) } /// Create a new Edge app. @@ -191,6 +195,34 @@ impl CmdAppCreate { crate::utils::prompts::prompt_for_namespace("Who should own this app?", None, user.as_ref()) } + async fn get_output_dir(&self, app_name: &str) -> anyhow::Result { + let mut output_path = if let Some(path) = &self.app_dir_path { + path.clone() + } else { + PathBuf::from(".").canonicalize()? + }; + + if output_path.is_dir() && output_path.read_dir()?.next().is_some() { + if self.non_interactive { + if !self.quiet { + eprintln!("The current directory is not empty."); + eprintln!("Use the `--dir` flag to specify another directory, or remove files from the currently selected one.") + } + anyhow::bail!("Stopping as the directory is not empty") + } else { + let theme = ColorfulTheme::default(); + let raw: String = dialoguer::Input::with_theme(&theme) + .with_prompt("Select the directory to save the app in") + .with_initial_text(app_name) + .interact_text() + .context("could not read user input")?; + output_path = PathBuf::from_str(&raw)? + } + } + + Ok(output_path) + } + async fn create_from_local_manifest( &self, owner: &str, @@ -247,9 +279,11 @@ impl CmdAppCreate { return Ok(false); } + let output_path = self.get_output_dir(app_name).await?; + if let Some(pkg) = &self.package { let app_config = self.get_app_config(owner, app_name, pkg); - write_app_config(&app_config, self.app_dir_path.clone()).await?; + write_app_config(&app_config, Some(output_path.clone())).await?; self.try_deploy(owner, app_name).await?; return Ok(true); } else if !self.non_interactive { @@ -266,7 +300,7 @@ impl CmdAppCreate { .await?; let app_config = self.get_app_config(owner, app_name, &package_id.to_string()); - write_app_config(&app_config, self.app_dir_path.clone()).await?; + write_app_config(&app_config, Some(output_path)).await?; self.try_deploy(owner, app_name).await?; return Ok(true); } else { @@ -474,20 +508,7 @@ impl CmdAppCreate { tracing::info!("Downloading template from url {url}"); - let output_path = if let Some(path) = &self.app_dir_path { - path.clone() - } else { - PathBuf::from(".").canonicalize()? - }; - - if output_path.is_dir() && output_path.read_dir()?.next().is_some() { - if !self.quiet { - eprintln!("The current directory is not empty."); - eprintln!("Use the `--dir` flag to specify another directory, or remove files from the currently selected one.") - } - anyhow::bail!("Stopping as the directory is not empty") - } - + let output_path = self.get_output_dir(app_name).await?; let pb = indicatif::ProgressBar::new_spinner(); pb.enable_steady_tick(std::time::Duration::from_millis(500));