feat: add publish date and update time

This commit is contained in:
2025-01-19 15:44:29 +08:00
parent d4d14ff7c6
commit a31d055835
2 changed files with 45 additions and 4 deletions

View File

@@ -3,12 +3,16 @@
"script_name": "helloworld.go", "script_name": "helloworld.go",
"script_length": 128, "script_length": 128,
"script_sha256": "b807f5d29dd2dbd4dc8c665616c836b1e1c0274d377672109a3e2397bb1139bf", "script_sha256": "b807f5d29dd2dbd4dc8c665616c836b1e1c0274d377672109a3e2397bb1139bf",
"script_full_url": "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/helloworld-go/main.go" "script_full_url": "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/helloworld-go/main.go",
"publish_time": 1737272661406,
"update_time": 1737272661406
}, },
"myip.go": { "myip.go": {
"script_name": "myip.go", "script_name": "myip.go",
"script_length": 798, "script_length": 798,
"script_sha256": "2abb41d64288774c01c465508070bec5ba7350228ab0dc79a2fe679b5afb07b6", "script_sha256": "2abb41d64288774c01c465508070bec5ba7350228ab0dc79a2fe679b5afb07b6",
"script_full_url": "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/myip-go/main.go" "script_full_url": "https://git.hatter.ink/hatter/go-scripts/raw/branch/main/myip-go/main.go",
"publish_time": 1737272661405,
"update_time": 1737272661405
} }
} }

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,
}) })
} }