From f09fd0c020a353cbcddf39d043318d0eedcd6938 Mon Sep 17 00:00:00 2001 From: syui Date: Tue, 20 May 2025 23:37:00 +0900 Subject: [PATCH] add config --- Cargo.toml | 1 + config/config.json => example.json | 0 src/cli.rs | 37 ++++++++++++++---------- src/commands/db.rs | 26 ++++++++++++----- src/config.rs | 46 ++++++++++++++++++++++++++++++ src/main.rs | 1 + 6 files changed, 89 insertions(+), 22 deletions(-) rename config/config.json => example.json (100%) create mode 100644 src/config.rs diff --git a/Cargo.toml b/Cargo.toml index 21198f1..e6d0755 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ serde_json = "1.0" chrono = "0.4" seahorse = "*" rusqlite = { version = "0.29", features = ["serde_json"] } +shellexpand = "*" diff --git a/config/config.json b/example.json similarity index 100% rename from config/config.json rename to example.json diff --git a/src/cli.rs b/src/cli.rs index 37cd8c5..f6c90d7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,15 +1,15 @@ -//src/cli.rs -use seahorse::{App, Command, Context}; - -use crate::utils::{load_config, save_config}; -use crate::commands::db::{save_cmd, export_cmd}; - -use crate::agent::AIState; +// src/cli.rs +use std::path::{Path}; use chrono::{Duration, Local}; use rusqlite::Connection; -pub fn cli_app() -> App { +use seahorse::{App, Command, Context}; +use crate::utils::{load_config, save_config}; +use crate::commands::db::{save_cmd, export_cmd}; +use crate::config::ConfigPaths; +use crate::agent::AIState; +pub fn cli_app() -> App { let set_cmd = Command::new("set") .usage("set [trust|intimacy|curiosity] [value]") .action(|c: &Context| { @@ -24,9 +24,13 @@ pub fn cli_app() -> App { std::process::exit(1); }); - let json_path = "config/config.json"; - let db_path = "config/ai_state.db"; - let mut ai = load_config(json_path); + // ConfigPathsを使って設定ファイルのパスを取得 + let config_paths = ConfigPaths::new(); + let json_path = config_paths.data_file("json"); + // まだ user.json がない場合、example.json をコピー + config_paths.ensure_file_exists("json", Path::new("example.json")); + let db_path = config_paths.data_file("db"); + let mut ai = load_config(json_path.to_str().unwrap()); match field.as_str() { "trust" => ai.relationship.trust = value, @@ -37,9 +41,9 @@ pub fn cli_app() -> App { std::process::exit(1); } } - save_config(json_path, &ai); + save_config(json_path.to_str().unwrap(), &ai); - let conn = Connection::open(db_path).expect("DB接続失敗"); + let conn = Connection::open(db_path.to_str().unwrap()).expect("DB接続失敗"); ai.save_to_db(&conn).expect("DB保存失敗"); println!("✅ {field} を {value} に更新しました"); @@ -48,14 +52,17 @@ pub fn cli_app() -> App { let show_cmd = Command::new("show") .usage("show") .action(|_c: &Context| { - let ai = load_config("config/config.json"); + // ConfigPathsを使って設定ファイルのパスを取得 + let config_paths = ConfigPaths::new(); + let ai = load_config(config_paths.data_file("json").to_str().unwrap()); println!("🧠 現在のAI状態:\n{:#?}", ai); }); let talk_cmd = Command::new("talk") .usage("talk") .action(|_c: &Context| { - let ai = load_config("config/config.json"); + let config_paths = ConfigPaths::new(); + let ai = load_config(config_paths.data_file("json").to_str().unwrap()); let now = Local::now().naive_local(); let mut state = AIState { diff --git a/src/commands/db.rs b/src/commands/db.rs index 1e3c342..6934a36 100644 --- a/src/commands/db.rs +++ b/src/commands/db.rs @@ -1,7 +1,9 @@ // src/commands/db.rs use seahorse::{Command, Context}; -use crate::utils::load_config; +use crate::utils::{load_config}; use crate::model::AiSystem; +use crate::config::ConfigPaths; + use rusqlite::Connection; use std::fs; @@ -9,8 +11,14 @@ pub fn save_cmd() -> Command { Command::new("save") .usage("save") .action(|_c: &Context| { - let ai = load_config("config/config.json"); - let conn = Connection::open("config/ai_state.db").expect("DB接続失敗"); + let paths = ConfigPaths::new(); + + let json_path = paths.data_file("json"); + let db_path = paths.data_file("db"); + + let ai = load_config(json_path.to_str().unwrap()); + let conn = Connection::open(db_path).expect("DB接続失敗"); + ai.save_to_db(&conn).expect("DB保存失敗"); println!("💾 DBに保存完了"); }) @@ -20,13 +28,17 @@ pub fn export_cmd() -> Command { Command::new("export") .usage("export [output.json]") .action(|c: &Context| { - let path = c.args.get(0).map(|s| s.as_str()).unwrap_or("output.json"); - let conn = Connection::open("config/ai_state.db").expect("DB接続失敗"); + let output_path = c.args.get(0).map(|s| s.as_str()).unwrap_or("output.json"); + + let paths = ConfigPaths::new(); + let db_path = paths.data_file("db"); + + let conn = Connection::open(db_path).expect("DB接続失敗"); let ai = AiSystem::load_from_db(&conn).expect("DB読み込み失敗"); let json = serde_json::to_string_pretty(&ai).expect("JSON変換失敗"); - fs::write(path, json).expect("ファイル書き込み失敗"); + fs::write(output_path, json).expect("ファイル書き込み失敗"); - println!("📤 JSONにエクスポート完了: {path}"); + println!("📤 JSONにエクスポート完了: {output_path}"); }) } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..6551cd8 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,46 @@ +// src/config.rs +use std::fs; +use std::path::{Path, PathBuf}; +use shellexpand; + +pub struct ConfigPaths { + pub base_dir: PathBuf, +} + +impl ConfigPaths { + pub fn new() -> Self { + let app_name = env!("CARGO_PKG_NAME"); + let mut base_dir = shellexpand::tilde("~").to_string(); + base_dir.push_str(&format!("/.config/{}/", app_name)); + let base_path = Path::new(&base_dir); + if !base_path.exists() { + let _ = fs::create_dir_all(base_path); + } + + ConfigPaths { + base_dir: base_path.to_path_buf(), + } + } + + pub fn data_file(&self, file_name: &str) -> PathBuf { + let file_path = match file_name { + "db" => self.base_dir.join("user.db"), + "toml" => self.base_dir.join("user.toml"), + "json" => self.base_dir.join("user.json"), + _ => self.base_dir.join(format!(".{}", file_name)), + }; + + file_path + } + /// 設定ファイルがなければ `example.json` をコピーする + pub fn ensure_file_exists(&self, file_name: &str, template_path: &Path) { + let target = self.data_file(file_name); + if !target.exists() { + if let Err(e) = fs::copy(template_path, &target) { + eprintln!("⚠️ 設定ファイルの初期化に失敗しました: {}", e); + } else { + println!("📄 {} を {} にコピーしました", template_path.display(), target.display()); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 9f8642d..993e504 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod agent; mod cli; mod utils; mod commands; +mod config; use cli::cli_app; use seahorse::App;