diff options
author | Profpatsch <mail@profpatsch.de> | 2021-02-13T22·13+0100 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2021-04-23T20·45+0000 |
commit | 9d7c3ee877837070c51760cb621bc409c6d0f6a7 (patch) | |
tree | 3d875423c597b950cc8ada8fa80077d8070dcfdb | |
parent | 5156da542bac99aa2e625bc4c1033d2017aa5c54 (diff) |
feat(users/Profpatsch/netencode): add env-splice-record r/2543
It’s the inverse of record-splice-env! It sucks up the environment and prints it as a netencode dict! Only the utf-8 clean parts at least. Change-Id: I96c19fc5ea3a67a23e238f15f4d0fa783081859c Reviewed-on: https://cl.tvl.fyi/c/depot/+/2527 Tested-by: BuildkiteCI Reviewed-by: Profpatsch <mail@profpatsch.de>
-rw-r--r-- | users/Profpatsch/netencode/default.nix | 39 | ||||
-rw-r--r-- | users/Profpatsch/netencode/netencode.rs | 6 |
2 files changed, 37 insertions, 8 deletions
diff --git a/users/Profpatsch/netencode/default.nix b/users/Profpatsch/netencode/default.nix index b913c3e39d79..3088ee66bfe4 100644 --- a/users/Profpatsch/netencode/default.nix +++ b/users/Profpatsch/netencode/default.nix @@ -86,12 +86,39 @@ let } ''; + env-splice-record = imports.writers.rustSimple { + name = "env-splice-record"; + dependencies = [ + netencode-rs + depot.users.Profpatsch.execline.exec-helpers + ]; + } '' + extern crate netencode; + extern crate exec_helpers; + use netencode::{T}; + use std::os::unix::ffi::OsStringExt; + + fn main() { + exec_helpers::no_args("env-splice-record"); + let mut res = std::collections::HashMap::new(); + for (key, val) in std::env::vars_os() { + match (String::from_utf8(key.into_vec()), String::from_utf8(val.into_vec())) { + (Ok(k), Ok(v)) => { let _ = res.insert(k, T::Text(v)); }, + // same as in record-splice-env, we ignore non-utf8 variables + (_, _) => {}, + } + } + netencode::encode(&mut std::io::stdout(), &T::Record(res).to_u()).unwrap() + } + ''; + in depot.nix.utils.drvTargets { inherit - netencode-rs - netencode-mustache - record-get - record-splice-env - gen - ; + netencode-rs + netencode-mustache + record-get + record-splice-env + env-splice-record + gen + ; } diff --git a/users/Profpatsch/netencode/netencode.rs b/users/Profpatsch/netencode/netencode.rs index 249cc33ed1c0..ab10af307c6f 100644 --- a/users/Profpatsch/netencode/netencode.rs +++ b/users/Profpatsch/netencode/netencode.rs @@ -26,6 +26,7 @@ pub enum T { Binary(Vec<u8>), // Tags // TODO: make into &str + // TODO: rename to Tag Sum(Tag<String, T>), // TODO: make into &str Record(HashMap<String, T>), @@ -81,9 +82,10 @@ pub enum U<'a> { // Text Text(&'a str), Binary(&'a [u8]), - // Tags // TODO: the U-recursion we do here means we can’t be breadth-lazy anymore // like we originally planned; maybe we want to go `U<'a>` → `&'a [u8]` again? + // Tags + // TODO: rename to Tag Sum(Tag<&'a str, U<'a>>), Record(HashMap<&'a str, U<'a>>), List(Vec<U<'a>>), @@ -195,7 +197,7 @@ pub fn u_from_stdin_or_die_user_error<'a>(prog_name: &'_ str, stdin_buf: &'a mut let u = match parse::u_u(stdin_buf) { Ok((rest, u)) => match rest { b"" => u, - _ => exec_helpers::die_user_error(prog_name, format!("stdin contained some soup after netencode value: {:?}", rest)) + _ => exec_helpers::die_user_error(prog_name, format!("stdin contained some soup after netencode value: {:?}", String::from_utf8_lossy(rest))) }, Err(err) => exec_helpers::die_user_error(prog_name, format!("unable to parse netencode from stdin: {:?}", err)) }; |