From 85d21a71640f3229c4dd325119d3c27ee80f1e8a Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Sun, 16 Nov 2025 11:53:38 +0800 Subject: [PATCH] perf: remove clone, trim --- src/main.rs | 2 +- src/op.rs | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 33c4681..28364aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ fn main() { let username = if cli.username.starts_with("op://") { OnePassword::get_item(&cli.username) } else { - cli.username.to_owned() + cli.username }; let token = OnePassword::get_item(&cli.token); diff --git a/src/op.rs b/src/op.rs index c20350b..1f9e8e5 100644 --- a/src/op.rs +++ b/src/op.rs @@ -4,22 +4,24 @@ pub struct OnePassword {} impl OnePassword { pub fn get_item(reference: &str) -> String { - let output = Command::new(if cfg!(target_os = "windows") { + let exe = if cfg!(target_os = "windows") { "op.exe" } else { "op" - }) - .arg("read") - .arg(reference) - .output() - .expect("failed to execute process"); + }; + let output = Command::new(exe) + .arg("read") + .arg(reference) + .arg("-n") + .output() + .expect("failed to execute process"); if output.status.success() { - let token = String::from_utf8(output.stdout).expect("Invalid UTF-8 sequence"); - token.trim().to_owned() + let token = + String::from_utf8(output.stdout).expect("Invalid UTF-8 output from op command"); + token } else { - // output the error message from stderr - let error_message = String::from_utf8(output.stderr).expect("Invalid UTF-8 sequence"); + let error_message = String::from_utf8_lossy(&output.stderr); eprintln!("{}", error_message); std::process::exit(1); } @@ -30,7 +32,9 @@ mod tests { #[test] fn test_init_1password() { use crate::op::OnePassword; - let token = OnePassword::get_item("op://Private/GitHub/token"); + let token = OnePassword::get_item("op://Private/GitHub/token".into()); assert!(!token.is_empty()); + assert!(token.len() > 10); + assert!(!token.contains('\n')); } } -- 2.51.2