Add Package example to main
Add remove method to pacman/apt package managers
This commit is contained in:
parent
0f85701cc4
commit
f26f1c1bf2
34
src/main.rs
34
src/main.rs
|
@ -1,7 +1,30 @@
|
||||||
use rustible::Facts;
|
use rustible::Facts;
|
||||||
use rustible::modules::{PlaybookRun, Module, Command};
|
|
||||||
|
|
||||||
fn main() -> Result<(), rustible::modules::command::CommandError> {
|
use rustible::modules::{
|
||||||
|
Module, PlaybookRun,
|
||||||
|
package::{PackageModule as Package, PackageState, PackageError},
|
||||||
|
command::{CommandModule as Command, CommandError},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, serde::Serialize)]
|
||||||
|
pub enum PlaybookError {
|
||||||
|
Command(CommandError),
|
||||||
|
Package(PackageError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CommandError> for PlaybookError {
|
||||||
|
fn from(e: CommandError) -> PlaybookError {
|
||||||
|
PlaybookError::Command(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PackageError> for PlaybookError {
|
||||||
|
fn from(e: PackageError) -> PlaybookError {
|
||||||
|
PlaybookError::Package(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), PlaybookError> {
|
||||||
let facts = Facts::new();
|
let facts = Facts::new();
|
||||||
println!("Hello, world! Running system {}", facts.os.family().as_str());
|
println!("Hello, world! Running system {}", facts.os.family().as_str());
|
||||||
|
|
||||||
|
@ -20,7 +43,12 @@ fn main() -> Result<(), rustible::modules::command::CommandError> {
|
||||||
.arg("/tmp/lol")
|
.arg("/tmp/lol")
|
||||||
.build();
|
.build();
|
||||||
playbook.run(cmd)?;
|
playbook.run(cmd)?;
|
||||||
//let res = cmd.run
|
|
||||||
|
let pkg = Package::new()
|
||||||
|
.name("hello")
|
||||||
|
.state(PackageState::Absent)
|
||||||
|
.build();
|
||||||
|
playbook.run(pkg)?;
|
||||||
|
|
||||||
playbook.print_json_pretty();
|
playbook.print_json_pretty();
|
||||||
|
|
||||||
|
|
|
@ -129,4 +129,4 @@ pub trait Module<A: Serialize, S: Serialize, E: Serialize> {
|
||||||
|
|
||||||
/// Declare module types
|
/// Declare module types
|
||||||
pub use command::CommandModule as Command;
|
pub use command::CommandModule as Command;
|
||||||
|
pub use package::{PackageModule as Package, PackageState};
|
||||||
|
|
|
@ -28,7 +28,12 @@ impl SpecificPackageManager for DebianPackageManager {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&self, _list: PackageList) -> Result<(), PackageError> {
|
fn remove(&self, list: PackageList) -> Result<(), PackageError> {
|
||||||
todo!()
|
let res = Cmd::new("apt").arg("remove").args(list.list()).run()?;
|
||||||
|
|
||||||
|
if ! res.success {
|
||||||
|
return Err(PackageError::CmdFail(res));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,3 +101,17 @@ impl PackageArgsBuilder<PackageList, PackageState, Box<dyn SpecificPackageManage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PackageArgsBuilder<PackageList, PackageState, NoManager> {
|
||||||
|
pub fn build(self) -> PackageModule<NoManager> {
|
||||||
|
let Self { name, state, .. } = self;
|
||||||
|
|
||||||
|
PackageModule {
|
||||||
|
args: PackageArgs {
|
||||||
|
name,
|
||||||
|
state,
|
||||||
|
},
|
||||||
|
manager: NoManager,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ pub mod apt;
|
||||||
use apt::DebianPackageManager;
|
use apt::DebianPackageManager;
|
||||||
|
|
||||||
pub mod builder;
|
pub mod builder;
|
||||||
pub use builder::PackageArgsBuilder;
|
pub use builder::{PackageArgsBuilder, NoManager};
|
||||||
|
|
||||||
pub type PackageStatus = ();
|
pub type PackageStatus = ();
|
||||||
|
|
||||||
|
@ -35,10 +35,6 @@ pub struct PackageModule<Manager> {
|
||||||
args: PackageArgs,
|
args: PackageArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct NoManager;
|
|
||||||
|
|
||||||
impl Module<PackageArgs, (), PackageError> for PackageModule<Box<dyn SpecificPackageManager>> {
|
impl Module<PackageArgs, (), PackageError> for PackageModule<Box<dyn SpecificPackageManager>> {
|
||||||
fn serialize_args(&self) -> serde_json::Value {
|
fn serialize_args(&self) -> serde_json::Value {
|
||||||
serde_json::to_value(&self.args).unwrap()
|
serde_json::to_value(&self.args).unwrap()
|
||||||
|
@ -49,7 +45,17 @@ impl Module<PackageArgs, (), PackageError> for PackageModule<Box<dyn SpecificPac
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(self) -> Result<(), PackageError> {
|
fn run(self) -> Result<(), PackageError> {
|
||||||
Ok(())
|
let packages = self.args.name;
|
||||||
|
|
||||||
|
match &self.args.state {
|
||||||
|
PackageState::Present => {
|
||||||
|
self.manager.install(packages)
|
||||||
|
}, PackageState::Absent => {
|
||||||
|
self.manager.remove(packages)
|
||||||
|
}, PackageState::Latest => {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,15 +123,35 @@ impl PackageList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Turn a stringy value or list of values into an actual package list.
|
||||||
|
/// Only implemented for &str and &str/String slices because apparently
|
||||||
|
/// there may be a future implementation that turns a slice of stringy values
|
||||||
|
/// into a stringy value, resulting in conflicting implementations.
|
||||||
|
/// https://stackoverflow.com/questions/63136970/how-do-i-work-around-the-upstream-crates-may-add-a-new-impl-of-trait-error
|
||||||
pub trait IntoPackageList {
|
pub trait IntoPackageList {
|
||||||
fn into_package_list(self) -> PackageList;
|
fn into_package_list(self) -> PackageList;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T: Into<Vec<String>>> IntoPackageList for T {
|
impl IntoPackageList for &str {
|
||||||
fn into_package_list(self) -> PackageList {
|
fn into_package_list(self) -> PackageList {
|
||||||
PackageList {
|
PackageList {
|
||||||
list: self.into()
|
list: vec!(self.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IntoPackageList for & [ &str ] {
|
||||||
|
fn into_package_list(self) -> PackageList {
|
||||||
|
PackageList {
|
||||||
|
list: self.iter().map(|x| x.to_string()).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoPackageList for & [ String ] {
|
||||||
|
fn into_package_list(self) -> PackageList {
|
||||||
|
PackageList {
|
||||||
|
list: self.to_vec(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -31,6 +31,12 @@ impl SpecificPackageManager for ArchlinuxPackageManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&self, _list: PackageList) -> Result<(), PackageError> {
|
fn remove(&self, _list: PackageList) -> Result<(), PackageError> {
|
||||||
todo!()
|
let res = Cmd::new("pacman").arg("-R").args(list.list()).run()?;
|
||||||
|
|
||||||
|
if ! res.success {
|
||||||
|
return Err(PackageError::CmdFail(res));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user