From b9cb18ea0b5446b51495ac183e72aa0237c55578 Mon Sep 17 00:00:00 2001 From: Valerie Wolfe Date: Tue, 11 Jun 2024 09:32:57 -0400 Subject: [PATCH] fixed a crash and made paths canonicalize --- mkwin/Cargo.toml | 2 +- mkwin/src/error.rs | 5 +++++ mkwin/src/flag.rs | 2 +- mkwin/src/main.rs | 32 ++++++++++++++++++++------------ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/mkwin/Cargo.toml b/mkwin/Cargo.toml index 7124133..7743070 100644 --- a/mkwin/Cargo.toml +++ b/mkwin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mkwin" -version = "0.0.1" +version = "0.0.2" edition = "2021" [dependencies] diff --git a/mkwin/src/error.rs b/mkwin/src/error.rs index 36aca2a..796547a 100644 --- a/mkwin/src/error.rs +++ b/mkwin/src/error.rs @@ -8,4 +8,9 @@ pub fn missing_target() { exit(1); } +/// failed to canonicalize target path; code 2 +pub fn canonicalize_fail(target: String) { + println!("mkwin: failed to canonicalze '{target}'--does the file exist?"); + exit(2); +} diff --git a/mkwin/src/flag.rs b/mkwin/src/flag.rs index 9165d3b..39ebb0b 100644 --- a/mkwin/src/flag.rs +++ b/mkwin/src/flag.rs @@ -1,6 +1,6 @@ //! constants for flag arguments pub const HELP: [&str;2] = ["-h", "--help"]; -pub const PATH_CONVERT: [&str;2] = [ "--pc", "--path-convert" ]; +pub const PATH_CONVERT: &str = "--pc"; pub const QUIET: [&str;2] = ["-q", "--quiet"]; diff --git a/mkwin/src/main.rs b/mkwin/src/main.rs index 79e9661..e9d5b72 100644 --- a/mkwin/src/main.rs +++ b/mkwin/src/main.rs @@ -1,3 +1,4 @@ +use std::path::Path; use pico_args::Arguments; @@ -41,12 +42,19 @@ pub fn main() { let quiet = args.contains(flag::QUIET); // get target executable - let i_target = args.subcommand().unwrap(); - if i_target.is_none() { + let target: String; + if let Ok(Some(arg)) = args.subcommand() { + let path = Path::new(&arg); + if let Ok(path) = path.canonicalize() { + target = path.to_string_lossy().into(); + } else { + error::canonicalize_fail(arg); + return; + } + } else { error::missing_target(); + return; } - let target = i_target.unwrap(); - // construct output print!("#!/usr/bin/bash\n# generated with: mkwin{arg_string}\n\n'{target}' "); @@ -70,16 +78,16 @@ Quickly make bash scripts to run windows programs in WSL. usage: mkwin [flags] args: - The target program the resulting script will run. + The target program the resulting script will run. flags: - -h, --help Shows this help text. - --pc=, The resulting script will use the 'path-convert' - --path-convert= to convert arguments from UNIX to DOS, with the - provided set of flags ('x' for no flags). + -h, --help Shows this help text and exit. - -q, --quiet The resulting script will run the target program - in the background and with its outputs redirected - to '/dev/null'.", + --pc= The resulting script will use the 'path-convert' to convert + arguments from UNIX to DOS, with the provided set of flags + ('x' for no flags). + + -q, --quiet The resulting script will run the target program in the + background and with its outputs redirected to '/dev/null'.", env!("CARGO_PKG_VERSION"));}