Skip to content

Commit

Permalink
💫 Improve health
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixr-codes committed Apr 25, 2024
1 parent 4c92d9b commit 7b4f41a
Showing 1 changed file with 110 additions and 98 deletions.
208 changes: 110 additions & 98 deletions src/health.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Utilities for validating and fixing the project structure and data.
// TODO: cover more cases

use crate::uuid::Uuids;
use crate::{paths, uuid};
use std::fs;
Expand Down Expand Up @@ -79,116 +77,130 @@ impl Health {

pub fn check_uuids_presence(&self) -> bool {
let uuids = self.root.join(paths::uuids());
let mut data: Uuids = toml::from_str(match &fs::read_to_string(&uuids) {
let content = match fs::read_to_string(&uuids) {
Ok(data) => data,
Err(e) => {
log::error!("Failed to read UUID file: {}", e);
return false;
// FIXME: if `fix`, create new UUID file
}
})
.expect("invalid TOML"); // TODO: handle errors
let mut modified = false;
let mut ok = true;

if has_content(&self.root.join(paths::src_bp())) {
if data.bp.header.is_none() {
if self.fix {
log::info!("Add missing BP header UUID");
data.bp.update_header(None);
modified = true;
} else {
log::error!("BP header UUID is missing; try `allay health --fix`");
ok = false;
}
}
if data.bp.module.is_none() {
if self.fix {
log::info!("Add missing BP module UUID");
data.bp.update_module(None);
modified = true;
} else {
log::error!("BP module UUID is missing; try `allay health --fix`");
ok = false;
}
}
}
if has_content(&self.root.join(paths::src_rp())) {
if data.rp.header.is_none() {
if self.fix {
log::info!("Add missing RP header UUID");
data.rp.update_header(None);
modified = true;
let data = uuid::Uuids::default();
let content = data.to_string();
fs::write(&uuids, &content).expect("failed to fill UUIDs");
content
} else {
log::error!("RP header UUID is missing; try `allay health --fix`");
ok = false;
return false;
}
}
if data.rp.module.is_none() {
if self.fix {
log::info!("Add missing RP module UUID");
data.bp.update_module(None);
modified = true;
} else {
log::error!("RP module UUID is missing; try `allay health --fix`");
ok = false;
}
};
let data: Result<Uuids, _> = toml::from_str(&content);
match data {
Err(e) => {
log::error!("Invalid TOML: {}", e);
return false;
}
}
if has_content(&self.root.join(paths::src_sp())) {
if data.sp.header.is_none() {
if self.fix {
log::info!("Add missing SP header UUID");
data.sp.update_header(None);
modified = true;
} else {
log::error!("BP header UUID is missing; try `allay health --fix`");
ok = false;
Ok(mut data) => {
let mut modified = false;
let mut ok = true;

if has_content(&self.root.join(paths::src_bp())) {
if data.bp.header.is_none() {
if self.fix {
log::info!("Add missing BP header UUID");
data.bp.update_header(None);
modified = true;
} else {
log::error!("BP header UUID is missing; try `allay health --fix`");
ok = false;
}
}
if data.bp.module.is_none() {
if self.fix {
log::info!("Add missing BP module UUID");
data.bp.update_module(None);
modified = true;
} else {
log::error!("BP module UUID is missing; try `allay health --fix`");
ok = false;
}
}
}
}
if data.sp.module.is_none() {
if self.fix {
log::info!("Add missing SP module UUID");
data.sp.update_module(None);
modified = true;
} else {
log::error!("SP module UUID is missing; try `allay health --fix`");
ok = false;
if has_content(&self.root.join(paths::src_rp())) {
if data.rp.header.is_none() {
if self.fix {
log::info!("Add missing RP header UUID");
data.rp.update_header(None);
modified = true;
} else {
log::error!("RP header UUID is missing; try `allay health --fix`");
ok = false;
}
}
if data.rp.module.is_none() {
if self.fix {
log::info!("Add missing RP module UUID");
data.bp.update_module(None);
modified = true;
} else {
log::error!("RP module UUID is missing; try `allay health --fix`");
ok = false;
}
}
}
}
}
if has_content(&self.root.join(paths::src_wt())) {
if data.wt.header.is_none() {
if self.fix {
log::info!("Add missing WT header UUID");
data.wt.update_header(None);
modified = true;
} else {
log::error!("WT header UUID is missing; try `allay health --fix`");
ok = false;
if has_content(&self.root.join(paths::src_sp())) {
if data.sp.header.is_none() {
if self.fix {
log::info!("Add missing SP header UUID");
data.sp.update_header(None);
modified = true;
} else {
log::error!("BP header UUID is missing; try `allay health --fix`");
ok = false;
}
}
if data.sp.module.is_none() {
if self.fix {
log::info!("Add missing SP module UUID");
data.sp.update_module(None);
modified = true;
} else {
log::error!("SP module UUID is missing; try `allay health --fix`");
ok = false;
}
}
}
}
if data.wt.module.is_none() {
if self.fix {
log::info!("Add missing WT module UUID");
data.wt.update_module(None);
modified = true;
} else {
log::error!("WT module UUID is missing; try `allay health --fix`");
ok = false;
if has_content(&self.root.join(paths::src_wt())) {
if data.wt.header.is_none() {
if self.fix {
log::info!("Add missing WT header UUID");
data.wt.update_header(None);
modified = true;
} else {
log::error!("WT header UUID is missing; try `allay health --fix`");
ok = false;
}
}
if data.wt.module.is_none() {
if self.fix {
log::info!("Add missing WT module UUID");
data.wt.update_module(None);
modified = true;
} else {
log::error!("WT module UUID is missing; try `allay health --fix`");
ok = false;
}
}
}
}
}

if modified {
// write back to UUID file
if let Err(e) = fs::write(&uuids, data.to_string()) {
log::error!("Error while writing to UUID file: {}", e);
ok = false;
};
};
if modified {
// write back to UUID file
if let Err(e) = fs::write(&uuids, data.to_string()) {
log::error!("Error while writing to UUID file: {}", e);
ok = false;
};
};

ok
ok
}
}
}
}

Expand Down

0 comments on commit 7b4f41a

Please sign in to comment.