diff --git a/README.md b/README.md index 7cba4907c..9da5af7b7 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,9 @@ Cookbook has special config to avoid repetitive args, place this file into `cook # This is a configuration file to avoid repetitively spelling command args. # At the moment only mirrors here implemented but in future it will be expanded when scripts are rusted -[[mirrors]] -# https://www.gnu.org/prep/ftp.en.html -"ftp.gnu.com" = "example.com/gnu" +[mirrors] +# see list of GNU FTP mirrors at https://www.gnu.org/prep/ftp.en.html +"ftp.gnu.org/gnu" = "example.com/gnu" "github.com/foo/bar" = "github.com/baz/bar" ``` diff --git a/src/config.rs b/src/config.rs index fb07ce2aa..54d6f8370 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,9 +10,15 @@ pub struct CookbookConfig { static CONFIG: OnceLock = OnceLock::new(); pub fn init_config() { - let config: CookbookConfig = { - let toml_content = fs::read_to_string("cookbook.toml").unwrap_or("".to_owned()); - toml::from_str(&toml_content).unwrap_or(CookbookConfig::default()) + let config: CookbookConfig = if fs::exists("cookbook.toml").unwrap_or(false) { + let toml_content = fs::read_to_string("cookbook.toml") + .map_err(|e| format!("Unable to read config: {:?}", e)) + .unwrap(); + toml::from_str(&toml_content) + .map_err(|e| format!("Unable to parse config: {:?}", e)) + .unwrap() + } else { + CookbookConfig::default() }; CONFIG.set(config).expect("config is initialized twice"); @@ -57,16 +63,13 @@ mod tests { use super::*; fn setup_test_config() { - let mut mirrors = HashMap::new(); - mirrors.insert("ftp.gnu.com".to_string(), "example.com/gnu".to_string()); - mirrors.insert( - "github.com/foo/bar".to_string(), - "github.com/baz/bar".to_string(), - ); - mirrors.insert("github.com/a".to_string(), "github.com/b".to_string()); - - let app_config = CookbookConfig { mirrors }; - + let app_config = toml::from_str( + "[mirrors]\n\ + \"ftp.gnu.org/gnu\" = \"example.com/gnu\"\n\ + \"github.com/foo/bar\" = \"github.com/baz/bar\"\n\ + \"github.com/a\" = \"github.com/b\"\n", + ) + .expect("Unable to parse test config"); // This will be called for each test. If the config is already set, // it will do nothing, which is fine as all tests use the same config. let _ = CONFIG.set(app_config); @@ -75,7 +78,7 @@ mod tests { #[test] fn test_exact_match() { setup_test_config(); - assert_eq!(translate_mirror("ftp.gnu.com"), "example.com/gnu"); + assert_eq!(translate_mirror("ftp.gnu.org/gnu"), "example.com/gnu"); assert_eq!(translate_mirror("github.com/foo/bar"), "github.com/baz/bar"); } @@ -87,8 +90,8 @@ mod tests { "https://github.com/b/c" ); assert_eq!( - translate_mirror("https://ftp.gnu.com/path/to/file"), - "https://example.com/gnu/path/to/file" + translate_mirror("https://ftp.gnu.org/gnu/bash/bash-5.2.15.tar.gz"), + "https://example.com/gnu/bash/bash-5.2.15.tar.gz" ); }