From 83fb27dfbc60075711eca1a3aace40915822b78b Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 7 Jan 2023 14:53:18 +0100 Subject: chore(users/Profpatsch): move rust exec-helpers to own subdir This is so we can use the rust language server for the file. Change-Id: I8a2fe15ea67fd0e26814fda57bf0cace0d264cae Reviewed-on: https://cl.tvl.fyi/c/depot/+/7792 Autosubmit: Profpatsch Reviewed-by: Profpatsch Tested-by: BuildkiteCI --- users/Profpatsch/execline/default.nix | 7 - users/Profpatsch/execline/exec-helpers/Cargo.lock | 7 + users/Profpatsch/execline/exec-helpers/Cargo.toml | 8 ++ users/Profpatsch/execline/exec-helpers/default.nix | 6 + .../execline/exec-helpers/exec_helpers.rs | 149 +++++++++++++++++++++ users/Profpatsch/execline/exec_helpers.rs | 149 --------------------- 6 files changed, 170 insertions(+), 156 deletions(-) create mode 100644 users/Profpatsch/execline/exec-helpers/Cargo.lock create mode 100644 users/Profpatsch/execline/exec-helpers/Cargo.toml create mode 100644 users/Profpatsch/execline/exec-helpers/default.nix create mode 100644 users/Profpatsch/execline/exec-helpers/exec_helpers.rs delete mode 100644 users/Profpatsch/execline/exec_helpers.rs diff --git a/users/Profpatsch/execline/default.nix b/users/Profpatsch/execline/default.nix index 9b8856a9a84a..47c7f8b74971 100644 --- a/users/Profpatsch/execline/default.nix +++ b/users/Profpatsch/execline/default.nix @@ -1,12 +1,6 @@ { depot, pkgs, lib, ... }: let - exec-helpers = depot.nix.writers.rustSimpleLib - { - name = "exec-helpers"; - } - (builtins.readFile ./exec_helpers.rs); - exec-helpers-hs = pkgs.haskellPackages.mkDerivation { pname = "exec-helpers"; version = "0.1.0"; @@ -48,7 +42,6 @@ let in depot.nix.readTree.drvTargets { inherit - exec-helpers exec-helpers-hs print-one-env ; diff --git a/users/Profpatsch/execline/exec-helpers/Cargo.lock b/users/Profpatsch/execline/exec-helpers/Cargo.lock new file mode 100644 index 000000000000..1753cc949d3a --- /dev/null +++ b/users/Profpatsch/execline/exec-helpers/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "exec_helpers" +version = "0.1.0" diff --git a/users/Profpatsch/execline/exec-helpers/Cargo.toml b/users/Profpatsch/execline/exec-helpers/Cargo.toml new file mode 100644 index 000000000000..6642b66ee375 --- /dev/null +++ b/users/Profpatsch/execline/exec-helpers/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "exec_helpers" +version = "0.1.0" +edition = "2021" + +[lib] +name = "exec_helpers" +path = "exec_helpers.rs" diff --git a/users/Profpatsch/execline/exec-helpers/default.nix b/users/Profpatsch/execline/exec-helpers/default.nix new file mode 100644 index 000000000000..5545d41d9de7 --- /dev/null +++ b/users/Profpatsch/execline/exec-helpers/default.nix @@ -0,0 +1,6 @@ +{ depot, ... }: +depot.nix.writers.rustSimpleLib +{ + name = "exec-helpers"; +} + (builtins.readFile ./exec_helpers.rs) diff --git a/users/Profpatsch/execline/exec-helpers/exec_helpers.rs b/users/Profpatsch/execline/exec-helpers/exec_helpers.rs new file mode 100644 index 000000000000..a57cbca35391 --- /dev/null +++ b/users/Profpatsch/execline/exec-helpers/exec_helpers.rs @@ -0,0 +1,149 @@ +use std::ffi::OsStr; +use std::os::unix::ffi::{OsStrExt, OsStringExt}; +use std::os::unix::process::CommandExt; + +pub fn no_args(current_prog_name: &str) -> () { + let mut args = std::env::args_os(); + // remove argv[0] + let _ = args.nth(0); + if args.len() > 0 { + die_user_error( + current_prog_name, + format!("Expected no arguments, got {:?}", args.collect::>()), + ) + } +} + +pub fn args(current_prog_name: &str, no_of_positional_args: usize) -> Vec> { + let mut args = std::env::args_os(); + // remove argv[0] + let _ = args.nth(0); + if args.len() != no_of_positional_args { + die_user_error( + current_prog_name, + format!( + "Expected {} arguments, got {}, namely {:?}", + no_of_positional_args, + args.len(), + args.collect::>() + ), + ) + } + args.map(|arg| arg.into_vec()).collect() +} + +pub fn args_for_exec( + current_prog_name: &str, + no_of_positional_args: usize, +) -> (Vec>, Vec>) { + let mut args = std::env::args_os(); + // remove argv[0] + let _ = args.nth(0); + let mut args = args.map(|arg| arg.into_vec()); + let mut pos_args = vec![]; + // get positional args + for i in 1..no_of_positional_args + 1 { + pos_args.push(args.nth(0).expect(&format!( + "{}: expects {} positional args, only got {}", + current_prog_name, no_of_positional_args, i + ))); + } + // prog... is the rest of the iterator + let prog: Vec> = args.collect(); + (pos_args, prog) +} + +pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>( + current_prog_name: &str, + args: Args, + env_additions: Env, +) -> ! +where + Args: IntoIterator, + Arg: AsRef<[u8]>, + Env: IntoIterator, + Key: AsRef<[u8]>, + Val: AsRef<[u8]>, +{ + // TODO: is this possible without collecting into a Vec first, just leaving it an IntoIterator? + let args = args.into_iter().collect::>(); + let mut args = args.iter().map(|v| OsStr::from_bytes(v.as_ref())); + let prog = args.nth(0).expect(&format!( + "{}: first argument must be an executable", + current_prog_name + )); + // TODO: same here + let env = env_additions.into_iter().collect::>(); + let env = env + .iter() + .map(|(k, v)| (OsStr::from_bytes(k.as_ref()), OsStr::from_bytes(v.as_ref()))); + let err = std::process::Command::new(prog).args(args).envs(env).exec(); + die_missing_executable( + current_prog_name, + format!( + "exec failed: {}, while trying to execing into {:?}", + err, prog + ), + ); +} + +/// Exit 1 to signify a generic expected error +/// (e.g. something that sometimes just goes wrong, like a nix build). +pub fn die_expected_error(current_prog_name: &str, msg: S) -> ! +where + S: AsRef, +{ + die_with(1, current_prog_name, msg) +} + +/// Exit 100 to signify a user error (“the user is holding it wrong”). +/// This is a permanent error, if the program is executed the same way +/// it should crash with 100 again. +pub fn die_user_error(current_prog_name: &str, msg: S) -> ! +where + S: AsRef, +{ + die_with(100, current_prog_name, msg) +} + +/// Exit 101 to signify an unexpected crash (failing assertion or panic). +/// This is the same exit code that `panic!()` emits. +pub fn die_panic(current_prog_name: &str, msg: S) -> ! +where + S: AsRef, +{ + die_with(101, current_prog_name, msg) +} + +/// Exit 111 to signify a temporary error (such as resource exhaustion) +pub fn die_temporary(current_prog_name: &str, msg: S) -> ! +where + S: AsRef, +{ + die_with(111, current_prog_name, msg) +} + +/// Exit 126 to signify an environment problem +/// (the user has set up stuff incorrectly so the program cannot work) +pub fn die_environment_problem(current_prog_name: &str, msg: S) -> ! +where + S: AsRef, +{ + die_with(126, current_prog_name, msg) +} + +/// Exit 127 to signify a missing executable. +pub fn die_missing_executable(current_prog_name: &str, msg: S) -> ! +where + S: AsRef, +{ + die_with(127, current_prog_name, msg) +} + +fn die_with(status: i32, current_prog_name: &str, msg: S) -> ! +where + S: AsRef, +{ + eprintln!("{}: {}", current_prog_name, msg.as_ref()); + std::process::exit(status) +} diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs deleted file mode 100644 index a57cbca35391..000000000000 --- a/users/Profpatsch/execline/exec_helpers.rs +++ /dev/null @@ -1,149 +0,0 @@ -use std::ffi::OsStr; -use std::os::unix::ffi::{OsStrExt, OsStringExt}; -use std::os::unix::process::CommandExt; - -pub fn no_args(current_prog_name: &str) -> () { - let mut args = std::env::args_os(); - // remove argv[0] - let _ = args.nth(0); - if args.len() > 0 { - die_user_error( - current_prog_name, - format!("Expected no arguments, got {:?}", args.collect::>()), - ) - } -} - -pub fn args(current_prog_name: &str, no_of_positional_args: usize) -> Vec> { - let mut args = std::env::args_os(); - // remove argv[0] - let _ = args.nth(0); - if args.len() != no_of_positional_args { - die_user_error( - current_prog_name, - format!( - "Expected {} arguments, got {}, namely {:?}", - no_of_positional_args, - args.len(), - args.collect::>() - ), - ) - } - args.map(|arg| arg.into_vec()).collect() -} - -pub fn args_for_exec( - current_prog_name: &str, - no_of_positional_args: usize, -) -> (Vec>, Vec>) { - let mut args = std::env::args_os(); - // remove argv[0] - let _ = args.nth(0); - let mut args = args.map(|arg| arg.into_vec()); - let mut pos_args = vec![]; - // get positional args - for i in 1..no_of_positional_args + 1 { - pos_args.push(args.nth(0).expect(&format!( - "{}: expects {} positional args, only got {}", - current_prog_name, no_of_positional_args, i - ))); - } - // prog... is the rest of the iterator - let prog: Vec> = args.collect(); - (pos_args, prog) -} - -pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>( - current_prog_name: &str, - args: Args, - env_additions: Env, -) -> ! -where - Args: IntoIterator, - Arg: AsRef<[u8]>, - Env: IntoIterator, - Key: AsRef<[u8]>, - Val: AsRef<[u8]>, -{ - // TODO: is this possible without collecting into a Vec first, just leaving it an IntoIterator? - let args = args.into_iter().collect::>(); - let mut args = args.iter().map(|v| OsStr::from_bytes(v.as_ref())); - let prog = args.nth(0).expect(&format!( - "{}: first argument must be an executable", - current_prog_name - )); - // TODO: same here - let env = env_additions.into_iter().collect::>(); - let env = env - .iter() - .map(|(k, v)| (OsStr::from_bytes(k.as_ref()), OsStr::from_bytes(v.as_ref()))); - let err = std::process::Command::new(prog).args(args).envs(env).exec(); - die_missing_executable( - current_prog_name, - format!( - "exec failed: {}, while trying to execing into {:?}", - err, prog - ), - ); -} - -/// Exit 1 to signify a generic expected error -/// (e.g. something that sometimes just goes wrong, like a nix build). -pub fn die_expected_error(current_prog_name: &str, msg: S) -> ! -where - S: AsRef, -{ - die_with(1, current_prog_name, msg) -} - -/// Exit 100 to signify a user error (“the user is holding it wrong”). -/// This is a permanent error, if the program is executed the same way -/// it should crash with 100 again. -pub fn die_user_error(current_prog_name: &str, msg: S) -> ! -where - S: AsRef, -{ - die_with(100, current_prog_name, msg) -} - -/// Exit 101 to signify an unexpected crash (failing assertion or panic). -/// This is the same exit code that `panic!()` emits. -pub fn die_panic(current_prog_name: &str, msg: S) -> ! -where - S: AsRef, -{ - die_with(101, current_prog_name, msg) -} - -/// Exit 111 to signify a temporary error (such as resource exhaustion) -pub fn die_temporary(current_prog_name: &str, msg: S) -> ! -where - S: AsRef, -{ - die_with(111, current_prog_name, msg) -} - -/// Exit 126 to signify an environment problem -/// (the user has set up stuff incorrectly so the program cannot work) -pub fn die_environment_problem(current_prog_name: &str, msg: S) -> ! -where - S: AsRef, -{ - die_with(126, current_prog_name, msg) -} - -/// Exit 127 to signify a missing executable. -pub fn die_missing_executable(current_prog_name: &str, msg: S) -> ! -where - S: AsRef, -{ - die_with(127, current_prog_name, msg) -} - -fn die_with(status: i32, current_prog_name: &str, msg: S) -> ! -where - S: AsRef, -{ - eprintln!("{}: {}", current_prog_name, msg.as_ref()); - std::process::exit(status) -} -- cgit 1.4.1