mit_commit_message_lints/mit/cmd/
get_authors.rs1use std::{
2 convert::TryFrom,
3 fs,
4 path::PathBuf,
5 process::{Command, Stdio},
6};
7
8use miette::{IntoDiagnostic, Result};
9
10use crate::mit::Authors;
11
12#[derive(Debug, Clone)]
14pub struct GenericArgs<'a> {
15 pub author_command: Option<&'a str>,
17 pub author_file: Option<&'a str>,
19}
20
21impl AuthorArgs for GenericArgs<'_> {
22 fn author_command(&self) -> Option<&str> {
23 self.author_command
24 }
25
26 fn author_file(&self) -> Option<&str> {
27 self.author_file
28 }
29}
30
31pub trait AuthorArgs {
33 fn author_command(&self) -> Option<&str>;
35
36 fn author_file(&self) -> Option<&str>;
38}
39
40pub fn get_authors<'a>(args: &'a dyn AuthorArgs) -> Result<Authors<'a>> {
46 let toml = args
47 .author_command()
48 .map_or_else(|| from_file(args), from_exec)?;
49 let authors: Authors<'a> = Authors::try_from(toml)?;
50 Ok(authors)
51}
52
53fn from_file(args: &dyn AuthorArgs) -> Result<String> {
54 args.author_file()
55 .map_or_else(|| Err(super::errors::Error::AuthorFileNotSet.into()), Ok)
56 .and_then(|path| match path {
57 "$HOME/.config/git-mit/mit.toml" => author_file_path(),
58 _ => Ok(path.into()),
59 })
60 .and_then(|path| fs::read_to_string(path).into_diagnostic())
61}
62
63#[cfg(not(target_os = "windows"))]
64fn author_file_path() -> Result<String> {
65 let home: PathBuf = std::env::var("HOME").into_diagnostic()?.into();
66 Ok(home
67 .join(".config")
68 .join("git-mit")
69 .join("mit.toml")
70 .to_string_lossy()
71 .to_string())
72}
73
74#[cfg(target_os = "windows")]
75fn author_file_path() -> Result<String> {
76 std::env::var("APPDATA")
77 .map(|x| {
78 PathBuf::from(x)
79 .join("git-mit")
80 .join("mit.toml")
81 .to_string_lossy()
82 .into()
83 })
84 .into_diagnostic()
85}
86
87fn from_exec(command: &str) -> Result<String> {
88 let commandline = shell_words::split(command).into_diagnostic()?;
89 Command::new(commandline.first().unwrap_or(&String::new()))
90 .stderr(Stdio::inherit())
91 .args(commandline.iter().skip(1))
92 .output()
93 .into_diagnostic()
94 .and_then(|output| {
95 String::from_utf8(output.stdout).map_err(|source| {
96 super::errors::Error::ExecUtf8 {
97 source,
98 command: command.to_string(),
99 }
100 .into()
101 })
102 })
103}
104
105#[cfg(test)]
106mod tests {
107 use std::io::Write;
108
109 use crate::mit::{get_authors, GenericArgs};
110
111 #[test]
112 #[cfg(unix)]
113 fn unreadable_author_file_returns_error() {
114 use std::os::unix::fs::PermissionsExt;
115
116 let mut temp_file = std::env::temp_dir();
117 temp_file.push(format!("unreadable_mit_test_{}.toml", std::process::id()));
118
119 let _ = std::fs::remove_file(&temp_file);
120
121 {
122 let mut file = std::fs::File::create(&temp_file).unwrap();
123 file.write_all(b"[authors]").unwrap();
124 }
125
126 let mut permissions = std::fs::metadata(&temp_file).unwrap().permissions();
127 permissions.set_mode(0o000);
128 std::fs::set_permissions(&temp_file, permissions).unwrap();
129
130 let args = GenericArgs {
131 author_command: None,
132 author_file: Some(temp_file.to_str().unwrap()),
133 };
134
135 let result = get_authors(&args);
136 assert!(
137 result.is_err(),
138 "expected an IO error when the author file is unreadable, but got Ok"
139 );
140
141 let mut permissions = std::fs::metadata(&temp_file).unwrap().permissions();
143 permissions.set_mode(0o644);
144 std::fs::set_permissions(&temp_file, permissions).unwrap();
145 let _ = std::fs::remove_file(&temp_file);
146 }
147
148 use super::AuthorArgs;
149
150 #[test]
151 fn author_command_passes_through_some_value() {
152 let args = GenericArgs {
153 author_command: Some("echo hello"),
154 author_file: None,
155 };
156 assert_eq!(
157 args.author_command(),
158 Some("echo hello"),
159 "Expected author_command to pass through the provided value"
160 );
161 }
162
163 #[test]
164 fn author_command_passes_through_none() {
165 let args = GenericArgs {
166 author_command: None,
167 author_file: None,
168 };
169 assert_eq!(
170 args.author_command(),
171 None,
172 "Expected author_command to be None when not set"
173 );
174 }
175
176 #[test]
177 fn author_file_passes_through_some_value() {
178 let args = GenericArgs {
179 author_command: None,
180 author_file: Some("/custom/path.toml"),
181 };
182 assert_eq!(
183 args.author_file(),
184 Some("/custom/path.toml"),
185 "Expected author_file to pass through the provided value"
186 );
187 }
188
189 #[test]
190 fn author_file_passes_through_none() {
191 let args = GenericArgs {
192 author_command: None,
193 author_file: None,
194 };
195 assert_eq!(
196 args.author_file(),
197 None,
198 "Expected author_file to be None when not set"
199 );
200 }
201}