| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use std::path::{Path, PathBuf}; |
| 9 |
use std::time::Duration; |
| 10 |
|
| 11 |
use anyhow::{Context, Result}; |
| 12 |
use serde::Deserialize; |
| 13 |
|
| 14 |
|
| 15 |
const DEFAULT_POLL_SECS: u64 = 5; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
const DEFAULT_STALE_SECS: u64 = 60; |
| 23 |
|
| 24 |
#[derive(Debug, Clone, Deserialize)] |
| 25 |
pub(crate) struct Config { |
| 26 |
|
| 27 |
#[serde(default = "default_stale")] |
| 28 |
pub stale_after_secs: u64, |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#[serde(default)] |
| 37 |
pub theme: Option<String>, |
| 38 |
#[serde(default, rename = "source")] |
| 39 |
pub sources: Vec<Source>, |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 52 |
pub(crate) enum Origin { |
| 53 |
|
| 54 |
Http(String), |
| 55 |
File(PathBuf), |
| 56 |
} |
| 57 |
|
| 58 |
#[derive(Debug, Clone, Deserialize)] |
| 59 |
pub(crate) struct Source { |
| 60 |
|
| 61 |
pub name: String, |
| 62 |
|
| 63 |
|
| 64 |
#[serde(default)] |
| 65 |
pub url: Option<String>, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#[serde(default)] |
| 72 |
pub path: Option<PathBuf>, |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#[serde(default)] |
| 81 |
pub token_env: Option<String>, |
| 82 |
#[serde(default = "default_poll")] |
| 83 |
pub poll_secs: u64, |
| 84 |
#[serde(default)] |
| 85 |
pub stale_after_secs: Option<u64>, |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#[serde(default)] |
| 95 |
pub allow_actions: bool, |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
fn expand_tilde(p: &Path) -> PathBuf { |
| 104 |
let Ok(rest) = p.strip_prefix("~") else { |
| 105 |
return p.to_path_buf(); |
| 106 |
}; |
| 107 |
match std::env::var_os("HOME") { |
| 108 |
Some(home) => PathBuf::from(home).join(rest), |
| 109 |
None => p.to_path_buf(), |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
fn default_poll() -> u64 { |
| 114 |
DEFAULT_POLL_SECS |
| 115 |
} |
| 116 |
|
| 117 |
fn default_stale() -> u64 { |
| 118 |
DEFAULT_STALE_SECS |
| 119 |
} |
| 120 |
|
| 121 |
impl Source { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
pub(crate) fn origin(&self) -> Origin { |
| 128 |
match (&self.url, &self.path) { |
| 129 |
(Some(url), _) => Origin::Http(format!("{}/status.json", url.trim_end_matches('/'))), |
| 130 |
(None, Some(path)) => Origin::File(path.clone()), |
| 131 |
(None, None) => unreachable!("validate rejects a source with neither url nor path"), |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
pub(crate) fn poll_interval(&self) -> Duration { |
| 136 |
Duration::from_secs(self.poll_secs.max(1)) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
pub(crate) fn action_url(&self, path: &str) -> String { |
| 149 |
if path.starts_with("http://") || path.starts_with("https://") { |
| 150 |
return path.to_string(); |
| 151 |
} |
| 152 |
let Some(base) = self.url.as_deref() else { |
| 153 |
return path.to_string(); |
| 154 |
}; |
| 155 |
format!( |
| 156 |
"{}/{}", |
| 157 |
base.trim_end_matches('/'), |
| 158 |
path.trim_start_matches('/') |
| 159 |
) |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
pub(crate) fn token(&self) -> Option<String> { |
| 164 |
self.token_env |
| 165 |
.as_deref() |
| 166 |
.and_then(|name| std::env::var(name).ok()) |
| 167 |
.filter(|t| !t.is_empty()) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
impl Config { |
| 172 |
pub(crate) fn load(path: &Path) -> Result<Self> { |
| 173 |
let raw = std::fs::read_to_string(path) |
| 174 |
.with_context(|| format!("reading magicmirror config at {}", path.display()))?; |
| 175 |
let mut cfg: Config = toml::from_str(&raw) |
| 176 |
.with_context(|| format!("parsing magicmirror config at {}", path.display()))?; |
| 177 |
for source in &mut cfg.sources { |
| 178 |
if let Some(p) = &source.path { |
| 179 |
source.path = Some(expand_tilde(p)); |
| 180 |
} |
| 181 |
} |
| 182 |
cfg.validate()?; |
| 183 |
Ok(cfg) |
| 184 |
} |
| 185 |
|
| 186 |
fn validate(&self) -> Result<()> { |
| 187 |
anyhow::ensure!( |
| 188 |
!self.sources.is_empty(), |
| 189 |
"no [[source]] entries: magicmirror would have nothing to show" |
| 190 |
); |
| 191 |
let mut seen = std::collections::HashSet::new(); |
| 192 |
for source in &self.sources { |
| 193 |
anyhow::ensure!( |
| 194 |
seen.insert(source.name.as_str()), |
| 195 |
"duplicate source name {:?}: tabs would be ambiguous", |
| 196 |
source.name |
| 197 |
); |
| 198 |
match (&source.url, &source.path) { |
| 199 |
(Some(url), None) => anyhow::ensure!( |
| 200 |
url.starts_with("http://") || url.starts_with("https://"), |
| 201 |
"source {:?} url must start with http:// or https://", |
| 202 |
source.name |
| 203 |
), |
| 204 |
(None, Some(_)) => { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
anyhow::ensure!( |
| 211 |
source.token_env.is_none(), |
| 212 |
"source {:?} is a file and cannot present a bearer token", |
| 213 |
source.name |
| 214 |
); |
| 215 |
anyhow::ensure!( |
| 216 |
!source.allow_actions, |
| 217 |
"source {:?} is a file: an action is a URL and there is no base to \ |
| 218 |
resolve one against", |
| 219 |
source.name |
| 220 |
); |
| 221 |
} |
| 222 |
(Some(_), Some(_)) => anyhow::bail!( |
| 223 |
"source {:?} sets both url and path: one payload, one origin", |
| 224 |
source.name |
| 225 |
), |
| 226 |
(None, None) => anyhow::bail!( |
| 227 |
"source {:?} sets neither url nor path: it could never say anything", |
| 228 |
source.name |
| 229 |
), |
| 230 |
} |
| 231 |
} |
| 232 |
Ok(()) |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
pub(crate) fn theme_selection(&self) -> makeover::ThemeSelection { |
| 239 |
makeover::ThemeSelection::parse(self.theme.as_deref()) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
pub(crate) fn stale_after(&self, source: &Source) -> chrono::TimeDelta { |
| 244 |
let secs = source.stale_after_secs.unwrap_or(self.stale_after_secs); |
| 245 |
chrono::TimeDelta::seconds(secs as i64) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
#[cfg(test)] |
| 250 |
mod tests { |
| 251 |
use super::*; |
| 252 |
|
| 253 |
fn write(body: &str) -> (tempfile::TempDir, std::path::PathBuf) { |
| 254 |
let dir = tempfile::tempdir().unwrap(); |
| 255 |
let path = dir.path().join("magicmirror.toml"); |
| 256 |
std::fs::write(&path, body).unwrap(); |
| 257 |
(dir, path) |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn a_minimal_source_gets_sensible_defaults() { |
| 262 |
let (_dir, path) = write( |
| 263 |
r#" |
| 264 |
[[source]] |
| 265 |
name = "sando" |
| 266 |
url = "http://fw13:8080" |
| 267 |
"#, |
| 268 |
); |
| 269 |
let cfg = Config::load(&path).unwrap(); |
| 270 |
assert_eq!( |
| 271 |
cfg.sources[0].origin(), |
| 272 |
Origin::Http("http://fw13:8080/status.json".into()) |
| 273 |
); |
| 274 |
assert_eq!(cfg.sources[0].poll_interval().as_secs(), DEFAULT_POLL_SECS); |
| 275 |
assert_eq!( |
| 276 |
cfg.stale_after(&cfg.sources[0]), |
| 277 |
chrono::TimeDelta::seconds(DEFAULT_STALE_SECS as i64) |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn an_action_path_resolves_against_the_base_and_a_full_url_is_left_alone() { |
| 283 |
let source: Source = toml::from_str( |
| 284 |
r#" |
| 285 |
name = "sando" |
| 286 |
url = "http://fw13:8080/" |
| 287 |
"#, |
| 288 |
) |
| 289 |
.unwrap(); |
| 290 |
assert_eq!( |
| 291 |
source.action_url("/rollback/b"), |
| 292 |
"http://fw13:8080/rollback/b" |
| 293 |
); |
| 294 |
assert_eq!( |
| 295 |
source.action_url("rollback/b"), |
| 296 |
"http://fw13:8080/rollback/b" |
| 297 |
); |
| 298 |
assert_eq!( |
| 299 |
source.action_url("https://elsewhere/x"), |
| 300 |
"https://elsewhere/x" |
| 301 |
); |
| 302 |
} |
| 303 |
|
| 304 |
#[test] |
| 305 |
fn a_trailing_slash_does_not_double_up() { |
| 306 |
let (_dir, path) = write( |
| 307 |
r#" |
| 308 |
[[source]] |
| 309 |
name = "bento" |
| 310 |
url = "http://fw13:8090/" |
| 311 |
"#, |
| 312 |
); |
| 313 |
let cfg = Config::load(&path).unwrap(); |
| 314 |
assert_eq!( |
| 315 |
cfg.sources[0].origin(), |
| 316 |
Origin::Http("http://fw13:8090/status.json".into()) |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
#[test] |
| 323 |
fn a_source_can_be_a_file_on_disk() { |
| 324 |
let (_dir, path) = write( |
| 325 |
r#" |
| 326 |
[[source]] |
| 327 |
name = "witchbroom" |
| 328 |
path = "/var/lib/witchbroom/status.json" |
| 329 |
poll_secs = 60 |
| 330 |
stale_after_secs = 129600 |
| 331 |
"#, |
| 332 |
); |
| 333 |
let cfg = Config::load(&path).unwrap(); |
| 334 |
assert_eq!( |
| 335 |
cfg.sources[0].origin(), |
| 336 |
Origin::File("/var/lib/witchbroom/status.json".into()) |
| 337 |
); |
| 338 |
|
| 339 |
|
| 340 |
assert_eq!(cfg.stale_after(&cfg.sources[0]).num_seconds(), 129_600); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
#[test] |
| 346 |
fn a_leading_tilde_in_a_path_expands() { |
| 347 |
let home = std::env::var("HOME").unwrap(); |
| 348 |
let (_dir, path) = write( |
| 349 |
r#" |
| 350 |
[[source]] |
| 351 |
name = "witchbroom" |
| 352 |
path = "~/.local/state/witchbroom/status.json" |
| 353 |
"#, |
| 354 |
); |
| 355 |
let cfg = Config::load(&path).unwrap(); |
| 356 |
assert_eq!( |
| 357 |
cfg.sources[0].origin(), |
| 358 |
Origin::File( |
| 359 |
std::path::PathBuf::from(home).join(".local/state/witchbroom/status.json") |
| 360 |
) |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
#[test] |
| 367 |
fn a_source_sets_exactly_one_of_url_and_path() { |
| 368 |
let (_dir, path) = write( |
| 369 |
r#" |
| 370 |
[[source]] |
| 371 |
name = "witchbroom" |
| 372 |
url = "http://astra:9000" |
| 373 |
path = "/var/lib/witchbroom/status.json" |
| 374 |
"#, |
| 375 |
); |
| 376 |
assert!( |
| 377 |
Config::load(&path) |
| 378 |
.unwrap_err() |
| 379 |
.to_string() |
| 380 |
.contains("one payload, one origin") |
| 381 |
); |
| 382 |
|
| 383 |
let (_dir, path) = write( |
| 384 |
r#" |
| 385 |
[[source]] |
| 386 |
name = "witchbroom" |
| 387 |
"#, |
| 388 |
); |
| 389 |
assert!( |
| 390 |
Config::load(&path) |
| 391 |
.unwrap_err() |
| 392 |
.to_string() |
| 393 |
.contains("neither url nor path") |
| 394 |
); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
#[test] |
| 400 |
fn a_file_source_refuses_a_token_and_refuses_actions() { |
| 401 |
let (_dir, path) = write( |
| 402 |
r#" |
| 403 |
[[source]] |
| 404 |
name = "witchbroom" |
| 405 |
path = "/var/lib/witchbroom/status.json" |
| 406 |
token_env = "WITCHBROOM_TOKEN" |
| 407 |
"#, |
| 408 |
); |
| 409 |
assert!( |
| 410 |
Config::load(&path) |
| 411 |
.unwrap_err() |
| 412 |
.to_string() |
| 413 |
.contains("bearer token") |
| 414 |
); |
| 415 |
|
| 416 |
let (_dir, path) = write( |
| 417 |
r#" |
| 418 |
[[source]] |
| 419 |
name = "witchbroom" |
| 420 |
path = "/var/lib/witchbroom/status.json" |
| 421 |
allow_actions = true |
| 422 |
"#, |
| 423 |
); |
| 424 |
assert!( |
| 425 |
Config::load(&path) |
| 426 |
.unwrap_err() |
| 427 |
.to_string() |
| 428 |
.contains("no base to resolve") |
| 429 |
); |
| 430 |
} |
| 431 |
|
| 432 |
#[test] |
| 433 |
fn a_per_source_staleness_overrides_the_default() { |
| 434 |
let (_dir, path) = write( |
| 435 |
r#" |
| 436 |
stale_after_secs = 60 |
| 437 |
|
| 438 |
[[source]] |
| 439 |
name = "sando" |
| 440 |
url = "http://fw13:8080" |
| 441 |
|
| 442 |
[[source]] |
| 443 |
name = "pom" |
| 444 |
url = "http://pom:9000" |
| 445 |
stale_after_secs = 600 |
| 446 |
"#, |
| 447 |
); |
| 448 |
let cfg = Config::load(&path).unwrap(); |
| 449 |
assert_eq!(cfg.stale_after(&cfg.sources[0]).num_seconds(), 60); |
| 450 |
assert_eq!(cfg.stale_after(&cfg.sources[1]).num_seconds(), 600); |
| 451 |
} |
| 452 |
|
| 453 |
#[test] |
| 454 |
fn an_absent_theme_key_follows_the_terminal_and_an_id_pins_one() { |
| 455 |
let (_dir, path) = write( |
| 456 |
r#" |
| 457 |
[[source]] |
| 458 |
name = "sando" |
| 459 |
url = "http://fw13:8080" |
| 460 |
"#, |
| 461 |
); |
| 462 |
let cfg = Config::load(&path).unwrap(); |
| 463 |
assert_eq!( |
| 464 |
cfg.theme_selection(), |
| 465 |
makeover::ThemeSelection::Follow, |
| 466 |
"a config that says nothing about theming must track the terminal, \ |
| 467 |
not pin whatever the first run guessed", |
| 468 |
); |
| 469 |
|
| 470 |
let (_dir, path) = write( |
| 471 |
r#" |
| 472 |
theme = "carbonfox" |
| 473 |
|
| 474 |
[[source]] |
| 475 |
name = "sando" |
| 476 |
url = "http://fw13:8080" |
| 477 |
"#, |
| 478 |
); |
| 479 |
assert_eq!( |
| 480 |
Config::load(&path).unwrap().theme_selection(), |
| 481 |
makeover::ThemeSelection::Fixed("carbonfox".into()), |
| 482 |
); |
| 483 |
} |
| 484 |
|
| 485 |
#[test] |
| 486 |
fn an_empty_config_is_rejected_rather_than_showing_an_empty_screen() { |
| 487 |
let (_dir, path) = write("stale_after_secs = 60\n"); |
| 488 |
assert!( |
| 489 |
Config::load(&path) |
| 490 |
.unwrap_err() |
| 491 |
.to_string() |
| 492 |
.contains("no [[source]]") |
| 493 |
); |
| 494 |
} |
| 495 |
|
| 496 |
#[test] |
| 497 |
fn duplicate_source_names_are_rejected() { |
| 498 |
let (_dir, path) = write( |
| 499 |
r#" |
| 500 |
[[source]] |
| 501 |
name = "sando" |
| 502 |
url = "http://a" |
| 503 |
[[source]] |
| 504 |
name = "sando" |
| 505 |
url = "http://b" |
| 506 |
"#, |
| 507 |
); |
| 508 |
assert!( |
| 509 |
Config::load(&path) |
| 510 |
.unwrap_err() |
| 511 |
.to_string() |
| 512 |
.contains("duplicate") |
| 513 |
); |
| 514 |
} |
| 515 |
|
| 516 |
#[test] |
| 517 |
fn a_url_without_a_scheme_is_rejected() { |
| 518 |
let (_dir, path) = write( |
| 519 |
r#" |
| 520 |
[[source]] |
| 521 |
name = "sando" |
| 522 |
url = "fw13:8080" |
| 523 |
"#, |
| 524 |
); |
| 525 |
assert!( |
| 526 |
Config::load(&path) |
| 527 |
.unwrap_err() |
| 528 |
.to_string() |
| 529 |
.contains("http://") |
| 530 |
); |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn actions_are_off_unless_a_source_opts_in() { |
| 535 |
let (_dir, path) = write( |
| 536 |
r#" |
| 537 |
[[source]] |
| 538 |
name = "pom" |
| 539 |
url = "http://pom:9000" |
| 540 |
|
| 541 |
[[source]] |
| 542 |
name = "sando" |
| 543 |
url = "http://fw13:8080" |
| 544 |
allow_actions = true |
| 545 |
"#, |
| 546 |
); |
| 547 |
let cfg = Config::load(&path).unwrap(); |
| 548 |
assert!( |
| 549 |
!cfg.sources[0].allow_actions, |
| 550 |
"a source must not be able to move production by default" |
| 551 |
); |
| 552 |
assert!(cfg.sources[1].allow_actions); |
| 553 |
} |
| 554 |
|
| 555 |
#[test] |
| 556 |
fn a_token_is_read_from_the_environment_never_the_file() { |
| 557 |
let (_dir, path) = write( |
| 558 |
r#" |
| 559 |
[[source]] |
| 560 |
name = "sando" |
| 561 |
url = "http://fw13:8080" |
| 562 |
token_env = "OPS_VIEWER_TEST_TOKEN" |
| 563 |
"#, |
| 564 |
); |
| 565 |
let cfg = Config::load(&path).unwrap(); |
| 566 |
|
| 567 |
unsafe { std::env::remove_var("OPS_VIEWER_TEST_TOKEN") }; |
| 568 |
assert_eq!(cfg.sources[0].token(), None); |
| 569 |
unsafe { std::env::set_var("OPS_VIEWER_TEST_TOKEN", "s3cr3t") }; |
| 570 |
assert_eq!(cfg.sources[0].token().as_deref(), Some("s3cr3t")); |
| 571 |
unsafe { std::env::remove_var("OPS_VIEWER_TEST_TOKEN") }; |
| 572 |
} |
| 573 |
} |
| 574 |
|