env-share: initial implementation
This commit is contained in:
parent
2445530f9b
commit
7c7d720f8e
6 changed files with 107 additions and 0 deletions
2
env-share/.gitignore
vendored
Normal file
2
env-share/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
Cargo.lock
|
8
env-share/Cargo.toml
Normal file
8
env-share/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "env-share"
|
||||||
|
version = "0.0.1"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
pico-args = "0.5.0"
|
||||||
|
|
18
env-share/scripts/env-share.ps1
Normal file
18
env-share/scripts/env-share.ps1
Normal file
|
@ -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
|
||||||
|
|
18
env-share/src/error.rs
Normal file
18
env-share/src/error.rs
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
|
31
env-share/src/main.rs
Normal file
31
env-share/src/main.rs
Normal file
|
@ -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(); }
|
||||||
|
}
|
||||||
|
|
30
man/env-share.1
Normal file
30
man/env-share.1
Normal file
|
@ -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
|
Loading…
Reference in a new issue