use crate::context::ExecContext;
use anyhow::{bail, Context, Result};
use std::path::Path;
use std::process::Stdio;
pub fn sync(ctx: &ExecContext, bin: &Path, ws: &Path) -> Result<Vec<(String, String)>> {
let file = ws.join(".env.portool");
let _ = std::fs::remove_file(&file);
let out = ctx
.command(bin)
.args(["sync", "--quiet"])
.current_dir(ws)
.stdin(Stdio::null())
.output()
.context("cannot run portool")?;
if !out.status.success() {
bail!(
"portool sync: {}",
String::from_utf8_lossy(&out.stderr).trim()
);
}
let text = std::fs::read_to_string(&file).context("portool wrote no .env.portool")?;
Ok(parse(&text))
}
pub fn parse(text: &str) -> Vec<(String, String)> {
text.lines()
.map(str::trim)
.filter(|l| !l.is_empty() && !l.starts_with('#'))
.filter_map(|l| l.split_once('='))
.map(|(k, v)| (k.trim().to_string(), v.trim().to_string()))
.collect()
}
pub fn release(ctx: &ExecContext, bin: &Path, ws: &Path) {
if let Err(error) = release_checked(ctx, bin, ws) {
eprintln!("local-ci: warning: port release incomplete: {error:#}");
}
}
pub(crate) fn release_checked(ctx: &ExecContext, bin: &Path, ws: &Path) -> Result<()> {
let mut command = ctx.command(bin);
command.arg("release").current_dir(ws);
crate::cleanup::bounded_command(ctx, command, std::time::Duration::from_secs(5), false)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn env_file() {
let v = parse("# generated by portool\n# block: 1-5\nAPP_PORT=4100\n\nTEST_PORT = 4101\n");
assert_eq!(
v,
vec![
("APP_PORT".into(), "4100".into()),
("TEST_PORT".into(), "4101".into())
]
);
}
}