From 2b416b6b2f97154e5300388798c5433a29fef331 Mon Sep 17 00:00:00 2001 From: Valerie Wolfe Date: Fri, 12 Jul 2024 14:05:01 -0400 Subject: [PATCH] dos-var: initial implementation --- dos-var/.gitignore | 2 ++ dos-var/Cargo.toml | 6 ++++++ dos-var/src/error.rs | 13 +++++++++++++ dos-var/src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 dos-var/.gitignore create mode 100644 dos-var/Cargo.toml create mode 100644 dos-var/src/error.rs create mode 100644 dos-var/src/main.rs diff --git a/dos-var/.gitignore b/dos-var/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/dos-var/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/dos-var/Cargo.toml b/dos-var/Cargo.toml new file mode 100644 index 0000000..5307eea --- /dev/null +++ b/dos-var/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "dos-var" +version = "0.0.1" +edition = "2021" + +[dependencies] diff --git a/dos-var/src/error.rs b/dos-var/src/error.rs new file mode 100644 index 0000000..a7fa008 --- /dev/null +++ b/dos-var/src/error.rs @@ -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); +} + diff --git a/dos-var/src/main.rs b/dos-var/src/main.rs new file mode 100644 index 0000000..9f4498d --- /dev/null +++ b/dos-var/src/main.rs @@ -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 = 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}"); + } + } +} +