Add hello.rs Hello World example

This commit is contained in:
selfhoster selfhoster 2023-04-21 17:25:31 +02:00
parent d2ef322761
commit 7d9e3d63b7
2 changed files with 63 additions and 0 deletions

49
examples/hello.rs Normal file
View File

@ -0,0 +1,49 @@
use rustible::Facts;
use rustible::modules::{
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();
println!("rustible running system: {}", facts.os.family().as_str());
let mut playbook = PlaybookRun::new();
let pkg = Package::new()
.name("hello")
.state(PackageState::Present)
.build();
playbook.run(pkg)?;
let cmd = Command::new()
.program("hello")
.arg("-g")
.arg("\"Welcome to rustible!\"")
.build();
let res = playbook.run(cmd)?;
println!("STDOUT:\n{}", res.stdout().unwrap());
Ok(())
}

View File

@ -92,6 +92,20 @@ impl CommandStatus {
Self::Skipped(_c) => true,
}
}
pub fn stdout(&self) -> Option<String> {
match self {
Self::Done(ret) => Some(ret.stdout.to_string()),
Self::Skipped(_c) => None,
}
}
pub fn stderr(&self) -> Option<String> {
match self {
Self::Done(ret) => Some(ret.stdout.to_string()),
Self::Skipped(_c) => None,
}
}
}
/// Return of a Command that was effectively run