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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
use std::io::Read; use std::{collections, fs, path}; use std::fmt; use std::result; use serde::de::{self, Deserialize}; use errors::*; #[derive(Debug)] pub struct TestData { pub base: path::PathBuf, pub source: String, pub target: String, pub copy_git_ignored: bool, } #[derive(Serialize, Debug, Clone)] pub struct TestDataConfiguration { pub source: String, pub copy_git_ignored: bool, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct DetailedTestDataConfiguration { pub source: String, pub copy_git_ignored: bool, } impl<'de> de::Deserialize<'de> for TestDataConfiguration { fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error> where D: de::Deserializer<'de>, { struct TestDataVisitor; impl<'de> de::Visitor<'de> for TestDataVisitor { type Value = TestDataConfiguration; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str( "a path like \"tests/my_test_data\" or a \ detailed dependency like { source = \ \"tests/my_test_data\", copy_git_ignored = true }", ) } fn visit_str<E>(self, s: &str) -> result::Result<Self::Value, E> where E: de::Error, { Ok(TestDataConfiguration { source: s.to_owned(), copy_git_ignored: false, }) } fn visit_map<V>(self, map: V) -> result::Result<Self::Value, V::Error> where V: de::MapAccess<'de>, { let mvd = de::value::MapAccessDeserializer::new(map); let detailed = DetailedTestDataConfiguration::deserialize(mvd)?; Ok(TestDataConfiguration { source: detailed.source, copy_git_ignored: detailed.copy_git_ignored, }) } } deserializer.deserialize_any(TestDataVisitor) } } #[derive(Debug, Default)] pub struct Configuration { pub ssh_devices: collections::BTreeMap<String, SshDeviceConfiguration>, pub test_data: Vec<TestData>, } #[derive(Serialize, Deserialize, Debug, Default)] struct ConfigurationFileContent { pub ssh_devices: Option<collections::BTreeMap<String, SshDeviceConfiguration>>, pub test_data: Option<collections::BTreeMap<String, TestDataConfiguration>>, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct SshDeviceConfiguration { pub hostname: String, pub username: String, pub target: String, pub port: Option<u16>, pub path: Option<String>, } impl Configuration { fn merge(&mut self, file: &path::Path, other: ConfigurationFileContent) { self.ssh_devices.extend(other.ssh_devices.unwrap_or(collections::BTreeMap::new()) ); for (target, source) in other.test_data.unwrap_or(collections::BTreeMap::new()) { self.test_data.push(TestData { base: file.to_path_buf(), source: source.source, target: target, copy_git_ignored: source.copy_git_ignored, }) } } } fn read_config_file<P: AsRef<path::Path>>(file: P) -> Result<ConfigurationFileContent> { let mut data = String::new(); let mut fd = fs::File::open(file)?; fd.read_to_string(&mut data)?; Ok(::toml::from_str(&data)?) } pub fn config<P: AsRef<path::Path>>(dir: P) -> Result<Configuration> { let mut conf = Configuration::default(); let mut files_to_try = vec![]; let dir = dir.as_ref().to_path_buf(); let mut d = dir.as_path(); while d.parent().is_some() { files_to_try.push(d.join(".dinghy.toml")); d = d.parent().unwrap(); } files_to_try.push(d.join(".dinghy.toml")); if let Some(home) = ::std::env::home_dir() { files_to_try.push(home.join(".dinghy.html")) } for file in files_to_try { if path::Path::new(&file).exists() { info!("Loading configuration from {:?}", file); conf.merge(&file, read_config_file(&file)?); } else { debug!("No configuration found at {:?}", file); } } Ok(conf) } #[cfg(test)] mod tests { #[test] fn load_config_with_str_test_data() { let config_file = ::std::env::current_exe().unwrap().parent().unwrap().join( "../../../test-ws/test-app/.dinghy.toml", ); super::read_config_file(config_file).unwrap(); } }