Skip to main content

os_checker_plugin/
lib.rs

1pub mod logger;
2
3pub mod prelude {
4    pub use camino::{Utf8Path, Utf8PathBuf};
5    pub use cargo_metadata::Metadata;
6    pub use compact_str::CompactString as XString;
7    pub use duct::cmd;
8    pub use eyre::{Context, ContextCompat, Result};
9    pub use indexmap::IndexMap;
10    pub use jiff::Timestamp;
11    pub use serde::{Deserialize, Serialize};
12
13    pub use cargo_metadata;
14    pub use duct;
15    pub use indexmap;
16    pub use serde;
17    pub use serde_json;
18}
19
20#[macro_use]
21extern crate tracing;
22
23use prelude::*;
24use std::fs;
25
26/// Write Serializable data to a file. This function creates all parent
27/// folders if they do not exist.
28pub fn write_json<T: Serialize>(path: &Utf8Path, val: &T) -> Result<()> {
29    let _span = error_span!("write_json", ?path).entered();
30
31    let parent = path.parent().with_context(|| "No parent path found.")?;
32    fs::create_dir_all(parent)?;
33
34    serde_json::to_writer_pretty(fs::File::create(path)?, val)?;
35    Ok(())
36}
37
38/// Get the list of repos, searching in the following order:
39/// * the first CLI argument: a json path to a vec of repo string
40/// * or read from the result of `os-checker config --list-repos`
41pub fn repos() -> Result<Vec<String>> {
42    let arg = std::env::args().nth(1);
43
44    let text = match arg.as_deref() {
45        Some(list_json) => {
46            let path = Utf8Path::new(list_json);
47            let _span = error_span!("repos", ?path).entered();
48            fs::read_to_string(path)?
49        }
50        None => duct::cmd!("os-checker", "config", "--list-repos")
51            .env_remove("RUST_LOG")
52            .read()
53            .with_context(|| "Failed to run `os-checker config --list-repos`")?,
54    };
55
56    info!(text);
57    Ok(serde_json::from_str(&text)?)
58}