Skip to content

Commit

Permalink
init: Bail if dir exist, at very beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Oct 14, 2024
1 parent 40a59ce commit 38663c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
26 changes: 19 additions & 7 deletions crates/omnix-cli/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,36 @@ pub struct InitCommand {

impl InitCommand {
pub async fn run(&self) -> anyhow::Result<()> {
// Prompt from builtin registry if the user has not specified one.
let flake = match self.flake {
Some(ref flake) => flake,
None => &omnix_init::core::select_from_registry().await?,
};
if self.test {
let cfg = NixConfig::get().await.as_ref()?;
omnix_init::core::run_tests(&cfg.system.value, flake).await?;
omnix_init::core::run_tests(&cfg.system.value, &self.registry_choose().await?).await?;
} else {
let path = self.path.as_ref().unwrap(); // unwrap is okay, because of `required_unless_present`
if path.exists() {
// Make sure that the directory does not already exist. We don't risk mutating accidentally incorrect location!
anyhow::bail!("Output directory already exists: {}", path.display());
}

let params = self
.params
.as_ref()
.map_or_else(HashMap::new, |hm| hm.0.clone());
omnix_init::core::run(path, flake, &params, self.non_interactive).await?;
omnix_init::core::run(
path,
&self.registry_choose().await?,
&params,
self.non_interactive,
)
.await?;
}
Ok(())
}
async fn registry_choose<'a>(&self) -> anyhow::Result<FlakeUrl> {
match self.flake {
Some(ref flake) => Ok(flake.clone()),
None => omnix_init::core::select_from_registry().await,
}
}
}

/// A map of parameter values
Expand Down
5 changes: 0 additions & 5 deletions crates/omnix-init/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ pub struct NixTemplate {
impl Template {
// Scaffold the [Template] at the given path.
pub async fn scaffold_at(&self, out_dir: &Path) -> anyhow::Result<()> {
// Make sure that the directory does not already exist. We don't risk mutating accidentally incorrect location!
if out_dir.exists() {
anyhow::bail!("Output directory already exists: {}", out_dir.display());
}

// Recursively copy the self.template.path to the output directory
omnix_common::fs::copy_dir_all(&self.template.path, out_dir)
.await
Expand Down

0 comments on commit 38663c0

Please sign in to comment.