From 7c7d720f8e9f3bdc53f5d189787d90f055fa4602 Mon Sep 17 00:00:00 2001 From: Valerie Wolfe Date: Mon, 8 Jul 2024 10:12:46 -0400 Subject: [PATCH] env-share: initial implementation --- env-share/.gitignore | 2 ++ env-share/Cargo.toml | 8 ++++++++ env-share/scripts/env-share.ps1 | 18 ++++++++++++++++++ env-share/src/error.rs | 18 ++++++++++++++++++ env-share/src/main.rs | 31 +++++++++++++++++++++++++++++++ man/env-share.1 | 30 ++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 env-share/.gitignore create mode 100644 env-share/Cargo.toml create mode 100644 env-share/scripts/env-share.ps1 create mode 100644 env-share/src/error.rs create mode 100644 env-share/src/main.rs create mode 100644 man/env-share.1 diff --git a/env-share/.gitignore b/env-share/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/env-share/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/env-share/Cargo.toml b/env-share/Cargo.toml new file mode 100644 index 0000000..91f0407 --- /dev/null +++ b/env-share/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "env-share" +version = "0.0.1" +edition = "2021" + +[dependencies] +pico-args = "0.5.0" + diff --git a/env-share/scripts/env-share.ps1 b/env-share/scripts/env-share.ps1 new file mode 100644 index 0000000..87fdf9c --- /dev/null +++ b/env-share/scripts/env-share.ps1 @@ -0,0 +1,18 @@ + +# make sure the file exists +$file = $env:ENV_SHARE_FILE +if -not (Test-Path $file) { + exit +} + +# insert values into env +foreach($line in Get-Content $file) { + $parts = $line -split ' = ' + $var = $parts[0] + $value = $parts[1] + Set-Item env:$var -Value $value +} + +# delete the file +Remove-Item $file + diff --git a/env-share/src/error.rs b/env-share/src/error.rs new file mode 100644 index 0000000..a073c47 --- /dev/null +++ b/env-share/src/error.rs @@ -0,0 +1,18 @@ +use std::process::exit; + +pub fn file_unset() { + println!("env-share: ENV_SHARE_FILE is unset"); + exit(1); +} + +pub fn vars_unset() { + println!("env-share: ENV_SHARE_VARS is unset"); + exit(1); +} + + +pub fn write_fail(file: &String) { + println!("env-share: failed to write to '{file}'"); + exit(2); +} + diff --git a/env-share/src/main.rs b/env-share/src/main.rs new file mode 100644 index 0000000..a925ee3 --- /dev/null +++ b/env-share/src/main.rs @@ -0,0 +1,31 @@ +use std::{ + env, + fs::write +}; + +mod error; + +fn main() { + // try to get share file variable + if let Ok(file) = env::var("ENV_SHARE_FILE") { + if let Ok(var_string) = env::var("ENV_SHARE_VARS") { + // build output + let mut output = String::new(); + let vars = var_string.split(':'); + for var in vars { + let value = env::var(var).unwrap_or(String::new()); + if value.is_empty() { continue; } + + output += &format!("{var} = {value}\n"); + } + + // write to file + let result = write(&file, output); + match result { + Err(_) => error::write_fail(&file), + _ => { } + } + } else { error::vars_unset(); } + } else { error::file_unset(); } +} + diff --git a/man/env-share.1 b/man/env-share.1 new file mode 100644 index 0000000..607b6d3 --- /dev/null +++ b/man/env-share.1 @@ -0,0 +1,30 @@ +.Dd $Mdocdate$ +.Dt ENV-SHARE 1 +.Os +.Sh NAME +.Nm env-share +.Nd share environment variables back to Windows from WSL +.Sh SYNOPSIS +.Nm env-share +.Sh DESCRIPTION +The +.Nm +utility writes environment variables to a file to be loaded by a script in the target process. +.Sh ENVIRONMENT +.Bl -tag -width Ds +.It Ev ENV_SHARE_FILE +The file to write variables to. +.It Ev ENV_SHARE_VARS +A colon-separated list of variables to copy. +.El +.Sh EXIT STATUS +.Bl -tag -width Ds +.It 1 +An environment variable is unset. +.It 2 +Failed to write to the file at +.Ev ENV_SHARE_FILE +.El +.Sh AUTHORS +.An -nosplit +.An Valerie Wolfe Aq Mt sleeplessval@gmail.com