feat: update keyring.rs

This commit is contained in:
2026-01-02 15:56:30 +08:00
parent 382bd834f6
commit 0db169932b
3 changed files with 48 additions and 18 deletions

View File

@@ -13,7 +13,7 @@
use clap::{Args, Parser, Subcommand};
use keyring::Entry;
use rust_util::{opt_result, success, XResult};
use rust_util::{XResult, opt_result, success};
use std::collections::BTreeMap;
use zeroizing_alloc::ZeroAlloc;
@@ -41,10 +41,10 @@ enum Commands {
#[derive(Debug, Args)]
pub struct CmdSet {
/// Service
/// Service (optional)
#[arg(long, short = 'S')]
pub service: String,
/// User
pub service: Option<String>,
/// User service:user when service is optional
#[arg(long, short = 'U')]
pub user: String,
/// Password
@@ -56,23 +56,52 @@ pub struct CmdSet {
pub struct CmdGet {
/// Service
#[arg(long, short = 'S')]
pub service: String,
/// User
pub service: Option<String>,
/// User service:user when service is optional
#[arg(long, short = 'U')]
pub user: String,
// JSON output
#[arg(long)]
pub json: bool,
// Raw output
#[arg(long)]
#[arg(long, short = 'R')]
pub raw: bool,
}
pub struct KeyringKey {
pub service: String,
pub user: String,
}
impl KeyringKey {
fn from(service: &str, user: &str) -> Self {
Self {
service: service.to_string(),
user: user.to_string(),
}
}
fn parse_key(service: &Option<String>, user: &str) -> KeyringKey {
match service {
Some(service) => KeyringKey::from(service, user),
None => {
let user_parts = user.split(":").collect::<Vec<_>>();
if user_parts.len() != 2 {
KeyringKey::from("keyring-service", user)
} else {
KeyringKey::from(user_parts[0], user_parts[1])
}
}
}
}
}
fn main() -> XResult<()> {
let args = ConnectArgs::parse();
match args.command {
Commands::Set(cmd_set) => {
let entry = Entry::new(&cmd_set.service, &cmd_set.user)?;
let keyring_key = KeyringKey::parse_key(&cmd_set.service, &cmd_set.user);
let entry = Entry::new(&keyring_key.service, &cmd_set.user)?;
let password = match cmd_set.password {
Some(password) => password,
@@ -88,12 +117,13 @@ fn main() -> XResult<()> {
success!("Set password successfully");
}
Commands::Get(cmd_get) => {
let entry = Entry::new(&cmd_get.service, &cmd_get.user)?;
let keyring_key = KeyringKey::parse_key(&cmd_get.service, &cmd_get.user);
let entry = Entry::new(&keyring_key.service, &keyring_key.user)?;
let password = entry.get_password()?;
if cmd_get.json {
let mut result = BTreeMap::new();
result.insert("service", cmd_get.service.to_string());
result.insert("user", cmd_get.user.to_string());
result.insert("service", keyring_key.service.to_string());
result.insert("user", keyring_key.user.to_string());
result.insert("password", password.to_string());
println!("{}", serde_json::to_string_pretty(&result)?);
} else if cmd_get.raw {
@@ -106,5 +136,5 @@ fn main() -> XResult<()> {
Ok(())
}
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20251020T000849+08:00.MEQCIFcMEJOSE3+uJTzhAE1C
// nV8sLPXE3E1uspg7Q7uLYzW/AiAFdhpS/8OLK6Ex2alVVHQOAkTnpP57pKLKWnxf+HNbMw==
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260102T155433+08:00.MEQCIFYYJ4OI5Q+EArEZGTAV
// WGOMtguuDWImfg+qngQopIIHAiAoWXzczFWsvhflIwqnaIX4Le4i8a7N6XQH/mlIrwgMvA==

View File

@@ -66,11 +66,11 @@
},
"keyring.rs": {
"script_name": "keyring.rs",
"script_length": 3074,
"script_sha256": "7f5e195881b0a3b091e6dc86d01f5f4d16a4be22d729ffa23f3766797523e1ed",
"script_length": 4130,
"script_sha256": "8993d6c34face72a3f4288328ba5bb543b334858d60bb1421943ed23179cc416",
"script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/keyring-rs/src/main.rs",
"publish_time": 1760858747229,
"update_time": 1760890167506
"update_time": 1767340579217
},
"makepassword.rs": {
"script_name": "makepassword.rs",

View File

@@ -36,8 +36,8 @@
},
"keyring-rs": {
"script_name": "keyring-rs",
"script_length": 3074,
"script_sha256": "7f5e195881b0a3b091e6dc86d01f5f4d16a4be22d729ffa23f3766797523e1ed"
"script_length": 4130,
"script_sha256": "8993d6c34face72a3f4288328ba5bb543b334858d60bb1421943ed23179cc416"
},
"makepassword-rs": {
"script_name": "makepassword-rs",