-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/hero): hero 添加config命令以及配置获取实现
- Loading branch information
Showing
8 changed files
with
119 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,7 @@ | |
#resolver = "2" | ||
members = [ | ||
"uymas/*", | ||
"cmd/uymas", | ||
"cmd/hero", | ||
"cmd/*", | ||
] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
||
private/ | ||
|
||
# ide | ||
.idea | ||
|
||
# 服务配置文件 | ||
config.toml | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,5 @@ | |
|
||
支持工具 | ||
|
||
- [ ] config 配置文件管理 | ||
- [x] config 配置文件管理 | ||
- [ ] log 日志支持 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::CONFIG; | ||
use cli::args::Args; | ||
|
||
pub struct App {} | ||
|
||
impl App { | ||
// 配置文件获取 | ||
pub fn config(_: &Args) { | ||
let decode = toml::to_string(&CONFIG.clone()); | ||
if decode.is_err() { | ||
println!("config 序列号字符串内容错误。\n {}", decode.err().unwrap()); | ||
return; | ||
} | ||
|
||
println!("-------------- config --------------"); | ||
println!(); | ||
println!("{}", decode.unwrap()); | ||
} | ||
|
||
// 帮助提示 | ||
pub fn help(arg: &Args) { | ||
let command = arg.sub_command.clone(); | ||
if !command.is_empty() { | ||
println!("{} 命令不存在", command); | ||
return; | ||
} | ||
println!("config 配置信息查看"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,59 @@ | ||
use cli::args::project_path; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] | ||
pub struct CfgLog { | ||
#[serde(default = "def_str_empty")] | ||
level: String, | ||
} | ||
|
||
// 配合下默认为空 | ||
fn def_str_empty() -> String { | ||
String::new() | ||
} | ||
|
||
/// 系统配置文件 | ||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Default)] | ||
pub struct Config {} | ||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] | ||
pub struct Config { | ||
log: CfgLog, | ||
} | ||
|
||
impl Config { | ||
pub fn new() -> Self { | ||
Default::default() | ||
match read_file() { | ||
Ok(cfg) => cfg, | ||
_ => Default::default(), | ||
} | ||
} | ||
} | ||
|
||
/// 通过配置文件读取配置 | ||
pub fn read_file() -> Result<Config, String> { | ||
let read_rslt = File::open(project_path("config.toml")); | ||
if let Err(er) = read_rslt { | ||
return Err(format!("读取文件错误,\n reference -> {}", er)); | ||
} | ||
|
||
let mut buf = String::new(); | ||
if let Err(msg) = read_rslt.unwrap().read_to_string(&mut buf) { | ||
return Err(format!("读取文件错误,\n reference -> {}", msg)); | ||
} | ||
let yaml_rslt = toml::from_str(buf.as_str()); | ||
if let Err(er) = yaml_rslt { | ||
return Err(format!("读取解析错误,\n reference -> {}", er)); | ||
} | ||
|
||
return Ok(yaml_rslt.unwrap()); | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Config { | ||
log: CfgLog { | ||
level: "trace".to_string(), | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters