use std::collections::HashMap;
-use anyhow::{Error};
+use anyhow::{bail, Error};
use lazy_static::lazy_static;
use super::time::*;
-use pbs_tools::nom::{
- parse_complete_line, parse_u64, parse_error, IResult,
-};
-
use nom::{
- error::{context},
+ error::{context, ParseError, VerboseError},
bytes::complete::{tag, take_while1},
- combinator::{map_res, opt, recognize},
+ combinator::{map_res, all_consuming, opt, recognize},
sequence::{pair, preceded, tuple},
character::complete::{alpha1, space0, digit1},
multi::separated_nonempty_list,
};
+type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
+
+fn parse_error<'a>(i: &'a str, context: &'static str) -> nom::Err<VerboseError<&'a str>> {
+ let err = VerboseError { errors: Vec::new() };
+ let err = VerboseError::add_context(i, context, err);
+ nom::Err::Error(err)
+}
+
+// Parse a 64 bit unsigned integer
+fn parse_u64(i: &str) -> IResult<&str, u64> {
+ map_res(recognize(digit1), str::parse)(i)
+}
+
+// Parse complete input, generate simple error message (use this for sinple line input).
+fn parse_complete_line<'a, F, O>(what: &str, i: &'a str, parser: F) -> Result<O, Error>
+ where F: Fn(&'a str) -> IResult<&'a str, O>,
+{
+ match all_consuming(parser)(i) {
+ Err(nom::Err::Error(VerboseError { errors })) |
+ Err(nom::Err::Failure(VerboseError { errors })) => {
+ if errors.is_empty() {
+ bail!("unable to parse {}", what);
+ } else {
+ bail!("unable to parse {} at '{}' - {:?}", what, errors[0].0, errors[0].1);
+ }
+ }
+ Err(err) => {
+ bail!("unable to parse {} - {}", what, err);
+ }
+ Ok((_, data)) => Ok(data),
+ }
+}
+
lazy_static! {
pub static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = {
let mut map = HashMap::new();
-use anyhow::{bail, Error};
+use std::process::Command;
+
+use anyhow::{bail, format_err, Error};
+
+fn run_command(mut command: Command) -> Result<(), Error> {
+ let output = command
+ .output()
+ .map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?;
+
+ proxmox::try_block!({
+ if !output.status.success() {
+ match output.status.code() {
+ Some(code) => {
+ if code != 0 {
+ let msg = String::from_utf8(output.stderr)
+ .map(|m| {
+ if m.is_empty() {
+ String::from("no error message")
+ } else {
+ m
+ }
+ })
+ .unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));
+
+ bail!("status code: {} - {}", code, msg);
+ }
+ }
+ None => bail!("terminated by signal"),
+ }
+ }
+ Ok(())
+ }).map_err(|err| format_err!("command {:?} failed - {}", command, err))?;
-use pbs_tools::run_command;
+ Ok(())
+}
/// Escape strings for usage in systemd unit names
pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
let mut command = std::process::Command::new("systemctl");
command.arg("daemon-reload");
- run_command(command, None)?;
+ run_command(command)?;
Ok(())
}
command.arg("disable");
command.arg(unit);
- run_command(command, None)?;
+ run_command(command)?;
Ok(())
}
command.arg("enable");
command.arg(unit);
- run_command(command, None)?;
+ run_command(command)?;
Ok(())
}
command.arg("start");
command.arg(unit);
- run_command(command, None)?;
+ run_command(command)?;
Ok(())
}
command.arg("stop");
command.arg(unit);
- run_command(command, None)?;
+ run_command(command)?;
Ok(())
}
command.arg("try-reload-or-restart");
command.arg(unit);
- run_command(command, None)?;
+ run_command(command)?;
Ok(())
}