use isoloom_plan::configure::{CommandPlan, FilePlan, Resource, ServicePlan, ServiceSource, UserPlan};
use isoloom_spec::shell::base64;
use isoloom_spec::syntax::{FileState, Guard, Invocation};
use super::Configured;
use crate::powershell::quote;
use crate::{GeneratedFile, header};
pub const LIBRARY: &str = include_str!("powershell/configure.ps1");
pub const LIBRARY_PATH: &str = ".isoloom/configure/lib/configure.ps1";
const PROJECT_DIR: &str = "C:\\isoloom";
const CHANGED: &str = "C:\\Windows\\Temp\\isoloom-configure.changed";
#[must_use]
pub fn library_file() -> GeneratedFile {
GeneratedFile {
path: LIBRARY_PATH.to_owned(),
contents: format!("{}{LIBRARY}", header("#")),
}
}
#[must_use]
pub fn script(c: &Configured<'_>, resources: &[Resource], fresh: bool) -> String {
let calls: String = resources.iter().filter_map(|r| call(c, r)).collect();
let mark = if fresh {
"Remove-Item -LiteralPath $env:ISOLOOM_CHANGED -Force -ErrorAction SilentlyContinue"
} else {
"Set-Content -LiteralPath $env:ISOLOOM_CHANGED -Value 'changed'"
};
let library = super::compact(LIBRARY);
format!("$ErrorActionPreference = 'Stop'\n{library}$env:ISOLOOM_CHANGED = '{CHANGED}'\n{mark}\nSet-Location -LiteralPath '{PROJECT_DIR}'\n{calls}")
}
#[must_use]
pub fn call(c: &Configured<'_>, r: &Resource) -> Option<String> {
let line = match r {
Resource::Packages(ps) => format!("Install-IsoloomFeature -Name {}", list(ps.iter().map(|p| p.name.to_string()))),
Resource::Copy(cp) => format!("Copy-IsoloomItem -From {} -To {}", quote(cp.from.as_str()), quote(cp.to.as_str())),
Resource::Template(t) => match c.template(&t.from) {
Ok(text) => format!("Install-IsoloomTemplate -To {} -Base64 '{}'", quote(t.to.as_str()), base64(text.as_bytes())),
Err(why) => format!("throw {}", quote(&format!("isoloom: {why}"))),
},
Resource::File(f) => file(f)?,
Resource::User(u) => user(u),
Resource::Group(g) => format!("Add-IsoloomGroup -Name {}", quote(g.name.as_str())),
Resource::Service(s) => service(s),
Resource::Command(cmd) => command(cmd),
Resource::Firewall(ports) => ports
.iter()
.map(|p| {
format!(
"Enable-IsoloomFirewallPort -Port {} -Protocol {}",
p.port,
if p.protocol.is_tcp() { "TCP" } else { "UDP" }
)
})
.collect::<Vec<_>>()
.join("\n"),
_ => return None,
};
(!line.is_empty()).then(|| format!("{line}\n"))
}
fn list<I: IntoIterator<Item = String>>(items: I) -> String {
items.into_iter().map(|i| quote(&i)).collect::<Vec<_>>().join(", ")
}
fn file(f: &FilePlan) -> Option<String> {
let path = quote(f.path.as_str());
Some(match &f.state {
FileState::Directory => format!("Install-IsoloomFile -Path {path} -State directory"),
FileState::Absent => format!("Install-IsoloomFile -Path {path} -State absent"),
FileState::Download { url, sha256 } => format!(
"Install-IsoloomFile -Path {path} -State download -Url {} -Sha256 '{sha256}'",
quote(url.as_str())
),
_ => return None,
})
}
fn user(u: &UserPlan) -> String {
let phrase = u.password.as_ref().map(|p| format!(" -Phrase {}", quote(p.as_str()))).unwrap_or_default();
let groups: Vec<String> = u.group.iter().chain(&u.groups).map(ToString::to_string).collect();
let groups = if groups.is_empty() {
String::new()
} else {
format!(" -Groups {}", list(groups))
};
format!("Add-IsoloomUser -Name {}{phrase}{groups}", quote(u.name.as_str()))
}
fn service(s: &ServicePlan) -> String {
let name = quote(s.unit.as_str());
let enabled = if s.enabled { "$true" } else { "$false" };
match &s.source {
ServiceSource::Defined { run, cwd, .. } => {
let dir = cwd.as_ref().map(|d| format!(" -Directory {}", quote(d.as_str()))).unwrap_or_default();
format!(
"Register-IsoloomTask -Name {name} -Run {}{dir}\nRegister-IsoloomTaskState -Name {name} -Enabled {enabled}",
list(run.args().iter().cloned())
)
}
_ => format!("Register-IsoloomService -Name {name} -Enabled {enabled}"),
}
}
fn command(cmd: &CommandPlan) -> String {
let guard = match &cmd.guard {
Guard::Creates(path) => format!(" -Creates {}", quote(path.as_str())),
Guard::Unless(check) => format!(" -Unless {}", quote(&text(check))),
_ => String::new(),
};
format!("Invoke-IsoloomCommand -Run {}{guard}", quote(&text(&cmd.run)))
}
fn text(i: &Invocation) -> String {
match i {
Invocation::Exec(a) => format!("& {}", a.args().iter().map(|w| quote(w)).collect::<Vec<_>>().join(" ")),
Invocation::Shell(t) => t.clone(),
_ => "throw 'this Isoloom cannot render that command'".to_owned(),
}
}
#[cfg(test)]
mod tests;