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
79
80
81
82
83
84
85
86
87
88
use std::{
convert::TryFrom,
fs,
path::PathBuf,
process::{Command, Stdio},
};
use miette::{IntoDiagnostic, Result};
use crate::mit::Authors;
pub trait AuthorArgs {
fn author_command(&self) -> Option<&str>;
fn author_file(&self) -> Option<&str>;
}
pub fn get_authors<'a>(args: &'a dyn AuthorArgs) -> Result<Authors<'a>> {
let toml = match args.author_command() {
Some(command) => from_exec(command),
None => from_file(args),
}?;
let authors: Authors<'a> = Authors::try_from(toml)?;
Ok(authors)
}
fn from_file(args: &dyn AuthorArgs) -> Result<String> {
match args.author_file() {
None => Err(super::errors::Error::AuthorFileNotSet.into()),
Some(path) => Ok(path),
}
.and_then(|path| match path {
"$HOME/.config/git-mit/mit.toml" => author_file_path(),
_ => Ok(path.into()),
})
.map(|path| fs::read_to_string(&path).unwrap_or_default())
}
#[cfg(not(target_os = "windows"))]
fn author_file_path() -> Result<String> {
let home: PathBuf = std::env::var("HOME").into_diagnostic()?.into();
return Ok(home
.join(".config")
.join("git-mit")
.join("mit.toml")
.to_string_lossy()
.to_string());
}
#[cfg(target_os = "windows")]
fn author_file_path() -> Result<String> {
std::env::var("APPDATA")
.map(|x| {
PathBuf::from(x)
.join("git-mit")
.join("mit.toml")
.to_string_lossy()
.into()
})
.into_diagnostic()
}
fn from_exec(command: &str) -> Result<String> {
let commandline = shell_words::split(command).into_diagnostic()?;
Command::new(commandline.first().unwrap_or(&String::from("")))
.stderr(Stdio::inherit())
.args(commandline.iter().skip(1).collect::<Vec<_>>())
.output()
.into_diagnostic()
.and_then(|output| {
String::from_utf8(output.stdout).map_err(|source| {
super::errors::Error::ExecUtf8 {
source,
command: command.to_string(),
}
.into()
})
})
}