Ensure passing relative paths to CLI works
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -1,5 +1,5 @@
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
|
||||
use clap::Parser;
|
||||
@@ -67,8 +67,16 @@ fn main() {
|
||||
mode,
|
||||
hardened: args.hardened,
|
||||
no_net: args.no_net,
|
||||
extra_rw: args.extra_rw,
|
||||
extra_ro: args.extra_ro,
|
||||
extra_rw: args
|
||||
.extra_rw
|
||||
.iter()
|
||||
.map(|p| canonicalize_or_exit(p))
|
||||
.collect(),
|
||||
extra_ro: args
|
||||
.extra_ro
|
||||
.iter()
|
||||
.map(|p| canonicalize_or_exit(p))
|
||||
.collect(),
|
||||
command,
|
||||
command_args,
|
||||
chdir,
|
||||
@@ -104,7 +112,7 @@ fn assert_binary_exists(name: &OsStr) -> PathBuf {
|
||||
|
||||
fn assert_chdir(explicit: Option<PathBuf>) -> PathBuf {
|
||||
if let Some(p) = explicit {
|
||||
return p;
|
||||
return canonicalize_or_exit(&p);
|
||||
}
|
||||
match std::env::current_dir() {
|
||||
Ok(p) => p,
|
||||
@@ -118,6 +126,13 @@ fn assert_chdir(explicit: Option<PathBuf>) -> PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
fn canonicalize_or_exit(p: &Path) -> PathBuf {
|
||||
std::fs::canonicalize(p).unwrap_or_else(|e| {
|
||||
eprintln!("error: cannot resolve path '{}': {e}", p.display());
|
||||
process::exit(1);
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_binary(name: &OsStr) -> Option<PathBuf> {
|
||||
let path = PathBuf::from(name);
|
||||
if path.is_absolute() || path.components().count() > 1 {
|
||||
|
||||
Reference in New Issue
Block a user