Skip to content

Commit

Permalink
feat(cmd/hero): hero 添加config命令以及配置获取实现
Browse files Browse the repository at this point in the history
  • Loading branch information
conero committed Sep 13, 2023
1 parent c3af6ff commit 4105610
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#resolver = "2"
members = [
"uymas/*",
"cmd/uymas",
"cmd/hero",
"cmd/*",
]


Expand Down
23 changes: 23 additions & 0 deletions cmd/hero/.gitignore
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
1 change: 1 addition & 0 deletions cmd/hero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env_logger = "0.10.0"
lazy_static = "1.4.0"
log = "0.4.20"
serde = { version = "1.0.188", features = ["derive"] }
toml = "0.8.0"

[features]
# 系统开发编译
Expand Down
2 changes: 1 addition & 1 deletion cmd/hero/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

支持工具

- [ ] config 配置文件管理
- [x] config 配置文件管理
- [ ] log 日志支持
29 changes: 29 additions & 0 deletions cmd/hero/src/app.rs
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 配置信息查看");
}
}
54 changes: 51 additions & 3 deletions cmd/hero/src/config.rs
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(),
},
}
}
}
12 changes: 10 additions & 2 deletions cmd/hero/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::App;
use crate::config::Config;
use cli::args::Args;
use cli::cmd;
Expand All @@ -15,6 +16,8 @@ mod config;
/// 日志支持
#[cfg(log)]
mod log;
// 系统应用IP
mod app;

fn log_init() {
// 日志初始化
Expand All @@ -26,13 +29,18 @@ fn main() {
let mut app = cmd::Cmd::new();

// 系统如可命令
app.empty(|_: &Args| {
app.empty(|arg: &Args| {
if arg.contain_opts(vec!["help", "h"]) {
App::help(arg);
return;
}
println!("这一个系统命令行脚手架项目");
println!();
println!("包含主要功能如下:");
println!(" . 系统配置文件 config ");
println!();
});

app.register("config", App::config);
app.register("help", App::help);
app.run();
}
3 changes: 3 additions & 0 deletions doc/Release.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
- **cli**
- pref!(Cmd.register_action): 传递参数更改为非“Box”处理
- feat(Cmd.registers): 新增方法,用于多参数注册
- **cmd/hero**
- feat: 新增包 hero 实现config、log等命令行脚手架模板




Expand Down

0 comments on commit 4105610

Please sign in to comment.