feat: add publish date and update time

This commit is contained in:
2025-01-19 15:44:06 +08:00
parent d3604ea8eb
commit 4b2a88d1cc
2 changed files with 63 additions and 10 deletions

View File

@@ -3,53 +3,69 @@
"script_name": "access-guard.ts", "script_name": "access-guard.ts",
"script_length": 5756, "script_length": 5756,
"script_sha256": "ecd11481f6182a5db14008c981a2ebba42a4851d656043c79498e83c95521c7b", "script_sha256": "ecd11481f6182a5db14008c981a2ebba42a4851d656043c79498e83c95521c7b",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/access-guard-ts/main.ts" "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/access-guard-ts/main.ts",
"publish_time": 1737272626138,
"update_time": 1737272626138
}, },
"helloworld.ts": { "helloworld.ts": {
"script_name": "helloworld.ts", "script_name": "helloworld.ts",
"script_length": 69, "script_length": 69,
"script_sha256": "b301944e64def6c3a1ada4e327a7b2c2e37c576a65da5f797998355c51686f76", "script_sha256": "b301944e64def6c3a1ada4e327a7b2c2e37c576a65da5f797998355c51686f76",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/helloworld-ts/main.ts" "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/helloworld-ts/main.ts",
"publish_time": 1737272626138,
"update_time": 1737272626138
}, },
"kpxcenv.ts": { "kpxcenv.ts": {
"script_name": "kpxcenv.ts", "script_name": "kpxcenv.ts",
"script_length": 3622, "script_length": 3622,
"script_sha256": "e2e35aa9a535837a7f663c0b35857d78c178b96451d24be3137dcc0e783cce9c", "script_sha256": "e2e35aa9a535837a7f663c0b35857d78c178b96451d24be3137dcc0e783cce9c",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/kpxcenv.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/kpxcenv.ts",
"single_script_file": true "single_script_file": true,
"publish_time": 1737272626141,
"update_time": 1737272626141
}, },
"print-env.ts": { "print-env.ts": {
"script_name": "print-env.ts", "script_name": "print-env.ts",
"script_length": 156, "script_length": 156,
"script_sha256": "b77d1b8c5ee91ef83e7b1bb72851f6a8f6ed4b0c4ab811fe62f24eebab72149a", "script_sha256": "b77d1b8c5ee91ef83e7b1bb72851f6a8f6ed4b0c4ab811fe62f24eebab72149a",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/print-env.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/print-env.ts",
"single_script_file": true "single_script_file": true,
"publish_time": 1737272626140,
"update_time": 1737272626140
}, },
"publickey.ts": { "publickey.ts": {
"script_name": "publickey.ts", "script_name": "publickey.ts",
"script_length": 1572, "script_length": 1572,
"script_sha256": "83947c19952ef8f69f50354b809bc584e4ff702a663364c9572db8a0bb32cc8e", "script_sha256": "83947c19952ef8f69f50354b809bc584e4ff702a663364c9572db8a0bb32cc8e",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/publickey.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/publickey.ts",
"single_script_file": true "single_script_file": true,
"publish_time": 1737272626140,
"update_time": 1737272626140
}, },
"sigstore-verify.ts": { "sigstore-verify.ts": {
"script_name": "sigstore-verify.ts", "script_name": "sigstore-verify.ts",
"script_length": 3515, "script_length": 3515,
"script_sha256": "0371be7620d5aa2607a5ed1a9b45e2664b5c27ee163966a681e630d568c4a445", "script_sha256": "0371be7620d5aa2607a5ed1a9b45e2664b5c27ee163966a681e630d568c4a445",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/sigstore-verify-ts/main.ts" "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/sigstore-verify-ts/main.ts",
"publish_time": 1737272626138,
"update_time": 1737272626138
}, },
"ssh-verify.ts": { "ssh-verify.ts": {
"script_name": "ssh-verify.ts", "script_name": "ssh-verify.ts",
"script_length": 2708, "script_length": 2708,
"script_sha256": "c8ffa0e730db27046f4fdc2c7946e53eb3f4209f663374b60b64521957218a43", "script_sha256": "c8ffa0e730db27046f4fdc2c7946e53eb3f4209f663374b60b64521957218a43",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/ssh-verify.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/ssh-verify.ts",
"single_script_file": true "single_script_file": true,
"publish_time": 1737272626141,
"update_time": 1737272626141
}, },
"wget.ts": { "wget.ts": {
"script_name": "wget.ts", "script_name": "wget.ts",
"script_length": 3848, "script_length": 3848,
"script_sha256": "02108963e530b4ae422172f93e373858e89206cfeb6b6df4ea74105600ee3a45", "script_sha256": "02108963e530b4ae422172f93e373858e89206cfeb6b6df4ea74105600ee3a45",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/wget.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/wget.ts",
"single_script_file": true "single_script_file": true,
"publish_time": 1737272626139,
"update_time": 1737272626139
} }
} }

View File

@@ -9,7 +9,8 @@
//! ``` //! ```
use rust_util::{ use rust_util::{
debugging, failure_and_exit, iff, opt_result, opt_value_result, success, warning, XResult, debugging, failure_and_exit, iff, opt_result, opt_value_result, success, util_time, warning,
XResult,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::BTreeMap; use std::collections::BTreeMap;
@@ -41,7 +42,7 @@ impl ScriptConfig {
} }
} }
#[derive(Serialize)] #[derive(Serialize, Deserialize)]
struct ScriptMeta { struct ScriptMeta {
script_name: String, script_name: String,
script_length: u64, script_length: u64,
@@ -49,6 +50,10 @@ struct ScriptMeta {
script_full_url: String, script_full_url: String,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
single_script_file: Option<bool>, single_script_file: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
publish_time: Option<u128>,
#[serde(skip_serializing_if = "Option::is_none")]
update_time: Option<u128>,
} }
fn main() -> XResult<()> { fn main() -> XResult<()> {
@@ -71,6 +76,7 @@ fn main() -> XResult<()> {
); );
debugging!("Script config: {:#?}", script_config); debugging!("Script config: {:#?}", script_config);
let former_script_meta_map = read_script_meta_map_from_file();
let mut script_meta_map = BTreeMap::new(); let mut script_meta_map = BTreeMap::new();
let current_read_dir = opt_result!(fs::read_dir("."), "Read dir '.' failed: {}"); let current_read_dir = opt_result!(fs::read_dir("."), "Read dir '.' failed: {}");
@@ -105,6 +111,7 @@ fn main() -> XResult<()> {
&main_script, &main_script,
false, false,
&script_config, &script_config,
&former_script_meta_map,
)?, )?,
); );
} }
@@ -142,6 +149,7 @@ fn main() -> XResult<()> {
&abs_file_entry, &abs_file_entry,
true, true,
&script_config, &script_config,
&former_script_meta_map,
)?, )?,
); );
} }
@@ -157,6 +165,14 @@ fn main() -> XResult<()> {
Ok(()) Ok(())
} }
fn read_script_meta_map_from_file() -> BTreeMap<String, ScriptMeta> {
let script_meta_content = match fs::read_to_string(SCRIPT_META_FILE) {
Ok(script_meta_content) => script_meta_content,
Err(_) => return BTreeMap::new(),
};
serde_json::from_str(&script_meta_content).unwrap_or_default()
}
// translate filename-ext to filename.ext // translate filename-ext to filename.ext
fn translate_script_dir_to_script_name( fn translate_script_dir_to_script_name(
script_dir: &str, script_dir: &str,
@@ -180,6 +196,7 @@ fn read_script_meta(
script_path: &PathBuf, script_path: &PathBuf,
is_simple_script: bool, is_simple_script: bool,
script_config: &ScriptConfig, script_config: &ScriptConfig,
former_script_meta_map: &BTreeMap<String, ScriptMeta>,
) -> XResult<ScriptMeta> { ) -> XResult<ScriptMeta> {
let script_meta = opt_result!( let script_meta = opt_result!(
script_path.metadata(), script_path.metadata(),
@@ -198,11 +215,31 @@ fn read_script_meta(
.replace("$NAME", script_dir) .replace("$NAME", script_dir)
}; };
let single_script_file = iff!(is_simple_script, Some(true), None); let single_script_file = iff!(is_simple_script, Some(true), None);
let former_script_meta = former_script_meta_map.get(&script_name);
let publish_time = Some(
former_script_meta
.map(|m| m.publish_time)
.flatten()
.unwrap_or_else(|| util_time::get_current_millis()),
);
let is_file_same = former_script_meta
.map(|m| m.script_sha256 == script_sha256)
.unwrap_or(false);
let update_time = Some(if is_file_same {
former_script_meta
.map(|m| m.update_time)
.flatten()
.unwrap_or_else(|| util_time::get_current_millis())
} else {
util_time::get_current_millis()
});
Ok(ScriptMeta { Ok(ScriptMeta {
script_name: script_name.clone(), script_name: script_name.clone(),
script_length: script_meta.len(), script_length: script_meta.len(),
script_sha256, script_sha256,
script_full_url, script_full_url,
single_script_file, single_script_file,
publish_time,
update_time,
}) })
} }