diff --git a/script-meta-v2.json b/script-meta-v2.json index be00e41..8e8b7ad 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -3,53 +3,69 @@ "script_name": "access-guard.ts", "script_length": 5756, "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": { "script_name": "helloworld.ts", "script_length": 69, "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": { "script_name": "kpxcenv.ts", "script_length": 3622, "script_sha256": "e2e35aa9a535837a7f663c0b35857d78c178b96451d24be3137dcc0e783cce9c", "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": { "script_name": "print-env.ts", "script_length": 156, "script_sha256": "b77d1b8c5ee91ef83e7b1bb72851f6a8f6ed4b0c4ab811fe62f24eebab72149a", "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": { "script_name": "publickey.ts", "script_length": 1572, "script_sha256": "83947c19952ef8f69f50354b809bc584e4ff702a663364c9572db8a0bb32cc8e", "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": { "script_name": "sigstore-verify.ts", "script_length": 3515, "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": { "script_name": "ssh-verify.ts", "script_length": 2708, "script_sha256": "c8ffa0e730db27046f4fdc2c7946e53eb3f4209f663374b60b64521957218a43", "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": { "script_name": "wget.ts", "script_length": 3848, "script_sha256": "02108963e530b4ae422172f93e373858e89206cfeb6b6df4ea74105600ee3a45", "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 } } \ No newline at end of file diff --git a/update-meta-v2.rs b/update-meta-v2.rs index f14586c..58b61c1 100755 --- a/update-meta-v2.rs +++ b/update-meta-v2.rs @@ -9,7 +9,8 @@ //! ``` 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 std::collections::BTreeMap; @@ -41,7 +42,7 @@ impl ScriptConfig { } } -#[derive(Serialize)] +#[derive(Serialize, Deserialize)] struct ScriptMeta { script_name: String, script_length: u64, @@ -49,6 +50,10 @@ struct ScriptMeta { script_full_url: String, #[serde(skip_serializing_if = "Option::is_none")] single_script_file: Option, + #[serde(skip_serializing_if = "Option::is_none")] + publish_time: Option, + #[serde(skip_serializing_if = "Option::is_none")] + update_time: Option, } fn main() -> XResult<()> { @@ -71,6 +76,7 @@ fn main() -> XResult<()> { ); debugging!("Script config: {:#?}", script_config); + let former_script_meta_map = read_script_meta_map_from_file(); let mut script_meta_map = BTreeMap::new(); let current_read_dir = opt_result!(fs::read_dir("."), "Read dir '.' failed: {}"); @@ -105,6 +111,7 @@ fn main() -> XResult<()> { &main_script, false, &script_config, + &former_script_meta_map, )?, ); } @@ -142,6 +149,7 @@ fn main() -> XResult<()> { &abs_file_entry, true, &script_config, + &former_script_meta_map, )?, ); } @@ -157,6 +165,14 @@ fn main() -> XResult<()> { Ok(()) } +fn read_script_meta_map_from_file() -> BTreeMap { + 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 fn translate_script_dir_to_script_name( script_dir: &str, @@ -180,6 +196,7 @@ fn read_script_meta( script_path: &PathBuf, is_simple_script: bool, script_config: &ScriptConfig, + former_script_meta_map: &BTreeMap, ) -> XResult { let script_meta = opt_result!( script_path.metadata(), @@ -198,11 +215,31 @@ fn read_script_meta( .replace("$NAME", script_dir) }; 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 { script_name: script_name.clone(), script_length: script_meta.len(), script_sha256, script_full_url, single_script_file, + publish_time, + update_time, }) }