1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::error::Error;
use std::process::{ExitStatus, Output};

use clap::{ArgMatches, Command};
use eyre::eyre;

pub trait TermCommandDefinition {
    fn clap(&self) -> Command;
    fn run(&self, command: &ArgMatches);
}

pub trait CommandOutputError {
    fn output_error(self) -> eyre::Result<Output>;
}

pub trait CommandStatusError {
    fn status_error(self) -> eyre::Result<ExitStatus>;
}

impl<Err> CommandOutputError for Result<Output, Err>
where
    Err: Error + Send + Sync + 'static,
{
    fn output_error(self) -> eyre::Result<Output> {
        let output = self?;
        if !output.status.success() {
            return Err(eyre!("exit status {}", output.status));
        }
        Ok(output)
    }
}

impl<Err> CommandStatusError for Result<ExitStatus, Err>
where
    Err: Error + Send + Sync + 'static,
{
    fn status_error(self) -> eyre::Result<ExitStatus> {
        let status = self?;
        if !status.success() {
            return Err(eyre!("exit status {}", status));
        }
        Ok(status)
    }
}