initial commit
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
use clap::Command;
|
||||
pub(crate) fn get_arg_parser() -> Command<'static> {
|
||||
let result = Command::new("pacdef")
|
||||
.about("declarative package manager for Arch Linux")
|
||||
.version("1.0.0-alpha")
|
||||
.subcommand_required(true)
|
||||
.arg_required_else_help(true)
|
||||
.subcommand(Command::new("sync").about("install packages from all imported groups"));
|
||||
result
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
use crate::package::Package;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::hash::Hash;
|
||||
use std::io::BufRead;
|
||||
use std::{io::BufReader, path::PathBuf};
|
||||
|
||||
const GROUPS_DIR: &str = "/home/ratajc72/.config/pacdef/groups";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Group {
|
||||
pub(crate) name: String,
|
||||
pub(crate) packages: HashSet<Package>,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
pub(crate) fn load_from_dir() -> HashSet<Self> {
|
||||
let mut result = HashSet::new();
|
||||
let path = PathBuf::from(GROUPS_DIR);
|
||||
for entry in path.read_dir().unwrap() {
|
||||
let file = entry.unwrap();
|
||||
let name = file.file_name();
|
||||
let f = File::open(file.path()).unwrap();
|
||||
let reader = BufReader::new(f);
|
||||
|
||||
let packages = Package::from_lines(reader.lines());
|
||||
result.insert(Group {
|
||||
name: name.into_string().unwrap(),
|
||||
packages,
|
||||
});
|
||||
|
||||
// let content = file.rea
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Group {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
match self.name.partial_cmp(&other.name) {
|
||||
Some(core::cmp::Ordering::Equal) => None,
|
||||
ord => ord,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Group {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.name.cmp(&other.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Group {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.name.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Group {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.name == other.name && self.packages == other.packages
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Group {
|
||||
fn assert_receiver_is_total_eq(&self) {}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
use std::io::BufRead;
|
||||
use std::io::Write;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::process::exit;
|
||||
use std::{collections::HashSet, process::Command};
|
||||
|
||||
use alpm::Alpm;
|
||||
use args::get_arg_parser;
|
||||
use group::Group;
|
||||
use package::Package;
|
||||
|
||||
mod args;
|
||||
pub(crate) mod group;
|
||||
pub(crate) mod package;
|
||||
|
||||
fn main() {
|
||||
let args = get_arg_parser();
|
||||
let matches = args.get_matches();
|
||||
match matches.subcommand() {
|
||||
Some(("sync", _)) => install_pacdef_packages(),
|
||||
s => panic!("{:#?}", s),
|
||||
}
|
||||
}
|
||||
|
||||
fn install_pacdef_packages() {
|
||||
let groups = Group::load_from_dir();
|
||||
let packages_hs = groups
|
||||
.into_iter()
|
||||
.flat_map(|g| g.packages)
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let local_packages = convert_to_pacdef_packages(get_alpm_packages());
|
||||
let mut diff: Vec<_> = packages_hs.difference(&local_packages).collect();
|
||||
diff.sort_unstable();
|
||||
if diff.is_empty() {
|
||||
exit(0);
|
||||
}
|
||||
println!("Would install the following packages:");
|
||||
for p in &diff {
|
||||
println!(" {p}");
|
||||
}
|
||||
println!();
|
||||
get_user_confirmation();
|
||||
|
||||
install_packages(diff);
|
||||
}
|
||||
|
||||
fn get_user_confirmation() {
|
||||
print!("Continue? [Y/n] ");
|
||||
std::io::stdout().flush().unwrap();
|
||||
let reply = std::io::stdin().lock().lines().next().unwrap().unwrap();
|
||||
if !(reply.is_empty() || reply.to_lowercase().contains('y')) {
|
||||
exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_alpm_packages() -> HashSet<String> {
|
||||
let db = Alpm::new("/", "/var/lib/pacman").unwrap();
|
||||
db.localdb()
|
||||
.pkgs()
|
||||
.iter()
|
||||
.map(|p| p.name().to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
|
||||
packages.into_iter().map(Package::from).collect()
|
||||
}
|
||||
|
||||
fn install_packages(diff: Vec<&Package>) {
|
||||
let mut cmd = Command::new("paru");
|
||||
cmd.arg("-S");
|
||||
for p in diff {
|
||||
cmd.arg(format!("{p}"));
|
||||
}
|
||||
dbg!(&cmd);
|
||||
cmd.exec();
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fmt::Display;
|
||||
use std::fmt::Write;
|
||||
use std::fs::File;
|
||||
use std::hash::Hash;
|
||||
use std::io::BufReader;
|
||||
use std::io::Lines;
|
||||
|
||||
#[derive(Debug, Eq, PartialOrd, Ord)]
|
||||
pub(crate) struct Package {
|
||||
pub(crate) name: String,
|
||||
repo: Option<String>,
|
||||
}
|
||||
|
||||
impl From<String> for Package {
|
||||
fn from(mut s: String) -> Self {
|
||||
s.remove_comment();
|
||||
s.remove_whitespace();
|
||||
let (name, repo) = Self::split_into_name_and_repo(s);
|
||||
Self { name, repo }
|
||||
}
|
||||
}
|
||||
|
||||
impl Package {
|
||||
pub(crate) fn from_lines(lines: Lines<BufReader<File>>) -> HashSet<Self> {
|
||||
lines
|
||||
.into_iter()
|
||||
.map(|l| Package::from(l.unwrap()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn split_into_name_and_repo(mut s: String) -> (String, Option<String>) {
|
||||
match s.find('/') {
|
||||
None => (s, None),
|
||||
Some(pos) => {
|
||||
let mut name = s.split_off(pos);
|
||||
name = name.split_off(1);
|
||||
(name, Some(s))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Package {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.name == other.name
|
||||
&& match &self.repo {
|
||||
None => true,
|
||||
Some(r) => match &other.repo {
|
||||
None => true,
|
||||
Some(r2) => r == r2,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Package {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.name.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
trait Whitespace {
|
||||
fn remove_comment(&mut self) {}
|
||||
fn remove_whitespace(&mut self) {}
|
||||
}
|
||||
|
||||
impl Whitespace for String {
|
||||
fn remove_comment(&mut self) {
|
||||
match self.find('#') {
|
||||
None => (),
|
||||
Some(idx) => self.truncate(idx),
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_whitespace(&mut self) {
|
||||
match self.find(char::is_whitespace) {
|
||||
None => (),
|
||||
Some(idx) => self.truncate(idx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Package {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match &self.repo {
|
||||
None => (),
|
||||
Some(repo) => {
|
||||
f.write_str(repo)?;
|
||||
f.write_char('/')?;
|
||||
}
|
||||
}
|
||||
f.write_str(&self.name)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Package;
|
||||
|
||||
#[test]
|
||||
fn split_into_name_and_repo() {
|
||||
let x = "repo/name".to_string();
|
||||
let (name, repo) = Package::split_into_name_and_repo(x);
|
||||
assert_eq!(name, "name");
|
||||
assert_eq!(repo, Some("repo".to_string()));
|
||||
|
||||
let x = "something".to_string();
|
||||
let (name, repo) = super::Package::split_into_name_and_repo(x);
|
||||
assert_eq!(name, "something");
|
||||
assert_eq!(repo, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from() {
|
||||
let x = "myrepo/somepackage # ".to_string();
|
||||
let p = Package::from(x);
|
||||
assert_eq!(p.name, "somepackage");
|
||||
assert_eq!(p.repo, Some("myrepo".to_string()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user