Skip to content

Commit

Permalink
Don't drop temp working dir too early
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed Aug 25, 2023
1 parent 2e59a74 commit e3ab6af
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/commands/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ impl UpCommand {
}
}

let working_dir = self.get_canonical_working_dir()?;
// Get working dir holder and hold on to it for the rest of the function.
// If the working dir is a temporary dir it will be deleted on drop.
let working_dir_holder = self.get_canonical_working_dir()?;
let working_dir = working_dir_holder
.path()
.canonicalize()
.context("Could not canonicalize working directory")?;

let mut locked_app = match &app_source {
AppSource::None => bail!("Internal error - should have shown help"),
Expand Down Expand Up @@ -153,20 +159,20 @@ impl UpCommand {
self.run_trigger(trigger_cmd, Some(run_opts)).await
}

fn get_canonical_working_dir(&self) -> Result<PathBuf, anyhow::Error> {
fn get_canonical_working_dir(&self) -> Result<WorkingDirectory, anyhow::Error> {
let working_dir_holder = match &self.tmp {
None => WorkingDirectory::Temporary(TempDir::with_prefix("spinup-")?),
Some(d) => WorkingDirectory::Given(d.to_owned()),
};
if !working_dir_holder.path().exists() {
std::fs::create_dir_all(working_dir_holder.path())
.context("Could not create working directory")?;
std::fs::create_dir_all(working_dir_holder.path()).with_context(|| {
format!(
"Could not create working directory '{}'",
working_dir_holder.path().display()
)
})?;
}
let working_dir = working_dir_holder
.path()
.canonicalize()
.context("Could not canonicalize working directory")?;
Ok(working_dir)
Ok(working_dir_holder)
}

async fn run_trigger(
Expand Down

0 comments on commit e3ab6af

Please sign in to comment.