~runiq/envgen-collector

e94f9d331858f21865067bc9c645f33facacbed5 — Patrice Peterson 7 months ago 90461cc
First working version
4 files changed, 71 insertions(+), 0 deletions(-)

A .gitignore
A Cargo.lock
A Cargo.toml
A src/main.rs
A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
/target

A Cargo.lock => Cargo.lock +16 -0
@@ 0,0 1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "anyhow"
version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1"

[[package]]
name = "envgen-collector"
version = "0.1.0"
dependencies = [
 "anyhow",
]

A Cargo.toml => Cargo.toml +9 -0
@@ 0,0 1,9 @@
[package]
name = "envgen-collector"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"

A src/main.rs => src/main.rs +45 -0
@@ 0,0 1,45 @@
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
};

use anyhow::Result;

static PREFIXES: [&str; 4] = ["/run", "/etc", "/usr/local/lib", "/usr/lib"];
static SUEG: &str = "systemd/user-environment-generators";

fn try_main() -> Result<()> {
    let dev_null = PathBuf::from("/dev/null");
    let mut generators = BTreeMap::new();
    let dirs = PREFIXES.map(|p| Path::new(p).join(SUEG));
    for dir in dirs {
        let entries = match dir.read_dir() {
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => return Err(e.into()),
            Ok(entries) => entries,
        };

        for entry in entries {
            let entry = entry?;
            let file_name = entry.file_name();
            let canonicalized = entry.path().canonicalize()?;
            let entry = if canonicalized == dev_null {
                None
            } else {
                Some(canonicalized)
            };
            generators.entry(file_name).or_insert(entry);
        }
    }
    for v in generators.values().filter(|v| v.is_some()) {
        // SAFETY: We filter out `None`s so this is fine
        println!("{}", v.as_ref().unwrap().display());
    }
    Ok(())
}

fn main() {
    if let Err(e) = try_main() {
        eprintln!("{:?}", e)
    }
}