dos-var: usability improvement and added release profile

This commit is contained in:
Valerie Wolfe 2024-07-12 14:16:07 -04:00
parent 2b416b6b2f
commit 30159a43ae
3 changed files with 42 additions and 26 deletions

View file

@ -4,6 +4,10 @@
These are tools I use to use Windows primarily through WSL. A lot of them These are tools I use to use Windows primarily through WSL. A lot of them
assume you have PowerShell somewhere in your `PATH` as "`pwsh`". assume you have PowerShell somewhere in your `PATH` as "`pwsh`".
## `dos-var`
Gets Windows environment variables from DOS.
## `env-share` ## `env-share`
A hacked-together utility for sending WSL variables back to Windows A hacked-together utility for sending WSL variables back to Windows

View file

@ -1,6 +1,15 @@
[package] [package]
name = "dos-var" name = "dos-var"
version = "0.0.1" version = "0.0.2"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
pico-args = "0.5.0"
[profile.release]
opt-level = 's'
codegen-units = 1
debug = false
lto = true
panic = "abort"
strip = "symbols"

View file

@ -1,36 +1,39 @@
use std::{ use std::process::Command;
env::args,
process::{ use pico_args::Arguments;
Command,
Stdio
}
};
mod error; mod error;
const CMD: &str = "/mnt/c/Windows/System32/cmd.exe"; const CMD: &str = "/mnt/c/Windows/System32/cmd.exe";
const FLAG_PATH: [&str; 2] = [ "-p", "--path" ];
pub fn main() { pub fn main() {
let args: Vec<String> = args().collect(); let mut args = Arguments::from_env();
if args.len() != 2 { error::arg_count(); }
let target = &args[1];
let cmd = Command::new(CMD) let convert_path = args.contains(FLAG_PATH);
.current_dir("/mnt/c/")
.arg( format!("/C echo %{target}%") )
.output();
if let Ok(output) = cmd {
if let Ok(stdout) = String::from_utf8(output.stdout) {
// trim output
let stdout = stdout.trim_end_matches("\"\r\n");
// catch empty variable case
if stdout == format!("%{target}%") { error::not_found(&target); }
// convert and write if let Ok(Some(target)) = args.subcommand() {
let path = stdout let cmd = Command::new(CMD)
.replace("\\", "/") .current_dir("/mnt/c/")
.replace("C:/", "/mnt/c/"); .arg( format!("/C echo %{target}%") )
println!("{path}"); .output();
if let Ok(output) = cmd {
if let Ok(stdout) = String::from_utf8(output.stdout) {
// trim output
let stdout = stdout.trim_end_matches("\"\r\n");
// catch empty variable case
if stdout == format!("%{target}%") { error::not_found(&target); }
// handle path flag and write
let path =
if convert_path {
stdout
.replace("\\", "/")
.replace("C:/", "/mnt/c/")
} else { stdout.to_owned() };
println!("{path}");
}
} }
} }
} }