hpk/src/hooks/mod.rs

101 lines
2.7 KiB
Rust

use hpk_package::{Group, User};
use {
crate::InstallError,
std::{
path::PathBuf,
process::{Command, Output},
},
};
#[derive(Debug, Clone, PartialEq)]
/// A post install script extracted from a package archive and the sysroot in
/// which it should be run (usually "/")
pub struct Pinstall {
script: PathBuf,
root: PathBuf,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
/// Defines a set of commands to be run in order to finish package installation
pub enum Hooks {
/// Runs `makewhatis` to update the mandoc database if the package contains
/// any Unix man pages
Man,
/// Runs `glib-compile-schemas` if the package contains any GLib schema files
GlibSchema,
/// runs `install-info` to update the GNU Texinfo database
Info(String),
/// runs the post install script for a package
Pinstall(Pinstall),
/// creates a new system user
User(User, Option<PathBuf>),
/// creates a new system group
Group(Group, Option<PathBuf>),
}
impl From<Pinstall> for Hooks {
fn from(value: Pinstall) -> Self {
Self::Pinstall(value)
}
}
impl From<(User, Option<PathBuf>)> for Hooks {
fn from(value: (User, Option<PathBuf>)) -> Self {
Self::User(value.0, value.1)
}
}
impl From<(Group, Option<PathBuf>)> for Hooks {
fn from(value: (Group, Option<PathBuf>)) -> Self {
Self::Group(value.0, value.1)
}
}
impl Hooks {
/// Runs a hook and returns it's output
pub fn run(&self) -> Result<Output, InstallError> {
match self {
Self::Man => makeinfo(),
Self::GlibSchema => compile_schemas(),
Self::Info(path) => install_info(path),
Self::Pinstall(p) => p.run(),
Self::User(_u, _p) => unimplemented!(),
Self::Group(_g, _p) => unimplemented!(),
}
}
}
fn makeinfo() -> Result<Output, InstallError> {
Command::new("makewhatis").output().map_err(|e| e.into())
}
fn compile_schemas() -> Result<Output, InstallError> {
Command::new("glib-compile-schemas")
.arg("/usr/share/glib-2.0/schemas")
.output()
.map_err(|e| e.into())
}
fn install_info(path: &str) -> Result<Output, InstallError> {
Command::new("install-info")
.arg(path)
.output()
.map_err(|e| e.into())
}
impl Pinstall {
pub fn new(script: PathBuf, root: PathBuf) -> Self {
Self { script, root }
}
fn run(&self) -> Result<Output, InstallError> {
Command::new("/bin/sh")
.arg(self.script.to_str().unwrap_or(""))
.env("HPK_ROOT", self.root.to_str().unwrap_or(""))
.output()
.map_err(|e| e.into())
}
}