Skip to main content

oseda_cli/
config.rs

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
34/// Reads and validates an oseda-config.json file in the working directory
35///
36/// This checks a few things:
37/// - the file exists and parses correctly
38/// - the git `user.name` matches the config author (unless --skip-git is passed)
39/// - the config `title` matches the name of the working directory
40///
41/// # Arguments
42/// * `skip_git` - skips the git author validation, primarily used for CI, not by the end user hopefully lol
43///
44/// # Returns
45/// * `Ok(OsedaConfig)` if the file is valid and all checks pass
46/// * `Err(OsedaCheckError)` if any check fails
47pub 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    // very cool pass in a lambda, swap that lambda out in the tests
71    // https://danielbunte.medium.com/a-guide-to-testing-and-mocking-in-rust-a73d022b4075
72    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/// Structure for an oseda-config.json
108#[derive(Serialize, Deserialize)]
109pub struct OsedaConfig {
110    pub title: String,
111    pub author: String,
112    pub tags: Vec<Tag>,
113    // effectively mutable. Will get updated on each deployment
114    pub last_updated: DateTime<Utc>,
115    pub color: String,
116    // description must not be empty for check/deploy
117    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}
135/// Prompts the user for everything needed to generate a new OsedaConfig
136///
137/// # Returns
138/// * `Ok(OsedaConfig)` containing validated project config options
139/// * `Err` if a required input conf is invalid
140pub 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        // start them with empty description
173        description: String::new(),
174    })
175}
176
177/// Prompts user for categories associated with their Oseda project
178///
179/// # Returns
180/// * `Ok(Vec<Category>)` with selected categories
181/// * `Err` if the prompting went wrong somewhere
182fn 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
211/// Updates the configs last-updated
212/// Currently this is used on creation only, TODO fix this
213///
214/// # Arguments
215/// * `conf` - a previously loaded or generated OsedaConfig
216///
217/// # Returns
218/// * `Ok(())` if the file is successfully updated
219/// * `Err` if file writing fails
220pub 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
227/// Gets the current system time in UTC
228///
229/// # Returns
230/// * a `DateTime<Utc>` representing the current time
231fn get_time() -> DateTime<Utc> {
232    chrono::offset::Utc::now()
233}
234
235/// Write an OsedaConfig to the provided directory
236///
237/// # Arguments
238/// * `path` - the directory path to write into
239/// * `conf` - the `OsedaConfig` instance to serialize via serde
240///
241/// # Returns            color: Color::Black
242/// * `Ok(())` if the file is written successfully
243/// * `Err` if file creation or serialization fails
244pub 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        // can mock the git credentials easier
296        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}