commit 171124050cc5c5996516091b26c9e1bc9e7426e7 Author: D. Scott Boggs Date: Sat May 11 16:40:44 2024 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7137d53 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "env_or_file" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8d2cca0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "env_or_file" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4f5f60b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,47 @@ +#![feature(iter_intersperse)] +use std::{ + env, + ffi::OsString, + fs, + os::unix::{ + ffi::{OsStrExt, OsStringExt}, + process::CommandExt, + }, + process::Command, +}; + +fn main() { + let mut child_env = vec![]; + for (key, value) in env::vars_os() { + let key_bytes: Vec<_> = key.as_bytes().into(); + if key_bytes.ends_with(b"_FILE") && !value.is_empty() { + match fs::read(&value) { + Ok(content) => { + let key = OsString::from_vec(key_bytes[0..key_bytes.len() - 5].to_owned()); + child_env.push((key, OsString::from_vec(content))); + } + Err(error) => { + panic!( + "failed to read contents of {value:?} (specified by ${}): {error:?}", + key.to_string_lossy() + ) + } + } + } else { + child_env.push((key, value)) + } + } + let mut args = env::args().skip(1); + if let Some(cmd) = args.next() { + let collected: Vec<_> = args.collect(); + let mut child = Command::new(&cmd); + child.env_clear().envs(child_env); + for arg in &collected { + child.arg(arg); + } + let err = child.exec(); // Ideally should not return + panic!("error running command {cmd} with args {collected:?}: {err}"); + } else { + panic!("no command given"); + } +}