feat: script-sign-rs script-verify-rs

This commit is contained in:
2025-01-23 00:25:34 +08:00
parent 912332da97
commit c2af7ec23b
12 changed files with 1238 additions and 115 deletions

View File

@@ -2,13 +2,13 @@
//! ```cargo
//! [dependencies]
//! clap = { version = "4.5.23", features = ["derive"] }
//! rust_util = "0.6.47"
//! clap = { version = "4.5", features = ["derive"] }
//! rust_util = "0.6"
//! script-sign = "0.1"
//! ```
use clap::Parser;
use rust_util::{failure, failure_and_exit, information, success, warning};
use rust_util::{failure, information, success, warning};
use script_sign::{KeyMap, Script};
use std::fs;
use std::path::PathBuf;
@@ -17,12 +17,6 @@ use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None, bin_name = "script-sign.rs")]
struct Args {
/// Sign script
#[arg(long)]
sign: bool,
/// Verify script
#[arg(long)]
verify: bool,
/// Force sign script
#[arg(long)]
force: bool,
@@ -32,15 +26,17 @@ struct Args {
fn main() {
let args = Args::parse();
if !args.verify && !args.sign {
failure_and_exit!("Argument --verify or --sign must assigned.");
let key_map = KeyMap::system();
let total_scripts = args.scripts.len();
if total_scripts == 0 {
warning!("No scripts assigned.");
return;
}
let key_map = KeyMap::system().unwrap();
let total_scripts = args.scripts.len();
for (i, script_path) in args.scripts.iter().enumerate() {
information!(
"Processing {}/{}: {}",
"Signing {}/{}: {}",
(i + 1),
total_scripts,
script_path.display()
@@ -67,69 +63,42 @@ fn main() {
}
};
if args.verify {
// VERIFY SCRIPT
if let Some(signature) = &script.signature {
match script.verify(&key_map) {
Ok(true) => {
success!(
"Verify script success: {}, key ID: {}, sign date: {}",
script_path.display(),
signature.key_id,
signature.time
);
}
Ok(false) => {
failure!("Verify script failed: {}", script_path.display());
}
Err(e) => {
warning!("Verify script: {} failed: {}", script_path.display(), e);
}
}
} else {
warning!("Script is not signed: {}", script_path.display());
}
} else if args.sign {
// SIGN SCRIPT
let mut continue_sign = false;
if script.signature.is_some() {
match script.verify(&key_map) {
Ok(true) => {
if args.force {
continue_sign = true;
} else {
warning!("Script is singed, force sign script need --force flag.");
}
}
Ok(false) => {
let mut continue_sign = false;
if script.signature.is_some() {
match script.verify(&key_map) {
Ok(true) => {
if args.force {
continue_sign = true;
}
Err(e) => {
warning!("Verify script: {} failed: {}", script_path.display(), e);
} else {
warning!("Script is singed, force sign script need --force flag.");
}
}
} else {
continue_sign = true;
}
if continue_sign {
information!("Prepare sign script: {}", script_path.display());
match script.sign() {
Ok(_) => match fs::write(script_path, &script.as_string()) {
Ok(_) => {
success!("Sign script success: {}", script_path.display());
}
Err(e) => {
failure!("Sign script {} failed: {}", script_path.display(), e);
}
},
Err(e) => {
failure!("Sign script {} failed: {}", script_path.display(), e);
}
Ok(false) => {
continue_sign = true;
}
Err(e) => {
warning!("Verify script: {} failed: {}", script_path.display(), e);
}
}
} else {
// SHOULD REACH HERE
failure_and_exit!("Argument --verify or --sign flag must assigned.");
continue_sign = true;
}
if continue_sign {
information!("Prepare sign script: {}", script_path.display());
match script.sign() {
Ok(_) => match fs::write(script_path, &script.as_string()) {
Ok(_) => {
success!("Sign script success: {}", script_path.display());
}
Err(e) => {
failure!("Sign script {} failed: {}", script_path.display(), e);
}
},
Err(e) => {
failure!("Sign script {} failed: {}", script_path.display(), e);
}
}
}
}
}