<commit_before>
use std::env;
use std::process::{Command, Stdio};
use std::io::Write;

macro_rules! try_log {
    ($exp:expr) => {
        match $exp {
            Ok(v) => v,
            Err(e) => { println!("<3>{}", e); return; }
        }
    }
}

fn get_systemd_unit_property(unit: &str, prop: &str) -> String {
    String::from_utf8_lossy(&try_log!(Command::new("systemctl")
                            .arg("show")
                            .arg(unit)
                            .arg("--property")
                            .arg(prop)
                            .output())
                            .stdout[prop.len() + 1..])
        .trim_right_matches('\n')
        .to_owned()
}

fn main() {
    kernlog::init().unwrap();

    let unit = match env::args().nth(1) {
        Some(unit) => unit,
        None => {
            println!("Usage: mail-on-failure <unit>");
            return;
        }
    };

    let mut user = get_systemd_unit_property(&*unit, "User");
    if user.len() == 0 {
        user = "root".to_owned();
    }


    let job_env = get_systemd_unit_property(&*unit, "Environment");
    for pair in job_env.split(' ') {
        let mut p = pair.splitn(2, '=');
        if let (Some(name), Some(value)) = (p.next(), p.next()) {
            if name == "MAILTO" {
                user = value.to_owned();
                break;
            }
        }
    }

    if user.len() == 0 {
        return;
    }

    let mut hostname = String::from_utf8_lossy(&try_log!(Command::new("uname")
        .arg("-n")
        .output())
        .stdout[..])
        .trim_right_matches('\n')
        .to_owned();

    if hostname.len() == 0 {
        hostname = "localhost".to_owned();<commit_msg>escalate errors from unit property getter<commit_after>
use std::env;
use std::process::{Command, Stdio};
use std::io::{Write, Result};

macro_rules! try_log {
    ($exp:expr) => {
        match $exp {
            Ok(v) => v,
            Err(e) => { println!("<3>{}", e); return; }
        }
    }
}

fn get_systemd_unit_property(unit: &str, prop: &str) -> Result<String> {
    Command::new("systemctl")
        .arg("show")
        .arg(unit)
        .arg("--property")
        .arg(prop)
        .output()
        .map(|out|
             String::from_utf8_lossy(&out.stdout[prop.len() + 1..])
             .trim_right_matches('\n')
             .to_owned())
}

fn main() {
    let unit = match env::args().nth(1) {
        Some(unit) => unit,
        None => {
            println!("Usage: mail-on-failure <unit>");
            return;
        }
    };

    let mut user = try_log!(get_systemd_unit_property(&*unit, "User"));
    if user.len() == 0 {
        user = "root".to_owned();
    }


    let job_env = try_log!(get_systemd_unit_property(&*unit, "Environment"));
    for pair in job_env.split(' ') {
        let mut p = pair.splitn(2, '=');
        if let (Some(name), Some(value)) = (p.next(), p.next()) {
            if name == "MAILTO" {
                user = value.to_owned();
                break;
            }
        }
    }

    if user.len() == 0 {
        return;
    }

    let mut hostname = String::from_utf8_lossy(&try_log!(Command::new("uname")
        .arg("-n")
        .output())
        .stdout[..])
        .trim_right_matches('\n')
        .to_owned();

    if hostname.len() == 0 {
        hostname = "localhost".to_owned();
{'fraction_non_alphanumeric': 0.18044444444444444, 'fraction_numerical': 0.005333333333333333, 'mean_word_length': 1.6857597454256166, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5599862', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1116, 'n_words': 207}