fix cfg path
This commit is contained in:
@@ -1,17 +1,28 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::Deserialize;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub path: Option<String>,
|
pub path: Option<String>,
|
||||||
pub did: Option<String>,
|
pub did: Option<String>,
|
||||||
pub handle: Option<String>,
|
pub handle: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct ConfigFile {
|
||||||
|
bot: Option<BotConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct BotConfig {
|
||||||
|
did: Option<String>,
|
||||||
|
handle: Option<String>,
|
||||||
|
path: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn config_file() -> PathBuf {
|
pub fn config_file() -> PathBuf {
|
||||||
dirs::config_dir()
|
dirs::config_dir()
|
||||||
.unwrap_or_else(|| PathBuf::from("."))
|
.unwrap_or_else(|| PathBuf::from("."))
|
||||||
@@ -19,15 +30,30 @@ pub fn config_file() -> PathBuf {
|
|||||||
.join("config.json")
|
.join("config.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load() -> Config {
|
fn expand_path(p: &str) -> PathBuf {
|
||||||
let path = config_file();
|
if p.starts_with("~/") {
|
||||||
match fs::read_to_string(&path) {
|
dirs::home_dir()
|
||||||
Ok(content) => serde_json::from_str(&content).unwrap_or(defaults()),
|
.unwrap_or_else(|| PathBuf::from("."))
|
||||||
Err(_) => defaults(),
|
.join(&p[2..])
|
||||||
|
} else {
|
||||||
|
PathBuf::from(p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load() -> Config {
|
||||||
|
let cfg_path = config_file();
|
||||||
|
if let Ok(content) = fs::read_to_string(&cfg_path) {
|
||||||
|
if let Ok(file) = serde_json::from_str::<ConfigFile>(&content) {
|
||||||
|
if let Some(bot) = file.bot {
|
||||||
|
return Config {
|
||||||
|
path: bot.path,
|
||||||
|
did: bot.did,
|
||||||
|
handle: bot.handle,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn defaults() -> Config {
|
|
||||||
Config {
|
Config {
|
||||||
path: None,
|
path: None,
|
||||||
did: None,
|
did: None,
|
||||||
@@ -42,9 +68,11 @@ pub fn init() {
|
|||||||
let _ = fs::create_dir_all(parent);
|
let _ = fs::create_dir_all(parent);
|
||||||
}
|
}
|
||||||
let default_cfg = json!({
|
let default_cfg = json!({
|
||||||
"path": null,
|
"bot": {
|
||||||
"did": null,
|
"did": null,
|
||||||
"handle": null
|
"handle": null,
|
||||||
|
"path": null
|
||||||
|
}
|
||||||
});
|
});
|
||||||
let _ = fs::write(&cfg_path, serde_json::to_string_pretty(&default_cfg).unwrap());
|
let _ = fs::write(&cfg_path, serde_json::to_string_pretty(&default_cfg).unwrap());
|
||||||
}
|
}
|
||||||
@@ -80,15 +108,7 @@ pub fn init() {
|
|||||||
|
|
||||||
pub fn base_dir(cfg: &Config) -> PathBuf {
|
pub fn base_dir(cfg: &Config) -> PathBuf {
|
||||||
match &cfg.path {
|
match &cfg.path {
|
||||||
Some(p) => {
|
Some(p) => expand_path(p),
|
||||||
if p.starts_with("~/") {
|
|
||||||
dirs::home_dir()
|
|
||||||
.unwrap_or_else(|| PathBuf::from("."))
|
|
||||||
.join(&p[2..])
|
|
||||||
} else {
|
|
||||||
PathBuf::from(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => dirs::config_dir()
|
None => dirs::config_dir()
|
||||||
.unwrap_or_else(|| PathBuf::from("."))
|
.unwrap_or_else(|| PathBuf::from("."))
|
||||||
.join("ai.syui.gpt"),
|
.join("ai.syui.gpt"),
|
||||||
|
|||||||
Reference in New Issue
Block a user