Skip to content

Commit

Permalink
Use OsStr instead of OsString
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed May 17, 2020
1 parent 0d5c4de commit 5a41126
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cargo-crev/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub fn crate_open(crate_sel: &CrateSelector, cmd: Option<String>, cmd_save: bool
&crate_.version(),
&crev_lib::ReviewActivity::new_full(),
)?;
let status = crev_lib::util::run_with_shell_cmd(open_cmd.into(), Some(crate_root))?;
let status = crev_lib::util::run_with_shell_cmd(open_cmd.as_ref(), Some(crate_root))?;

if !status.success() {
bail!("Shell returned {}", status);
Expand Down
20 changes: 7 additions & 13 deletions crev-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ pub mod convert;
pub mod fs;
pub mod rand;
pub mod serde;
use std::ffi::OsStr;
pub use crate::blake2b256::Blake2b256;
use blake2::{digest::FixedOutput, Digest};
use std::{
collections::HashSet,
env,
ffi::OsString,
io::{self, BufRead, Read, Write},
path::{Path, PathBuf},
process,
Expand Down Expand Up @@ -209,13 +209,13 @@ pub fn yes_or_no_was_y(msg: &str) -> io::Result<bool> {
}

pub fn run_with_shell_cmd(
cmd: OsString,
cmd: &OsStr,
arg: Option<&Path>,
) -> io::Result<std::process::ExitStatus> {
Ok(run_with_shell_cmd_custom(cmd, arg, false)?.status)
}

pub fn run_with_shell_cmd_capture_stdout(cmd: OsString, arg: Option<&Path>) -> io::Result<Vec<u8>> {
pub fn run_with_shell_cmd_capture_stdout(cmd: &OsStr, arg: Option<&Path>) -> io::Result<Vec<u8>> {
let output = run_with_shell_cmd_custom(cmd, arg, true)?;
if !output.status.success() {
return Err(std::io::Error::new(
Expand All @@ -227,7 +227,7 @@ pub fn run_with_shell_cmd_capture_stdout(cmd: OsString, arg: Option<&Path>) -> i
}

pub fn run_with_shell_cmd_custom(
cmd: OsString,
cmd: &OsStr,
arg: Option<&Path>,
capture_stdout: bool,
) -> io::Result<std::process::Output> {
Expand All @@ -250,20 +250,14 @@ pub fn run_with_shell_cmd_custom(
if let Some(arg) = arg {
proc.arg("-c").arg(format!(
"{} {}",
cmd.clone().into_string().map_err(|_| std::io::Error::new(
cmd.to_str().ok_or_else(|| std::io::Error::new(
io::ErrorKind::InvalidData,
"not a valid unicode"
))?,
shell_escape::escape(arg.display().to_string().into())
));
} else {
proc.arg("-c").arg(format!(
"{}",
cmd.clone().into_string().map_err(|_| std::io::Error::new(
io::ErrorKind::InvalidData,
"not a valid unicode"
))?,
));
proc.arg("-c").arg(cmd);
}
proc
} else {
Expand All @@ -285,7 +279,7 @@ pub fn read_passphrase() -> io::Result<String> {
return Ok(pass);
} else if let Some(cmd) = env::var_os("CREV_PASSPHRASE_CMD") {
return Ok(
String::from_utf8_lossy(&run_with_shell_cmd_capture_stdout(cmd, None)?)
String::from_utf8_lossy(&run_with_shell_cmd_capture_stdout(&cmd, None)?)
.trim()
.to_owned(),
);
Expand Down
2 changes: 1 addition & 1 deletion crev-lib/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn edit_text_iteractively_until_writen_to(text: &str) -> Result<String> {
pub fn edit_file(path: &Path) -> Result<()> {
let editor = get_editor_to_use()?;

let status = run_with_shell_cmd(editor, Some(path))?;
let status = run_with_shell_cmd(&editor, Some(path))?;

if !status.success() {
Error::EditorLaunch(status.code().unwrap_or(-1));
Expand Down

0 comments on commit 5a41126

Please sign in to comment.