diff options
author | sterni <sternenseemann@systemli.org> | 2021-09-09T16·26+0200 |
---|---|---|
committer | sterni <sternenseemann@systemli.org> | 2021-09-10T11·08+0000 |
commit | 318d10e60875ef69132651668a249de75730d2ba (patch) | |
tree | aa3afe8c249edf6ab398657895afbfd271ef91b1 /users/sterni/nint/nint.rs | |
parent | 5d5fb4e6a1b3baee89d89f17ebc11bc19888c59a (diff) |
chore(nint): move from //users/sterni to //nix r/2833
Since //web/bubblegum depends on nint, we need to move it to a non user directory to conform with the policy established via cl/3434. Note that this likely doesn't mean greater stability (which isn't really implied in depot anyways), since I still would like to use a more elaborate calling convention to allow for additional useful features. Change-Id: I616f905d8df13e3363674aab69a797b0d39fdd79 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3506 Tested-by: BuildkiteCI Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'users/sterni/nint/nint.rs')
-rw-r--r-- | users/sterni/nint/nint.rs | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/users/sterni/nint/nint.rs b/users/sterni/nint/nint.rs deleted file mode 100644 index 3d430612851a..000000000000 --- a/users/sterni/nint/nint.rs +++ /dev/null @@ -1,110 +0,0 @@ -extern crate serde_json; - -use serde_json::Value; -use std::ffi::OsString; -use std::os::unix::ffi::{OsStringExt, OsStrExt}; -use std::io::{Error, ErrorKind, Write}; -use std::process::Command; - -fn render_nix_string(s: &OsString) -> OsString { - let mut rendered = Vec::new(); - - rendered.extend(b"\""); - - for b in s.as_os_str().as_bytes() { - match char::from(*b) { - '\"' => rendered.extend(b"\\\""), - '\\' => rendered.extend(b"\\\\"), - '$' => rendered.extend(b"\\$"), - _ => rendered.push(*b), - } - } - - rendered.extend(b"\""); - - OsString::from_vec(rendered) -} - -fn render_nix_list(arr: &[OsString]) -> OsString { - let mut rendered = Vec::new(); - - rendered.extend(b"[ "); - - for el in arr { - rendered.extend(render_nix_string(el).as_os_str().as_bytes()); - rendered.extend(b" "); - } - - rendered.extend(b"]"); - - OsString::from_vec(rendered) -} - -fn main() -> std::io::Result<()> { - let mut nix_args = Vec::new(); - - let mut args = std::env::args_os().into_iter(); - let mut in_args = true; - - let mut argv: Vec<OsString> = Vec::new(); - - // skip argv[0] - args.next(); - - loop { - let arg = match args.next() { - Some(a) => a, - None => break, - }; - - if !arg.to_str().map(|s| s.starts_with("-")).unwrap_or(false) { - in_args = false; - } - - if in_args { - match(arg.to_str()) { - Some("--arg") | Some("--argstr") => { - nix_args.push(arg); - nix_args.push(args.next().unwrap()); - nix_args.push(args.next().unwrap()); - Ok(()) - } - _ => Err(Error::new(ErrorKind::Other, "unknown argument")), - }? - } else { - argv.push(arg); - } - } - - if argv.len() < 1 { - Err(Error::new(ErrorKind::Other, "missing argv")) - } else { - let cd = std::env::current_dir()?.into_os_string(); - - nix_args.push(OsString::from("--arg")); - nix_args.push(OsString::from("currentDir")); - nix_args.push(cd); - - nix_args.push(OsString::from("--arg")); - nix_args.push(OsString::from("argv")); - nix_args.push(render_nix_list(&argv[..])); - - nix_args.push(OsString::from("--eval")); - nix_args.push(OsString::from("--json")); - - nix_args.push(argv[0].clone()); - - let run = Command::new("nix-instantiate") - .args(nix_args) - .output()?; - - match serde_json::from_slice(&run.stdout[..]) { - Ok(Value::String(s)) => Ok(print!("{}", s)), - Ok(_) => Err(Error::new(ErrorKind::Other, "output must be a string")), - _ => { - std::io::stderr().write_all(&run.stderr[..]); - Err(Error::new(ErrorKind::Other, "internal nix error")) - }, - } - } -} |