From 76ce0c4952a9d1edad48b5928921bc0bcf831459 Mon Sep 17 00:00:00 2001 From: BlueFunny Date: Sat, 27 Jan 2024 23:48:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E5=B0=8F=E9=97=AE=E9=A2=98=EF=BC=8C=E6=9B=B4=E6=8D=A2=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 54 +++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6beae3d..7140ff3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,11 @@ -use log::{debug, error, info, warn}; +use { + log::{error, warn}, + serde::{Deserialize, Serialize}, + serde_json::Result, + std::{env, fs, io::Error}, +}; -use serde::{Deserialize, Serialize}; -use serde_json::Result; - -use std::{env, fs, io::Error}; - -const CONFIG_PATH: &str = "config.json"; +const CONFIG_PATH: &str = "config.toml"; #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Config { @@ -59,34 +59,24 @@ impl Config { .parse::() .unwrap_or_else(|_| { error!("CLUSTER_PORT must be a number"); - std::process::exit(1); + panic!(); }); let cluster_id = env::var("CLUSTER_ID").unwrap_or_else(|_| { error!("CLUSTER_ID must be set"); - std::process::exit(1); + panic!(); }); let cluster_secret = env::var("CLUSTER_SECRET").unwrap_or_else(|_| { error!("CLUSTER_SECRET must be set"); - std::process::exit(1); + panic!(); }); let no_demaon = env::var("NO_DAEMON") .unwrap_or("false".to_string()) .parse::() .unwrap_or_else(|_| { error!("NO_DAEMON must be true or false"); - std::process::exit(1); + panic!(); }); - // Create config - let config = Config::new( - center_url, - host_ip, - host_port, - cluster_id, - cluster_secret, - no_demaon, - ); - // Decrapated warning if env::var("CLUSTER_BYOC").is_ok() { warn!("CLUSTER_BYOC is deprecated, ignored"); @@ -102,19 +92,29 @@ impl Config { // If you want to use Nginx, why would you choose this program? } + // Create config + let config = Config::new( + center_url, + host_ip, + host_port, + cluster_id, + cluster_secret, + no_demaon, + ); + // Save config - Self::save(config); + config.save(); } - pub fn save(config: Config) { - if ! fs::canonicalize(CONFIG_PATH).is_ok() { + pub fn save(&self) { + if !fs::canonicalize(CONFIG_PATH).is_ok() { fs::File::create(CONFIG_PATH).unwrap_or_else(|_| { error!("Failed to create config file"); - std::process::exit(1); + panic!(); }); } - fs::write(CONFIG_PATH, serde_json::to_string(&config).unwrap()).unwrap_or_else(|_| { + fs::write(CONFIG_PATH, toml::to_string(&self).unwrap()).unwrap_or_else(|_| { error!("Failed to save config"); - std::process::exit(1); + panic!(); }); }