use crate::core::types::Resource;
pub fn check_script(resource: &Resource) -> String {
let target = resource.path.as_deref().unwrap_or("/mnt/unknown");
format!(
"mountpoint -q '{target}' 2>/dev/null && echo 'mounted:{target}' || echo 'unmounted:{target}'"
)
}
pub fn apply_script(resource: &Resource) -> String {
let source = resource.source.as_deref().unwrap_or("none");
let target = resource.path.as_deref().unwrap_or("/mnt/unknown");
let fstype = resource.fs_type.as_deref().unwrap_or("auto");
let options = resource.options.as_deref().unwrap_or("defaults");
let state = resource.state.as_deref().unwrap_or("mounted");
let mut lines = vec!["set -euo pipefail".to_string()];
match state {
"mounted" => {
lines.push(format!("mkdir -p '{target}'"));
lines.push(format!(
"if ! mountpoint -q '{target}'; then\n mount -t '{fstype}' -o '{options}' '{source}' '{target}'\nfi"
));
lines.push(format!(
"if ! grep -q '{target}' /etc/fstab 2>/dev/null; then\n echo '{source} {target} {fstype} {options} 0 0' >> /etc/fstab\nfi"
));
}
"unmounted" => {
lines.push(format!(
"if mountpoint -q '{target}'; then\n umount '{target}'\nfi"
));
}
"absent" => {
lines.push(format!(
"if mountpoint -q '{target}'; then\n umount '{target}'\nfi"
));
lines.push(format!(
"sed -i '\\|{target}|d' /etc/fstab 2>/dev/null || true"
));
}
_ => {}
}
lines.join("\n")
}
pub fn state_query_script(resource: &Resource) -> String {
let target = resource.path.as_deref().unwrap_or("/mnt/unknown");
format!(
"if mountpoint -q '{target}'; then\n\
findmnt -n -o SOURCE,FSTYPE,OPTIONS '{target}' 2>/dev/null\n\
else\n\
echo 'UNMOUNTED'\n\
fi"
)
}