Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli(app/create): Ask for directory to create the app in #4883

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions lib/cli/src/commands/app/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ async fn write_app_config(app_config: &AppConfigV1, dir: Option<PathBuf>) -> 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.
Expand Down Expand Up @@ -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<PathBuf> {
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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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));
Expand Down
Loading