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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use log::debug;
use std::fs::{create_dir_all, remove_file, rename};
use std::path::Path;
use std::process::Command;
use crate::error::PathError;
use anyhow::{ensure, Context, Result};
use tr::tr;
pub fn run_command_and_check_success(command_name: &str, mut command: Command) -> Result<()> {
debug!("Running command: {0:?}", &command);
let output = command
.spawn()
.with_context(|| tr!("The \"{0}\" command was unable to start.", command_name))?
.wait_with_output()
.with_context(|| {
tr!(
"The \"{0}\" command had a problem waiting for output.",
command_name
)
})?;
ensure!(
output.status.success(),
tr!(
"The \"{0}\" command reported that it was unsuccessful.",
command_name
)
);
Ok(())
}
pub fn check_path_exists<P: AsRef<Path>>(path: P) -> Result<(), PathError> {
if !path.as_ref().exists() {
Err(PathError::does_not_exist(path.as_ref()))
} else {
Ok(())
}
}
pub fn create_dir_all_if_not_exists<P: AsRef<Path>>(path: P) -> Result<(), PathError> {
if !path.as_ref().exists() {
create_dir_all(path.as_ref())
.map_err(|e| PathError::cannot_create_dir(path.as_ref(), e))?;
}
Ok(())
}
pub fn remove_file_if_exists<P: AsRef<Path>>(file_path: P) -> Result<(), PathError> {
if file_path.as_ref().exists() {
remove_file(file_path.as_ref())
.map_err(|e| PathError::cannot_delete_file(file_path.as_ref(), e))?;
}
Ok(())
}
pub fn remove_file_or_error<P: AsRef<Path>>(file_path: P) -> Result<(), PathError> {
remove_file(file_path.as_ref())
.map_err(|e| PathError::cannot_delete_file(file_path.as_ref(), e))
}
pub fn rename_file<P1: AsRef<Path>, P2: AsRef<Path>>(from: P1, to: P2) -> Result<(), PathError> {
let from_ref = from.as_ref();
let to_ref = to.as_ref();
rename(from_ref, to_ref)
.map_err(|e| PathError::cannot_rename_file(from_ref.to_path_buf(), to_ref.to_path_buf(), e))
}