1use std::error::Error;
2use std::fs::File;
3use std::io::BufWriter;
4use std::str::FromStr;
5use std::{ffi::OsString, fs};
6
7use chrono::{DateTime, Utc};
8use inquire::validator::Validation;
9use serde::{Deserialize, Serialize};
10use strum::IntoEnumIterator;
11
12use crate::cmd::check::OsedaCheckError;
13use crate::cmd::init::InitOptions;
14use crate::color::Color;
15use crate::github;
16use crate::tags::Tag;
17
18pub fn read_config_file<P: AsRef<std::path::Path>>(
19 path: P,
20) -> Result<OsedaConfig, OsedaCheckError> {
21 let config_str = fs::read_to_string(path.as_ref()).map_err(|_| {
22 OsedaCheckError::MissingConfig(format!(
23 "Could not find config file in {}",
24 path.as_ref().display()
25 ))
26 })?;
27
28 let conf: OsedaConfig = serde_json::from_str(&config_str)
29 .map_err(|_| OsedaCheckError::BadConfig("Could not parse oseda config file".to_owned()))?;
30
31 Ok(conf)
32}
33
34pub fn read_and_validate_config() -> Result<OsedaConfig, OsedaCheckError> {
48 let path = std::env::current_dir().map_err(|_| {
49 OsedaCheckError::DirectoryNameMismatch("Could not get path of working directory".to_owned())
50 })?;
51
52 let config_path = path.join("oseda-config.json");
53
54 let conf = read_config_file(config_path)?;
55
56 let in_ci = std::env::var("GITHUB_ACTIONS").is_ok_and(|v| v == "true");
57 let skip_git = in_ci;
58
59 validate_config(&conf, &path, skip_git, || {
60 github::get_config_from_user_git("user.name")
61 })?;
62
63 Ok(conf)
64}
65
66pub fn validate_config(
67 conf: &OsedaConfig,
68 current_dir: &std::path::Path,
69 skip_git: bool,
70 get_git_user: impl Fn() -> Option<String>,
73) -> Result<(), OsedaCheckError> {
74 if !skip_git {
75 let gh_name = get_git_user().ok_or_else(|| {
76 OsedaCheckError::BadGitCredentials(
77 "Could not get git user.name from git config".to_owned(),
78 )
79 })?;
80
81 if gh_name != conf.author {
82 return Err(OsedaCheckError::BadGitCredentials(
83 "Config author does not match git credentials".to_owned(),
84 ));
85 }
86 }
87
88 let cwd = current_dir.file_name().ok_or_else(|| {
89 OsedaCheckError::DirectoryNameMismatch("Could not resolve path name".to_owned())
90 })?;
91
92 if cwd != OsString::from(conf.title.clone()) {
93 return Err(OsedaCheckError::DirectoryNameMismatch(
94 "Config title does not match directory name".to_owned(),
95 ));
96 }
97
98 if conf.description.is_empty() {
99 return Err(OsedaCheckError::MissingDescription(
100 "Description is missing or empty. Please update the oseda-config.json".to_owned(),
101 ));
102 }
103
104 Ok(())
105}
106
107#[derive(Serialize, Deserialize)]
109pub struct OsedaConfig {
110 pub title: String,
111 pub author: String,
112 pub tags: Vec<Tag>,
113 pub last_updated: DateTime<Utc>,
115 pub color: String,
116 pub description: String,
118}
119
120pub fn prompt_for_title() -> Result<String, Box<dyn Error>> {
121 let validator = |input: &str| {
122 if input.chars().count() < 2 {
123 Ok(Validation::Invalid(
124 ("Title must be longer than two characters").into(),
125 ))
126 } else {
127 Ok(Validation::Valid)
128 }
129 };
130
131 Ok(inquire::Text::new("Title: ")
132 .with_validator(validator)
133 .prompt()?)
134}
135pub fn create_conf(options: InitOptions) -> Result<OsedaConfig, Box<dyn Error>> {
141 let title = match options.title {
142 Some(arg_title) => arg_title,
143 None => prompt_for_title()?.replace(" ", "-"),
144 };
145
146 let tags = match options.tags {
147 Some(arg_tags) => {
148 arg_tags
149 .iter()
150 .map(|arg_tag| Tag::from_str(arg_tag))
151 .collect::<Result<Vec<Tag>, _>>()
152 .map_err(|_| "Invalid tag. Custom Tags may be added to the oseda-config.json after initialization".to_string())?
153 },
154 None => prompt_for_tags()?
155 };
156
157 let color = match options.color {
158 Some(arg_color) => Color::from_str(&arg_color)
159 .map_err(|_| "Invalid color. Please use traditional english color names".to_string())?,
160 None => prompt_for_color()?,
161 };
162
163 let user_name = github::get_config_from_user_git("user.name")
164 .ok_or("Could not get github username. Please ensure you are signed into github")?;
165
166 Ok(OsedaConfig {
167 title: title.trim().to_owned(),
168 author: user_name,
169 tags,
170 last_updated: get_time(),
171 color: color.into_hex(),
172 description: String::new(),
174 })
175}
176
177fn prompt_for_tags() -> Result<Vec<Tag>, Box<dyn Error>> {
183 let options: Vec<Tag> = Tag::iter().collect();
184
185 let selected_tags =
186 inquire::MultiSelect::new("Select categories (type to search):", options.clone())
187 .prompt()?;
188
189 println!("You selected:");
190 for tags in selected_tags.iter() {
191 println!("- {:?}", tags);
192 }
193
194 Ok(selected_tags)
195}
196
197fn prompt_for_color() -> Result<Color, Box<dyn Error>> {
198 let options: Vec<Color> = Color::iter().collect();
199
200 let selected_color = inquire::Select::new(
201 "Select the color for your course (type to search):",
202 options.clone(),
203 )
204 .prompt()?;
205
206 println!("You selected: {:?}", selected_color);
207
208 Ok(selected_color)
209}
210
211pub fn update_time(mut conf: OsedaConfig) -> Result<(), Box<dyn Error>> {
221 conf.last_updated = get_time();
222
223 write_config(".", &conf)?;
224 Ok(())
225}
226
227fn get_time() -> DateTime<Utc> {
232 chrono::offset::Utc::now()
233}
234
235pub fn write_config(path: &str, conf: &OsedaConfig) -> Result<(), Box<dyn Error>> {
245 let file = File::create(format!("{}/oseda-config.json", path))?;
246 let writer = BufWriter::new(file);
247
248 serde_json::to_writer_pretty(writer, &conf)?;
249
250 Ok(())
251}
252
253#[cfg(test)]
254mod test {
255 use std::path::Path;
256 use tempfile::tempdir;
257
258 use super::*;
259
260 #[allow(dead_code)]
261 fn mock_config_json() -> String {
262 r#"
263 {
264 "title": "TestableRust",
265 "author": "JaneDoe",
266 "category": ["ComputerScience"],
267 "last_updated": "2024-07-10T12:34:56Z"
268 }
269 "#
270 .trim()
271 .to_string()
272 }
273
274 #[test]
275 fn test_read_config_file_missing() {
276 let dir = tempdir().unwrap();
277 let config_path = dir.path().join("oseda-config.json");
278
279 let result = read_config_file(&config_path);
280 assert!(matches!(result, Err(OsedaCheckError::MissingConfig(_))));
281 }
282
283 #[test]
284 fn test_validate_config_success() {
285 let conf = OsedaConfig {
286 title: "my-project".to_string(),
287 author: "JaneDoe".to_string(),
288 tags: vec![Tag::ComputerScience],
289 last_updated: chrono::Utc::now(),
290 color: Color::Black.into_hex(),
291 description: String::from("Test Description"),
292 };
293
294 let fake_dir = Path::new("/tmp/my-project");
295 let result = validate_config(&conf, fake_dir, false, || Some("JaneDoe".to_string()));
297
298 assert!(result.is_ok());
299 }
300
301 #[test]
302 fn test_validate_config_bad_git_user() {
303 let conf = OsedaConfig {
304 title: "my-project".to_string(),
305 author: "JaneDoe".to_string(),
306 tags: vec![Tag::ComputerScience],
307 last_updated: chrono::Utc::now(),
308 color: Color::Black.into_hex(),
309 description: String::from("Test Description"),
310 };
311
312 let fake_dir = Path::new("/tmp/oseda");
313
314 let result = validate_config(&conf, fake_dir, false, || Some("NotJane".to_string()));
315
316 assert!(matches!(result, Err(OsedaCheckError::BadGitCredentials(_))));
317 }
318
319 #[test]
320 fn test_validate_config_bad_dir_name() {
321 let conf = OsedaConfig {
322 title: "correct-name".to_string(),
323 author: "JaneDoe".to_string(),
324 tags: vec![Tag::ComputerScience],
325 last_updated: chrono::Utc::now(),
326 color: Color::Black.into_hex(),
327 description: String::new(),
328 };
329
330 let fake_dir = Path::new("/tmp/wrong-name");
331
332 let result = validate_config(&conf, fake_dir, false, || Some("JaneDoe".to_string()));
333 assert!(matches!(
334 result,
335 Err(OsedaCheckError::DirectoryNameMismatch(_))
336 ));
337 }
338
339 #[test]
340 fn test_validate_config_skip_git() {
341 let conf = OsedaConfig {
342 title: "oseda".to_string(),
343 author: "JaneDoe".to_string(),
344 tags: vec![Tag::ComputerScience],
345 last_updated: chrono::Utc::now(),
346 color: Color::Black.into_hex(),
347 description: String::from("Test Description"),
348 };
349
350 let fake_dir = Path::new("/tmp/oseda");
351
352 let result = validate_config(&conf, fake_dir, true, || None);
353 assert!(result.is_ok());
354 }
355}