initial commit
This commit is contained in:
commit
171124050c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -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"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -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]
|
47
src/main.rs
Normal file
47
src/main.rs
Normal file
|
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue