实现一下 reader from self
This commit is contained in:
parent
22ae6b3326
commit
6c5b177cd9
@ -61,7 +61,7 @@ pub const HELP_MESSAGE_ZH: &str = r#"call [选项] [--] [参数]
|
||||
pub fn show_help() {
|
||||
#[cfg(windows)]
|
||||
crate::win::attach_console();
|
||||
println!("version: {}", crate::VERSION);
|
||||
println!("call: {}", crate::VERSION);
|
||||
match std::env::var("LANG") {
|
||||
Ok(lang) => {
|
||||
println!("{}", lang);
|
||||
@ -223,10 +223,6 @@ impl RawConfig {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn from_executeable() -> Option<Self> {
|
||||
crate::reader::read_self()
|
||||
}
|
||||
|
||||
pub fn from_config(config_path: Option<PathBuf>) -> Option<Self> {
|
||||
if config_path.is_none() {
|
||||
let config_path = PathBuf::from("./run.conf");
|
||||
@ -332,7 +328,7 @@ impl Config {
|
||||
|
||||
pub fn from_cli() -> Self {
|
||||
let cli_conf = RawConfig::from_cli();
|
||||
let execueable_conf = RawConfig::from_executeable();
|
||||
let execueable_conf = crate::reader::read_self();
|
||||
|
||||
todo!("from_cli")
|
||||
}
|
||||
|
@ -7,6 +7,8 @@
|
||||
//!
|
||||
|
||||
use blake3::Hasher;
|
||||
use toml::{from_str, Value as TomlValue};
|
||||
|
||||
|
||||
pub fn read_self() -> Option<crate::config::RawConfig> {
|
||||
// 先校验最后部分是否为合法的校验码
|
||||
@ -27,10 +29,39 @@ pub fn read_self() -> Option<crate::config::RawConfig> {
|
||||
);
|
||||
return None;
|
||||
}
|
||||
let (data, data_len) = data.split_at(4);
|
||||
let data_len = u32::from_le_bytes(data_len.try_into().unwrap()) as usize;
|
||||
// 校验长度
|
||||
// 长度不应大于 data.len()
|
||||
if data_len > data.len() {
|
||||
println!("长度不匹配 {} {}", data_len, data.len());
|
||||
return None;
|
||||
}
|
||||
let (_, data) = data.split_at(data_len);
|
||||
let data = std::str::from_utf8(data).ok()?;
|
||||
let config_value: TomlValue = from_str(data).ok()?;
|
||||
|
||||
// 然后解析配置文件
|
||||
let show_console = config_value.get("show_console").and_then(|x| x.as_bool());
|
||||
let verbose = config_value.get("verbose").and_then(|x| x.as_bool());
|
||||
let chdir = config_value
|
||||
.get("chdir")
|
||||
.and_then(|x| x.as_str())
|
||||
.map(|x| x.to_string());
|
||||
let bin = config_value.get("bin").and_then(|x| x.as_str()).map(|x| x.to_string());
|
||||
let bin_arg = config_value
|
||||
.get("bin_arg")
|
||||
.and_then(|x| x.as_str())
|
||||
.map(|x| x.to_string());
|
||||
let config = config_value.get("config").and_then(|x| x.as_str()).map(|x| x.to_string());
|
||||
|
||||
None
|
||||
Some(crate::config::RawConfig {
|
||||
show_console,
|
||||
verbose,
|
||||
chdir,
|
||||
bin,
|
||||
bin_arg,
|
||||
config,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_self_raw() -> Option<Vec<u8>> {
|
||||
|
Loading…
Reference in New Issue
Block a user