dos-var: initial implementation

This commit is contained in:
Valerie Wolfe 2024-07-12 14:05:01 -04:00
parent 5bdeabbe76
commit 2b416b6b2f
4 changed files with 58 additions and 0 deletions

2
dos-var/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

6
dos-var/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "dos-var"
version = "0.0.1"
edition = "2021"
[dependencies]

13
dos-var/src/error.rs Normal file
View file

@ -0,0 +1,13 @@
use std::process::exit;
pub fn not_found(target: &String) {
eprintln!("dos-var: %{target}% has no value");
exit(1);
}
pub fn arg_count() {
eprintln!("dos-var: expects one argument");
exit(2);
}

37
dos-var/src/main.rs Normal file
View file

@ -0,0 +1,37 @@
use std::{
env::args,
process::{
Command,
Stdio
}
};
mod error;
const CMD: &str = "/mnt/c/Windows/System32/cmd.exe";
pub fn main() {
let args: Vec<String> = args().collect();
if args.len() != 2 { error::arg_count(); }
let target = &args[1];
let cmd = Command::new(CMD)
.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
let path = stdout
.replace("\\", "/")
.replace("C:/", "/mnt/c/");
println!("{path}");
}
}
}