feat: v0.4.1, enchance config outputs
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -31,9 +31,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aes-gcm-stream"
|
name = "aes-gcm-stream"
|
||||||
version = "0.2.1"
|
version = "0.2.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1702f71a8446da86c4fe4b2fa1a93e5a16880a81f0c1d08312367917dee0e3ee"
|
checksum = "30c72bb73e87b331c6e6c34d542f1b6e26b3a26d8b73a697726443eef99bdffa"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes",
|
"aes",
|
||||||
"cipher",
|
"cipher",
|
||||||
@@ -145,9 +145,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.73"
|
version = "0.1.74"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0"
|
checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -2197,7 +2197,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tiny-encrypt"
|
name = "tiny-encrypt"
|
||||||
version = "0.4.0"
|
version = "0.4.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes-gcm-stream",
|
"aes-gcm-stream",
|
||||||
"base64",
|
"base64",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tiny-encrypt"
|
name = "tiny-encrypt"
|
||||||
version = "0.4.0"
|
version = "0.4.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "A simple and tiny file encrypt tool"
|
description = "A simple and tiny file encrypt tool"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::cmp::Ordering;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
use rust_util::XResult;
|
use rust_util::{iff, information, warning, XResult};
|
||||||
use tabled::{Table, Tabled};
|
use tabled::{Table, Tabled};
|
||||||
use tabled::settings::Style;
|
use tabled::settings::Style;
|
||||||
|
|
||||||
@@ -33,16 +33,68 @@ impl PartialOrd for ConfigProfile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Tabled)]
|
||||||
|
pub struct ConfigEnvelop {
|
||||||
|
pub r#type: String,
|
||||||
|
pub sid: String,
|
||||||
|
pub kid: String,
|
||||||
|
pub desc: String,
|
||||||
|
pub args: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Args)]
|
#[derive(Debug, Args)]
|
||||||
pub struct CmdConfig {
|
pub struct CmdConfig {
|
||||||
/// Show KID
|
/// Show KID
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub show_kid: bool,
|
pub show_kid: bool,
|
||||||
|
/// Encryption profile (use default when --key-filter is assigned)
|
||||||
|
#[arg(long, short = 'p')]
|
||||||
|
pub profile: Option<String>,
|
||||||
|
/// Encryption key filter (key_id or type:TYPE(e.g. ecdh, pgp, ecdh-p384, pgp-ed25519), multiple joined by ',', ALL for all)
|
||||||
|
#[arg(long, short = 'k')]
|
||||||
|
pub key_filter: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn config(cmd_version: CmdConfig) -> XResult<()> {
|
pub fn config(cmd_version: CmdConfig) -> XResult<()> {
|
||||||
let config = TinyEncryptConfig::load(TINY_ENC_CONFIG_FILE)?;
|
let config = TinyEncryptConfig::load(TINY_ENC_CONFIG_FILE)?;
|
||||||
|
|
||||||
|
if cmd_version.profile.is_some() || cmd_version.key_filter.is_some() {
|
||||||
|
return config_key_filter(&cmd_version, &config);
|
||||||
|
}
|
||||||
|
config_profiles(&cmd_version, &config)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn config_key_filter(cmd_version: &CmdConfig, config: &TinyEncryptConfig) -> XResult<()> {
|
||||||
|
let envelops = config.find_envelops(&cmd_version.profile, &cmd_version.key_filter)?;
|
||||||
|
if envelops.is_empty() { warning!("Found no envelops"); }
|
||||||
|
information!("Found {} envelops", envelops.len());
|
||||||
|
let mut config_envelops = vec![];
|
||||||
|
for envelop in envelops {
|
||||||
|
config_envelops.push(ConfigEnvelop {
|
||||||
|
r#type: envelop.r#type.get_name().to_string(),
|
||||||
|
sid: envelop.sid.as_ref().map(ToString::to_string).unwrap_or_else(|| "-".to_string()),
|
||||||
|
kid: process_kid(&envelop.kid),
|
||||||
|
desc: envelop.desc.as_ref().map(ToString::to_string).unwrap_or_else(|| "-".to_string()),
|
||||||
|
args: envelop.args.as_ref().map(|a| format!("[{}]", a.join(", "))).unwrap_or_else(|| "-".to_string()),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let mut table = Table::new(config_envelops);
|
||||||
|
table.with(Style::sharp());
|
||||||
|
println!("{}", table);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_kid(kid: &str) -> String {
|
||||||
|
if kid.len() < 10 {
|
||||||
|
kid.to_string()
|
||||||
|
} else {
|
||||||
|
kid.chars().enumerate()
|
||||||
|
.filter(|(i, _c)| *i <= 50)
|
||||||
|
.map(|(i, c)| iff!(i >= 48, '.', c)).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn config_profiles(cmd_version: &CmdConfig, config: &TinyEncryptConfig) -> XResult<()> {
|
||||||
let mut reverse_map = HashMap::new();
|
let mut reverse_map = HashMap::new();
|
||||||
for (p, v) in &config.profiles {
|
for (p, v) in &config.profiles {
|
||||||
let p = p;
|
let p = p;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::cmp::Ordering;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use rust_util::{debugging, opt_result, simple_error, XResult};
|
use rust_util::{debugging, iff, opt_result, simple_error, XResult};
|
||||||
use rust_util::util_file::resolve_file_path;
|
use rust_util::util_file::resolve_file_path;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -20,6 +20,7 @@ use crate::spec::TinyEncryptEnvelopType;
|
|||||||
/// },
|
/// },
|
||||||
/// {
|
/// {
|
||||||
/// "type": "ecdh",
|
/// "type": "ecdh",
|
||||||
|
/// "sid": "SHORT-ID-1",
|
||||||
/// "kid": "KID-2",
|
/// "kid": "KID-2",
|
||||||
/// "desc": "this is key 002",
|
/// "desc": "this is key 002",
|
||||||
/// "publicPart": "04..."
|
/// "publicPart": "04..."
|
||||||
@@ -83,11 +84,7 @@ impl TinyEncryptConfig {
|
|||||||
|
|
||||||
pub fn find_by_kid(&self, kid: &str) -> Option<&TinyEncryptConfigEnvelop> {
|
pub fn find_by_kid(&self, kid: &str) -> Option<&TinyEncryptConfigEnvelop> {
|
||||||
let config_envelops = self.find_by_kid_or_filter(kid, |_| false);
|
let config_envelops = self.find_by_kid_or_filter(kid, |_| false);
|
||||||
if config_envelops.is_empty() {
|
iff!(config_envelops.is_empty(), None, Some(config_envelops[0]))
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(config_envelops[0])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_by_kid_or_type(&self, k_filter: &str) -> Vec<&TinyEncryptConfigEnvelop> {
|
pub fn find_by_kid_or_type(&self, k_filter: &str) -> Vec<&TinyEncryptConfigEnvelop> {
|
||||||
@@ -99,11 +96,9 @@ impl TinyEncryptConfig {
|
|||||||
pub fn find_by_kid_or_filter<F>(&self, kid: &str, f: F) -> Vec<&TinyEncryptConfigEnvelop>
|
pub fn find_by_kid_or_filter<F>(&self, kid: &str, f: F) -> Vec<&TinyEncryptConfigEnvelop>
|
||||||
where F: Fn(&TinyEncryptConfigEnvelop) -> bool {
|
where F: Fn(&TinyEncryptConfigEnvelop) -> bool {
|
||||||
self.envelops.iter().filter(|e| {
|
self.envelops.iter().filter(|e| {
|
||||||
if e.kid == kid {
|
if e.kid == kid { return true; }
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if let Some(sid) = &e.sid {
|
if let Some(sid) = &e.sid {
|
||||||
return sid == kid;
|
if sid == kid { return true; }
|
||||||
}
|
}
|
||||||
f(e)
|
f(e)
|
||||||
}).collect()
|
}).collect()
|
||||||
|
|||||||
Reference in New Issue
Block a user