add error handling per backend
This commit is contained in:
+14
-8
@@ -7,6 +7,8 @@ use std::collections::HashSet;
|
|||||||
use std::os::unix::process::CommandExt;
|
use std::os::unix::process::CommandExt;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
use crate::{Group, Package};
|
use crate::{Group, Package};
|
||||||
|
|
||||||
pub(crate) use todo_per_backend::ToDoPerBackend;
|
pub(crate) use todo_per_backend::ToDoPerBackend;
|
||||||
@@ -58,10 +60,10 @@ pub(crate) trait Backend {
|
|||||||
fn load(&mut self, groups: &HashSet<Group>);
|
fn load(&mut self, groups: &HashSet<Group>);
|
||||||
|
|
||||||
/// Get all packages that are installed in the system.
|
/// Get all packages that are installed in the system.
|
||||||
fn get_all_installed_packages(&self) -> HashSet<Package>;
|
fn get_all_installed_packages(&self) -> Result<HashSet<Package>>;
|
||||||
|
|
||||||
/// Get all packages that were installed in the system explicitly.
|
/// Get all packages that were installed in the system explicitly.
|
||||||
fn get_explicitly_installed_packages(&self) -> HashSet<Package>;
|
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>>;
|
||||||
|
|
||||||
/// Install the specified packages.
|
/// Install the specified packages.
|
||||||
fn install_packages(&self, packages: &[Package]) {
|
fn install_packages(&self, packages: &[Package]) {
|
||||||
@@ -99,21 +101,25 @@ pub(crate) trait Backend {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_missing_packages_sorted(&self) -> Vec<Package> {
|
fn get_missing_packages_sorted(&self) -> Result<Vec<Package>> {
|
||||||
let installed = self.get_all_installed_packages();
|
let installed = self
|
||||||
|
.get_all_installed_packages()
|
||||||
|
.context("could not get installed packages")?;
|
||||||
let managed = self.get_managed_packages();
|
let managed = self.get_managed_packages();
|
||||||
let mut diff: Vec<_> = managed.difference(&installed).cloned().collect();
|
let mut diff: Vec<_> = managed.difference(&installed).cloned().collect();
|
||||||
diff.sort_unstable();
|
diff.sort_unstable();
|
||||||
diff
|
Ok(diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_packages(&mut self, packages: HashSet<Package>);
|
fn add_packages(&mut self, packages: HashSet<Package>);
|
||||||
|
|
||||||
fn get_unmanaged_packages_sorted(&self) -> Vec<Package> {
|
fn get_unmanaged_packages_sorted(&self) -> Result<Vec<Package>> {
|
||||||
let installed = self.get_explicitly_installed_packages();
|
let installed = self
|
||||||
|
.get_explicitly_installed_packages()
|
||||||
|
.context("could not get explicitly installed packages")?;
|
||||||
let required = self.get_managed_packages();
|
let required = self.get_managed_packages();
|
||||||
let mut diff: Vec<_> = installed.difference(required).cloned().collect();
|
let mut diff: Vec<_> = installed.difference(required).cloned().collect();
|
||||||
diff.sort_unstable();
|
diff.sort_unstable();
|
||||||
diff
|
Ok(diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
use alpm::Alpm;
|
use alpm::Alpm;
|
||||||
use alpm::PackageReason::Explicit;
|
use alpm::PackageReason::Explicit;
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
use super::{Backend, Switches, Text};
|
use super::{Backend, Switches, Text};
|
||||||
use crate::{impl_backend_constants, Group, Package};
|
use crate::{impl_backend_constants, Group, Package};
|
||||||
@@ -18,12 +19,16 @@ const SWITCHES_REMOVE: Switches = &["-Rsn"];
|
|||||||
impl Backend for Pacman {
|
impl Backend for Pacman {
|
||||||
impl_backend_constants!();
|
impl_backend_constants!();
|
||||||
|
|
||||||
fn get_all_installed_packages(&self) -> HashSet<Package> {
|
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
|
||||||
convert_to_pacdef_packages(get_all_installed_packages_from_alpm())
|
Ok(convert_to_pacdef_packages(
|
||||||
|
get_all_installed_packages_from_alpm(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
|
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
|
||||||
convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm())
|
Ok(convert_to_pacdef_packages(
|
||||||
|
get_explicitly_installed_packages_from_alpm(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-10
@@ -2,7 +2,7 @@ use std::collections::HashSet;
|
|||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{Context, Result};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use super::{Backend, Switches, Text};
|
use super::{Backend, Switches, Text};
|
||||||
@@ -20,28 +20,34 @@ const SWITCHES_REMOVE: Switches = &["uninstall"];
|
|||||||
impl Backend for Rust {
|
impl Backend for Rust {
|
||||||
impl_backend_constants!();
|
impl_backend_constants!();
|
||||||
|
|
||||||
fn get_all_installed_packages(&self) -> HashSet<Package> {
|
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
|
||||||
let file = get_crates_file().unwrap();
|
let file = get_crates_file().unwrap();
|
||||||
let content = read_to_string(file).unwrap();
|
let content = read_to_string(file).unwrap();
|
||||||
let json: Value = serde_json::from_str(&content).unwrap();
|
let json: Value = serde_json::from_str(&content).unwrap();
|
||||||
extract_packages(json)
|
extract_packages(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
|
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
|
||||||
self.get_all_installed_packages()
|
self.get_all_installed_packages()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_packages(json: Value) -> HashSet<Package> {
|
fn extract_packages(json: Value) -> Result<HashSet<Package>> {
|
||||||
json.get("installs")
|
let result: HashSet<_> = json
|
||||||
.unwrap()
|
.get("installs")
|
||||||
|
.context("get 'installs' field from json")?
|
||||||
.as_object()
|
.as_object()
|
||||||
.unwrap()
|
.context("getting object")?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(name, _)| name)
|
.map(|(name, _)| name)
|
||||||
.map(|name| name.split_whitespace().next().unwrap())
|
.map(|name| {
|
||||||
.filter_map(Package::try_from)
|
name.split_whitespace()
|
||||||
.collect()
|
.next()
|
||||||
|
.expect("identifier is whitespace-delimited")
|
||||||
|
})
|
||||||
|
.map(|name| Package::try_from(name).expect("name is valid"))
|
||||||
|
.collect();
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rust {
|
impl Rust {
|
||||||
|
|||||||
+10
-6
@@ -39,11 +39,13 @@ impl Pacdef {
|
|||||||
fn get_missing_packages(&self) -> ToDoPerBackend {
|
fn get_missing_packages(&self) -> ToDoPerBackend {
|
||||||
let mut to_install = ToDoPerBackend::new();
|
let mut to_install = ToDoPerBackend::new();
|
||||||
|
|
||||||
for mut b in Backends::iter() {
|
for mut backend in Backends::iter() {
|
||||||
b.load(&self.groups);
|
backend.load(&self.groups);
|
||||||
|
|
||||||
let diff = b.get_missing_packages_sorted();
|
match backend.get_missing_packages_sorted() {
|
||||||
to_install.push((b, diff));
|
Ok(diff) => to_install.push((backend, diff)),
|
||||||
|
Err(e) => println!("WARNING: skipping backend '{}': {e}", backend.get_section()),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
to_install
|
to_install
|
||||||
@@ -119,8 +121,10 @@ impl Pacdef {
|
|||||||
for mut backend in Backends::iter() {
|
for mut backend in Backends::iter() {
|
||||||
backend.load(&self.groups);
|
backend.load(&self.groups);
|
||||||
|
|
||||||
let unmanaged = backend.get_unmanaged_packages_sorted();
|
match backend.get_unmanaged_packages_sorted() {
|
||||||
result.push((backend, unmanaged));
|
Ok(unmanaged) => result.push((backend, unmanaged)),
|
||||||
|
Err(e) => println!("WARNING: skipping backend '{}': {e}", backend.get_section()),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user