From 06f4b75a183d0a4d2ebdac275c7fb097a6526efa Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Fri, 29 Jan 2021 15:52:31 +0100 Subject: feat(users/Profpatsch/execline): add exec helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most tools end by execing into their argv, so here’s a small rust function which does the boilerplate. Change-Id: I9748955cf53828e02f04d7e8d74fbaf10c1158b5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2453 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/execline/default.nix | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 users/Profpatsch/execline/default.nix diff --git a/users/Profpatsch/execline/default.nix b/users/Profpatsch/execline/default.nix new file mode 100644 index 0000000000..51f4923e96 --- /dev/null +++ b/users/Profpatsch/execline/default.nix @@ -0,0 +1,27 @@ +{ depot, pkgs, lib, ... }: + +let + exec-helpers = depot.users.Profpatsch.writers.rustSimpleLib { + name = "exec-helpers"; + } '' + use std::os::unix::process::CommandExt; + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + pub fn exec_into_args<'a, I>(prog_name: &str, env_additions: I) -> ! + where + I: IntoIterator, + { + let mut argv = std::env::args_os(); + let prog = argv.nth(1).expect(&format!("{}: first argument must be an executable", prog_name)); + let args = argv; + let env = env_additions.into_iter().map(|(k,v)| (OsStr::from_bytes(k), OsStr::from_bytes(v))); + let err = std::process::Command::new(prog).args(args).envs(env).exec(); + panic!("{}: exec failed: {:?}", prog_name, err); + } + ''; + +in { + inherit + exec-helpers + ; +} -- cgit 1.4.1