1use std::collections::BTreeMap;
2use std::fs;
3use std::path::PathBuf;
4use log::info;
5use serde::{Deserialize, Serialize};
6use crate::Connection;
7
8#[derive(Clone, Debug, Deserialize, Serialize)]
9pub struct Config {
10 pub default: String,
11 pub connections: BTreeMap<String, Connection>,
12}
13
14impl Default for Config {
15 fn default() -> Self {
16 let mut connections = BTreeMap::new();
17 connections.insert("my_name".to_string(), Connection::default());
18 Self {
19 default: "my_name".to_string(),
20 connections,
21 }
22 }
23}
24
25impl Config {
26 pub fn create(config_file: PathBuf, pkg_name: bool) -> Config {
30 #[derive(Clone, Debug, Deserialize, Serialize)]
31 pub struct ConfigNew {
32 pub br_oss: Config,
33 }
34 impl ConfigNew {
35 pub fn default() -> ConfigNew {
36 let mut connections = BTreeMap::new();
37 connections.insert("my_name".to_string(), Connection::default());
38 Self {
39 br_oss: Config {
40 default: "my_name".to_string(),
41 connections,
42 },
43 }
44 }
45 }
46 match fs::read_to_string(config_file.clone()) {
47 Ok(e) => {
48 if pkg_name {
49 let data = ConfigNew::default();
50 toml::from_str::<ConfigNew>(&e).unwrap_or_else(|err| {
51 if err.span().unwrap().start == 0 && err.span().unwrap().end == 0 {
52 let toml_str = toml::to_string_pretty(&data).unwrap();
53 let _ = fs::write(config_file.clone(), e + "\r\n\r\n" + &*toml_str);
54 } else {
55 let field = err.message().trim_start_matches("missing field `").trim_end_matches("`");
56 let channel=data.br_oss.connections.get(&data.br_oss.default).unwrap().channel.clone();
57 let res = match field {
58 "default" => format!("\r\n{field} = {}", data.br_oss.default),
59 "access_key_id" => format!("\r\n{field} = {:?}", data.br_oss.connections.get(&data.br_oss.default).unwrap().access_key_id),
60 "access_key_secret" => format!("\r\n{field} = {:?}", data.br_oss.connections.get(&data.br_oss.default).unwrap().access_key_secret),
61 "bucket_name" => format!("\r\n{field} = {:?}", data.br_oss.connections.get(&data.br_oss.default).unwrap().bucket_name),
62 "endpoint" => format!("\r\n{field} = {:?}", data.br_oss.connections.get(&data.br_oss.default).unwrap().endpoint),
63 "channel" => format!("\r\n{field} = \"{channel:?}\""),
64 _ => {
65 info!("配置文件需添加参数: {field}");
66 "".to_string()
67 }
68 };
69 let text = format!("{}{}{}", &e[..err.span().unwrap().end], res, &e[err.span().unwrap().end..]);
70 let _ = fs::write(config_file.clone(), text);
71 }
72 data
73 }).br_oss
74 } else {
75 let data = Config::default();
76 toml::from_str::<Config>(&e).unwrap_or_else(|_| {
77 let toml_str = toml::to_string_pretty(&data).unwrap();
78 let _ = fs::write(config_file.clone(), e + "\r\n\r\n" + &*toml_str);
79 data
80 })
81 }
82 }
83 Err(_) => {
84 if pkg_name {
85 let data = ConfigNew::default();
86 fs::create_dir_all(config_file.parent().unwrap()).unwrap();
87 let toml = toml::to_string_pretty(&data).unwrap();
88 let _ = fs::write(config_file.to_str().unwrap(), toml);
89 data.br_oss
90 } else {
91 let data = Config::default();
92 fs::create_dir_all(config_file.parent().unwrap()).unwrap();
93 let toml = toml::to_string_pretty(&data).unwrap();
94 let _ = fs::write(config_file.to_str().unwrap(), toml);
95 data
96 }
97 }
98 }
99 }
100}