| 98286406 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use super::*; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 9 |
pub struct Config { |
| 10 |
|
| 11 |
repo_layer: BTreeMap<String, String>, |
| 12 |
pub author_name: String, |
| 13 |
pub author_email: String, |
| 14 |
|
| 15 |
pub author_key: Option<KeyId>, |
| ff1a64f8 Andy McCall ↶ |
16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
pub signer_key: Option<KeyId>, |
| 98286406 Andy McCall ↶ |
21 |
|
| 22 |
|
| 23 |
pub sign_changes: bool, |
| 24 |
pub bookmark: String, |
| 25 |
|
| 26 |
pub remotes: BTreeMap<String, String>, |
| 27 |
|
| 28 |
pub remote_tokens: BTreeMap<String, String>, |
| 2b5ee371 Andy McCall ↶ |
29 |
|
| 30 |
pub remote_cas: BTreeMap<String, String>, |
| ba160f60 Andy McCall ↶ |
31 |
|
| 32 |
pub op_retention_days: u64, |
| 33 |
|
| 34 |
pub gc_grace_days: u64, |
| 252e8cb2 Andy McCall ↶ |
35 |
|
| 36 |
pub checkpoint_interval: u64, |
| b3c5a59a Andy McCall ↶ |
37 |
|
| 38 |
pub max_blob: Option<u64>, |
| 7421309f Andy McCall ↶ |
39 |
|
| 40 |
pub sparse_paths: Vec<String>, |
| 6d3eefbc Andy McCall ↶ |
41 |
|
| 42 |
pub semantic_deps: bool, |
| 47ce8b8e Andy McCall ↶ |
43 |
|
| 44 |
pub revsets: BTreeMap<String, String>, |
| 98286406 Andy McCall ↶ |
45 |
} |
| 46 |
|
| 47 |
impl Config { |
| 48 |
pub(super) fn default_for_user() -> Config { |
| 49 |
let user = std::env::var("USER").unwrap_or_else(|_| "unknown".into()); |
| 50 |
Config { |
| 51 |
repo_layer: BTreeMap::new(), |
| 52 |
author_name: std::env::var("OXY_AUTHOR_NAME").unwrap_or_else(|_| user.clone()), |
| 75167672 Andy McCall |
53 |
author_email: std::env::var("OXY_AUTHOR_EMAIL") |
| 54 |
.unwrap_or_else(|_| format!("{user}@localhost")), |
| 98286406 Andy McCall ↶ |
55 |
author_key: None, |
| ff1a64f8 Andy McCall ↶ |
56 |
signer_key: std::env::var("OXY_SIGNER") |
| 57 |
.ok() |
| 58 |
.and_then(|v| KeyId::from_hex(&v)), |
| 98286406 Andy McCall ↶ |
59 |
sign_changes: false, |
| 60 |
bookmark: "main".into(), |
| 61 |
remotes: BTreeMap::new(), |
| 62 |
remote_tokens: BTreeMap::new(), |
| 2b5ee371 Andy McCall ↶ |
63 |
remote_cas: BTreeMap::new(), |
| ba160f60 Andy McCall ↶ |
64 |
op_retention_days: 90, |
| 65 |
gc_grace_days: 14, |
| 252e8cb2 Andy McCall ↶ |
66 |
checkpoint_interval: 256, |
| b3c5a59a Andy McCall ↶ |
67 |
max_blob: None, |
| 7421309f Andy McCall ↶ |
68 |
sparse_paths: Vec::new(), |
| 6d3eefbc Andy McCall ↶ |
69 |
semantic_deps: false, |
| 47ce8b8e Andy McCall ↶ |
70 |
revsets: BTreeMap::new(), |
| 98286406 Andy McCall ↶ |
71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
pub fn set(&mut self, key: &str, value: &str) -> Result<()> { |
| 76 |
self.apply(key, value)?; |
| 77 |
if value.is_empty() { |
| 78 |
self.repo_layer.remove(key); |
| 79 |
} else { |
| 80 |
self.repo_layer.insert(key.to_string(), value.to_string()); |
| 81 |
} |
| 82 |
Ok(()) |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
pub fn set_user(user_path: &Path, key: &str, value: &str) -> Result<()> { |
| 87 |
Config::default_for_user().apply(key, value)?; |
| 88 |
let mut entries = read_config_file(user_path)?; |
| 89 |
if value.is_empty() { |
| 90 |
entries.remove(key); |
| 91 |
} else { |
| 92 |
entries.insert(key.to_string(), value.to_string()); |
| 93 |
} |
| 94 |
if let Some(parent) = user_path.parent() { |
| 95 |
fs::create_dir_all(parent)?; |
| 96 |
} |
| 97 |
write_config_file(user_path, &entries) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
pub(super) fn apply(&mut self, key: &str, value: &str) -> Result<()> { |
| 102 |
match key { |
| 103 |
"author.name" => self.author_name = value.into(), |
| 104 |
"author.email" => self.author_email = value.into(), |
| 105 |
"author.key" => { |
| 106 |
self.author_key = if value.is_empty() { |
| 107 |
None |
| 108 |
} else { |
| 75167672 Andy McCall |
109 |
Some(KeyId::from_hex(value).ok_or_else(|| { |
| 110 |
RepoError::Msg("author.key must be a 64-character hex KeyId".into()) |
| 111 |
})?) |
| 98286406 Andy McCall ↶ |
112 |
} |
| 113 |
} |
| ff1a64f8 Andy McCall ↶ |
114 |
"author.signer" => { |
| 115 |
self.signer_key = if value.is_empty() { |
| 116 |
None |
| 117 |
} else { |
| 118 |
Some(KeyId::from_hex(value).ok_or_else(|| { |
| 119 |
RepoError::Msg("author.signer must be a 64-character hex KeyId".into()) |
| 120 |
})?) |
| 121 |
} |
| 122 |
} |
| 98286406 Andy McCall ↶ |
123 |
"core.sign" => { |
| 124 |
self.sign_changes = match value { |
| 125 |
"change" => true, |
| 126 |
"seal" => false, |
| 127 |
_ => return fail("core.sign must be \"change\" or \"seal\""), |
| 128 |
} |
| 129 |
} |
| 130 |
"workspace.bookmark" => self.bookmark = value.into(), |
| ff1f62fe Andy McCall ↶ |
131 |
|
| 132 |
"auth.helper" => {} |
| ba160f60 Andy McCall ↶ |
133 |
"core.op-retention" => self.op_retention_days = parse_days(value)?, |
| 134 |
"core.gc-grace" => self.gc_grace_days = parse_days(value)?, |
| 75167672 Andy McCall |
135 |
"filter.max-blob" => { |
| 136 |
self.max_blob = if value.is_empty() { |
| 137 |
None |
| 138 |
} else { |
| 139 |
Some(parse_size(value)?) |
| 140 |
} |
| 141 |
} |
| 6d3eefbc Andy McCall ↶ |
142 |
"hooks.semantic-deps" => self.semantic_deps = matches!(value, "true" | "1" | "yes"), |
| 7421309f Andy McCall ↶ |
143 |
"filter.paths" => { |
| 144 |
self.sparse_paths = value |
| 145 |
.split(',') |
| 75167672 Andy McCall |
146 |
.map(|p| { |
| 147 |
p.trim() |
| 148 |
.trim_start_matches("./") |
| 149 |
.trim_matches('/') |
| 150 |
.to_string() |
| 151 |
}) |
| 7421309f Andy McCall ↶ |
152 |
.filter(|p| !p.is_empty()) |
| 153 |
.collect(); |
| 154 |
} |
| 252e8cb2 Andy McCall ↶ |
155 |
"core.checkpoint-interval" => { |
| 75167672 Andy McCall |
156 |
self.checkpoint_interval = value |
| 157 |
.parse() |
| 158 |
.map_err(|_| RepoError::Msg(format!("expected a number, got {value:?}")))?; |
| 252e8cb2 Andy McCall ↶ |
159 |
} |
| 2b5ee371 Andy McCall ↶ |
160 |
k if k.starts_with("remote.") => { |
| 161 |
let rest = &k["remote.".len()..]; |
| 162 |
let Some((name, field)) = rest.rsplit_once('.') else { |
| 163 |
return fail(format!("unknown configuration key {key:?}")); |
| 98286406 Andy McCall ↶ |
164 |
}; |
| 165 |
if name.is_empty() || name.contains('/') { |
| 166 |
return fail(format!("invalid remote name in {key:?}")); |
| 167 |
} |
| 2b5ee371 Andy McCall ↶ |
168 |
let map = match field { |
| 169 |
"url" => &mut self.remotes, |
| 170 |
"token" => &mut self.remote_tokens, |
| 171 |
"ca" => &mut self.remote_cas, |
| 172 |
_ => return fail(format!("unknown configuration key {key:?}")), |
| 173 |
}; |
| 98286406 Andy McCall ↶ |
174 |
if value.is_empty() { |
| 175 |
map.remove(name); |
| 2b5ee371 Andy McCall ↶ |
176 |
if field == "url" { |
| 98286406 Andy McCall ↶ |
177 |
self.remote_tokens.remove(name); |
| 2b5ee371 Andy McCall ↶ |
178 |
self.remote_cas.remove(name); |
| 98286406 Andy McCall ↶ |
179 |
} |
| 180 |
} else { |
| 181 |
map.insert(name.to_string(), value.into()); |
| 182 |
} |
| 183 |
} |
| 47ce8b8e Andy McCall ↶ |
184 |
k if k.starts_with("revset.") => { |
| 185 |
let name = &k["revset.".len()..]; |
| 186 |
if name.is_empty() |
| 187 |
|| !name |
| 188 |
.chars() |
| 189 |
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') |
| 190 |
{ |
| 191 |
return fail(format!( |
| 192 |
"a revset's name is letters, digits, _ and -: {key:?}" |
| 193 |
)); |
| 194 |
} |
| 195 |
if value.is_empty() { |
| 196 |
self.revsets.remove(name); |
| 197 |
} else { |
| 198 |
crate::revset::parse(value) |
| 199 |
.map_err(|e| RepoError::Msg(format!("{key} = {value:?}: {e}")))?; |
| 200 |
self.revsets.insert(name.to_string(), value.into()); |
| 201 |
} |
| 202 |
} |
| 98286406 Andy McCall ↶ |
203 |
_ => return fail(format!("unknown configuration key {key:?}")), |
| 204 |
} |
| 205 |
Ok(()) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
pub(super) fn load(user_path: Option<&Path>, repo_path: &Path) -> Result<Config> { |
| 210 |
let mut c = Config::default_for_user(); |
| 211 |
if let Some(u) = user_path { |
| 212 |
for (k, v) in read_config_file(u)? { |
| 213 |
let _ = c.apply(&k, &v); |
| 214 |
} |
| 215 |
} |
| 216 |
for (k, v) in read_config_file(repo_path)? { |
| 217 |
if c.apply(&k, &v).is_ok() { |
| 218 |
c.repo_layer.insert(k, v); |
| 219 |
} |
| 220 |
} |
| 221 |
Ok(c) |
| 222 |
} |
| 223 |
|
| 224 |
pub(super) fn save(&self, path: &Path) -> Result<()> { |
| 225 |
write_config_file(path, &self.repo_layer) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
pub fn repo_layer(&self) -> &BTreeMap<String, String> { |
| 230 |
&self.repo_layer |
| 231 |
} |
| 232 |
} |
| 233 |
|
| b3c5a59a Andy McCall ↶ |
234 |
|
| 235 |
pub fn parse_size(v: &str) -> Result<u64> { |
| 236 |
let v = v.trim(); |
| 237 |
let (num, mult) = match v.chars().last() { |
| 238 |
Some('K' | 'k') => (&v[..v.len() - 1], 1u64 << 10), |
| 239 |
Some('M' | 'm') => (&v[..v.len() - 1], 1u64 << 20), |
| 240 |
Some('G' | 'g') => (&v[..v.len() - 1], 1u64 << 30), |
| 241 |
_ => (v, 1), |
| 242 |
}; |
| 75167672 Andy McCall |
243 |
let n: u64 = num |
| 244 |
.trim() |
| 245 |
.parse() |
| 246 |
.map_err(|_| RepoError::Msg(format!("expected a size like 1M, got {v:?}")))?; |
| b3c5a59a Andy McCall ↶ |
247 |
Ok(n * mult) |
| 248 |
} |
| 249 |
|
| ba160f60 Andy McCall ↶ |
250 |
|
| 251 |
fn parse_days(v: &str) -> Result<u64> { |
| 75167672 Andy McCall |
252 |
v.trim_end_matches('d') |
| 253 |
.parse() |
| 254 |
.map_err(|_| RepoError::Msg(format!("expected a number of days, got {v:?}"))) |
| ba160f60 Andy McCall ↶ |
255 |
} |
| 256 |
|
| 98286406 Andy McCall ↶ |
257 |
|
| 258 |
pub fn read_config_file(path: &Path) -> Result<BTreeMap<String, String>> { |
| 259 |
let text = match fs::read_to_string(path) { |
| 260 |
Ok(t) => t, |
| 261 |
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(BTreeMap::new()), |
| 262 |
Err(e) => return Err(e.into()), |
| 263 |
}; |
| 264 |
let mut out = BTreeMap::new(); |
| 265 |
for line in text.lines() { |
| 266 |
let line = line.trim(); |
| 267 |
if line.is_empty() || line.starts_with('#') { |
| 268 |
continue; |
| 269 |
} |
| 75167672 Andy McCall |
270 |
let Some((k, v)) = line.split_once('=') else { |
| 271 |
continue; |
| 272 |
}; |
| 98286406 Andy McCall ↶ |
273 |
out.insert(k.trim().to_string(), v.trim().trim_matches('"').to_string()); |
| 274 |
} |
| 275 |
Ok(out) |
| 276 |
} |
| 277 |
|
| 278 |
fn write_config_file(path: &Path, entries: &BTreeMap<String, String>) -> Result<()> { |
| 75167672 Andy McCall |
279 |
let text: String = entries |
| 280 |
.iter() |
| 281 |
.map(|(k, v)| format!("{k} = \"{v}\"\n")) |
| 282 |
.collect(); |
| 98286406 Andy McCall ↶ |
283 |
write_atomic(path, text.as_bytes()) |
| 284 |
} |