Files
ah/src/packages/sync.rs

56 lines
1.4 KiB
Rust

use colored::Colorize;
use std::io::Write;
use std::process::{Command, Stdio};
use crate::file;
use crate::packages::{ask_confirmation, get_package_path, PACKAGE_MANAGER};
pub fn sync(noconfirm: bool) -> Result<(), Box<dyn std::error::Error>> {
println!("{} {}", "::".bold().green(), "Syncing packages...".bold());
if !ask_confirmation()? {
return Err("Operation aborted".into());
}
let packages = file::read_packages(get_package_path());
let packages = packages
.into_iter()
.filter(|p| !p.contains("#") && !p.is_empty())
.collect::<Vec<String>>();
let noconfirm = if noconfirm {
"--noconfirm"
} else {
"--confirm"
};
let mut child = Command::new(PACKAGE_MANAGER)
.arg("--color")
.arg("always")
.arg("-S")
.arg("--needed")
.arg(noconfirm)
.arg("-")
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute command");
if let Some(mut stdin) = child.stdin.take() {
for package in packages {
writeln!(stdin, "{}", package).unwrap();
}
}
let status = child.wait().expect("Failed to wait on child");
if !status.success() {
return Err("Failed to sync packages".into());
}
println!("{} {}", "::".bold().green(), "Packages synced".bold());
Ok(())
}