Remove old command module

This commit is contained in:
Leonard Steppy 2025-02-05 23:56:44 +01:00
parent 7c64383d72
commit 89cd5232cf
4 changed files with 0 additions and 194 deletions

View File

@ -84,7 +84,3 @@ Once you have that installed, just run
cargo build --release
```
and you will find an executable in `target/release`.
### Unit tests
In order for the unit tests to pass, you will need `python3`.

View File

@ -1,188 +0,0 @@
#[deprecated]
use crate::log;
use crate::logger::{LogLevel, Logger};
use crate::shell_interface::command_to_string;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::io;
use std::process::{Command, ExitStatus, Output};
pub trait LogRunnable {
fn run(&mut self, logger: &Logger) -> Result<(), CommandSpecificError<ExecutionError>>;
fn collect_output(&mut self) -> Result<Output, CommandSpecificError<ExecutionError>>;
fn collect_full_output(&mut self) -> Result<Output, CommandSpecificError<StartError>>;
}
impl LogRunnable for Command {
fn run(&mut self, logger: &Logger) -> Result<(), CommandSpecificError<ExecutionError>> {
run(self, logger).map_err(|error| CommandSpecificError {
command: self,
error,
})
}
fn collect_output(&mut self) -> Result<Output, CommandSpecificError<ExecutionError>> {
collect_output(self, None).map_err(|error| CommandSpecificError {
command: self,
error,
})
}
fn collect_full_output(&mut self) -> Result<Output, CommandSpecificError<StartError>> {
collect_full_output(self).map_err(|error| CommandSpecificError {
command: self,
error,
})
}
}
fn run(command: &mut Command, logger: &Logger) -> Result<(), ExecutionError> {
match logger.level {
LogLevel::Debug | LogLevel::Info => {
let status = command.status()?;
if !status.success() {
Err(status)?;
}
}
LogLevel::Error => {
collect_output(command, Some(logger))?;
}
}
Ok(())
}
fn collect_output(
command: &mut Command,
logger: Option<&Logger>,
) -> Result<Output, ExecutionError> {
let output = collect_full_output(command)?;
if !output.status.success() {
if let Some(logger) = logger {
log!(logger, error, "{}", String::from_utf8_lossy(&output.stdout));
log!(logger, error, "{}", String::from_utf8_lossy(&output.stderr));
}
Err(output.status)?;
}
Ok(output)
}
fn collect_full_output(command: &mut Command) -> Result<Output, StartError> {
Ok(command.output()?)
}
#[derive(Debug)]
pub struct CommandSpecificError<'a, E> {
pub command: &'a Command,
pub error: E,
}
impl<E> Display for CommandSpecificError<'_, E>
where
E: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Failed to execute command '{}': {}",
command_to_string(self.command),
self.error
)
}
}
impl<E> Error for CommandSpecificError<'_, E> where E: Debug + Display {}
#[derive(Debug)]
pub struct StartError(io::Error);
impl From<io::Error> for StartError {
fn from(value: io::Error) -> Self {
StartError(value)
}
}
impl Display for StartError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Failed to start command: {}", self.0)
}
}
impl Error for StartError {}
#[derive(Debug)]
pub enum ExecutionError {
StartError(StartError),
BadExitStatus(ExitStatus),
}
impl From<io::Error> for ExecutionError {
fn from(value: io::Error) -> Self {
Self::StartError(StartError(value))
}
}
impl From<StartError> for ExecutionError {
fn from(value: StartError) -> Self {
Self::StartError(value)
}
}
impl From<ExitStatus> for ExecutionError {
fn from(value: ExitStatus) -> Self {
Self::BadExitStatus(value)
}
}
impl Display for ExecutionError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ExecutionError::StartError(e) => Display::fmt(e, f),
ExecutionError::BadExitStatus(status) => write!(f, "Command failed with {}", status),
}
}
}
impl Error for ExecutionError {}
#[cfg(test)]
mod test {
use crate::command::{CommandSpecificError, ExecutionError, LogRunnable};
use crate::logger::Logger;
use std::path::PathBuf;
use std::process::Command;
#[test]
fn test_unknown_command() {
let mut command = Command::new("python7");
let Err(
e @ CommandSpecificError {
error: ExecutionError::StartError(_),
..
},
) = command
.args([PathBuf::from("test-ressources/python/exit_1.py")])
.run(&Logger::default())
else {
panic!("command shouldn't exist");
};
assert_eq!(e.to_string(), "Failed to execute command 'python7': Failed to start command: No such file or directory (os error 2)");
}
#[test]
fn test_error() {
let mut command = Command::new("python3");
let Err(
e @ CommandSpecificError {
error: ExecutionError::BadExitStatus(_),
..
},
) = command
.arg("test-ressources/python/exit_1.py")
.run(&Logger::default())
else {
panic!("command should return exit-code 1")
};
assert_eq!(e.to_string(), "Failed to execute command 'python3 test-ressources/python/exit_1.py': Command failed with exit status: 1");
}
}

View File

@ -1,5 +1,4 @@
mod action;
mod command;
mod environment;
mod file;
mod logger;

View File

@ -1 +0,0 @@
exit(1)