mirror of
https://github.com/philmmanjaro/alma.git
synced 2025-07-26 23:19:29 +02:00
Implement process module
This commit is contained in:
parent
8b0d9ef16a
commit
a250e749d1
51
src/main.rs
51
src/main.rs
@ -9,16 +9,17 @@ extern crate which;
|
|||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod mountstack;
|
mod mountstack;
|
||||||
|
mod process;
|
||||||
mod tool;
|
mod tool;
|
||||||
|
|
||||||
use error::*;
|
use error::*;
|
||||||
use failure::{Fail, ResultExt};
|
use failure::{Fail, ResultExt};
|
||||||
use mountstack::{Filesystem, MountStack};
|
use mountstack::{Filesystem, MountStack};
|
||||||
|
use process::CommandExt;
|
||||||
use simplelog::*;
|
use simplelog::*;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::{exit, Command, ExitStatus};
|
use std::process::exit;
|
||||||
use std::str;
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
@ -30,52 +31,6 @@ BINARIES=()
|
|||||||
FILES=()
|
FILES=()
|
||||||
HOOKS=(base udev block filesystems keyboard fsck)";
|
HOOKS=(base udev block filesystems keyboard fsck)";
|
||||||
|
|
||||||
#[derive(Debug, Fail)]
|
|
||||||
enum ProcessError {
|
|
||||||
#[fail(display = "Bad exit code: {}", _0)]
|
|
||||||
BadExitCode(ExitStatus),
|
|
||||||
|
|
||||||
#[fail(display = "Process output isn't valid UTF-8")]
|
|
||||||
InvalidUtf8,
|
|
||||||
}
|
|
||||||
|
|
||||||
trait CommandExt {
|
|
||||||
fn run(&mut self, context: ErrorKind) -> Result<(), Error>;
|
|
||||||
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CommandExt for Command {
|
|
||||||
fn run(&mut self, context: ErrorKind) -> Result<(), Error> {
|
|
||||||
let exit_status = self.spawn().context(context)?.wait().context(context)?;
|
|
||||||
|
|
||||||
if !exit_status.success() {
|
|
||||||
return Err(ProcessError::BadExitCode(exit_status)
|
|
||||||
.context(context)
|
|
||||||
.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error> {
|
|
||||||
let output = self.output().context(context)?;
|
|
||||||
|
|
||||||
if !output.status.success() {
|
|
||||||
let error = str::from_utf8(&output.stderr).unwrap_or("[INVALID UTF8]");
|
|
||||||
error!("{}", error);
|
|
||||||
return Err(ProcessError::BadExitCode(output.status)
|
|
||||||
.context(context)
|
|
||||||
.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(String::from(
|
|
||||||
str::from_utf8(&output.stdout)
|
|
||||||
.map_err(|_| ProcessError::InvalidUtf8)
|
|
||||||
.context(context)?,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
#[structopt(name = "alma", about = "Arch Linux Mobile Applicance")]
|
#[structopt(name = "alma", about = "Arch Linux Mobile Applicance")]
|
||||||
enum App {
|
enum App {
|
||||||
|
50
src/process.rs
Normal file
50
src/process.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use super::error::*;
|
||||||
|
use failure::{Fail, ResultExt};
|
||||||
|
use std::process::{Command, ExitStatus};
|
||||||
|
use std::str;
|
||||||
|
|
||||||
|
#[derive(Debug, Fail)]
|
||||||
|
enum ProcessError {
|
||||||
|
#[fail(display = "Bad exit code: {}", _0)]
|
||||||
|
BadExitCode(ExitStatus),
|
||||||
|
|
||||||
|
#[fail(display = "Process output isn't valid UTF-8")]
|
||||||
|
InvalidUtf8,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait CommandExt {
|
||||||
|
fn run(&mut self, context: ErrorKind) -> Result<(), Error>;
|
||||||
|
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandExt for Command {
|
||||||
|
fn run(&mut self, context: ErrorKind) -> Result<(), Error> {
|
||||||
|
let exit_status = self.spawn().context(context)?.wait().context(context)?;
|
||||||
|
|
||||||
|
if !exit_status.success() {
|
||||||
|
return Err(ProcessError::BadExitCode(exit_status)
|
||||||
|
.context(context)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error> {
|
||||||
|
let output = self.output().context(context)?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
let error = str::from_utf8(&output.stderr).unwrap_or("[INVALID UTF8]");
|
||||||
|
error!("{}", error);
|
||||||
|
return Err(ProcessError::BadExitCode(output.status)
|
||||||
|
.context(context)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(String::from(
|
||||||
|
str::from_utf8(&output.stdout)
|
||||||
|
.map_err(|_| ProcessError::InvalidUtf8)
|
||||||
|
.context(context)?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user