Skip to content

Commit

Permalink
Delete machine config if init failed (fixes #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
htngr committed Sep 16, 2024
1 parent 7244d02 commit d8a0acf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
5 changes: 5 additions & 0 deletions codchi/src/config/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ impl MachineConfig {
machines.sort_by(|a, b| a.name.cmp(&b.name));
Ok(machines)
}

pub fn delete(name: &str) {
let path = host::DIR_CONFIG.join_machine(name).join("config.json");
path.remove()
}
}

pub type CodchiModule = FlakeUrl<Required>;
Expand Down
55 changes: 30 additions & 25 deletions codchi/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::cli::{InputOptions, ModuleAttrPath, ModuleName, NixpkgsLocation};
use crate::config::git_url::{GitUrl, Scheme};
use crate::config::*;
use crate::consts;
use crate::logging::set_progress_status;
use crate::platform::{nix::NixDriver, *};
use crate::progress_scope;
use crate::util::{Empty, Required, StringExt};
use crate::consts;
use crate::config::*;
use anyhow::{anyhow, bail, Context, Result};
use inquire::{list_option::ListOption, validator::Validation};
use lazy_regex::regex_is_match;
Expand All @@ -29,31 +29,36 @@ pub fn init(
}
ConfigResult::None => {}
}
let (lock, _) = MachineConfig::open(machine_name, true)?;
if *opts != InputOptions::default() && url.is_none() {
bail!("<URL> is missing.");
}

let empty = MachineConfig::new(machine_name);
let cfg = match url {
None => empty,
Some(url) => {
let (modules, use_nixpkgs) =
fetch_modules(&empty, &url, opts, module_paths, true, false)?;

MachineConfig {
name: machine_name.to_owned(),
nixpkgs_from: match use_nixpkgs {
UseNixpkgs::Remote(module) => Some(module),
_else => None,
},
modules,
secrets: Default::default(),
}
let cfg = (|| {
let (lock, _) = MachineConfig::open(machine_name, true)?;
if *opts != InputOptions::default() && url.is_none() {
bail!("<URL> is missing.");
}
};

cfg.write(lock)?;
let empty = MachineConfig::new(machine_name);
let cfg = match url {
None => empty,
Some(url) => {
let (modules, use_nixpkgs) =
fetch_modules(&empty, &url, opts, module_paths, true, false)?;

MachineConfig {
name: machine_name.to_owned(),
nixpkgs_from: match use_nixpkgs {
UseNixpkgs::Remote(module) => Some(module),
_else => None,
},
modules,
secrets: Default::default(),
}
}
};
cfg.write(lock)?;
Ok(cfg)
})()
.inspect_err(|_| {
MachineConfig::delete(machine_name);
})?;

let machine = Machine {
config: cfg,
Expand Down

0 comments on commit d8a0acf

Please sign in to comment.