1
0

add config

This commit is contained in:
syui 2025-05-20 23:37:00 +09:00
parent 6824266a19
commit f09fd0c020
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56
6 changed files with 89 additions and 22 deletions

View File

@ -9,3 +9,4 @@ serde_json = "1.0"
chrono = "0.4" chrono = "0.4"
seahorse = "*" seahorse = "*"
rusqlite = { version = "0.29", features = ["serde_json"] } rusqlite = { version = "0.29", features = ["serde_json"] }
shellexpand = "*"

View File

@ -1,15 +1,15 @@
//src/cli.rs // src/cli.rs
use seahorse::{App, Command, Context}; use std::path::{Path};
use crate::utils::{load_config, save_config};
use crate::commands::db::{save_cmd, export_cmd};
use crate::agent::AIState;
use chrono::{Duration, Local}; use chrono::{Duration, Local};
use rusqlite::Connection; 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") let set_cmd = Command::new("set")
.usage("set [trust|intimacy|curiosity] [value]") .usage("set [trust|intimacy|curiosity] [value]")
.action(|c: &Context| { .action(|c: &Context| {
@ -24,9 +24,13 @@ pub fn cli_app() -> App {
std::process::exit(1); std::process::exit(1);
}); });
let json_path = "config/config.json"; // ConfigPathsを使って設定ファイルのパスを取得
let db_path = "config/ai_state.db"; let config_paths = ConfigPaths::new();
let mut ai = load_config(json_path); 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() { match field.as_str() {
"trust" => ai.relationship.trust = value, "trust" => ai.relationship.trust = value,
@ -37,9 +41,9 @@ pub fn cli_app() -> App {
std::process::exit(1); 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保存失敗"); ai.save_to_db(&conn).expect("DB保存失敗");
println!("{field}{value} に更新しました"); println!("{field}{value} に更新しました");
@ -48,14 +52,17 @@ pub fn cli_app() -> App {
let show_cmd = Command::new("show") let show_cmd = Command::new("show")
.usage("show") .usage("show")
.action(|_c: &Context| { .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); println!("🧠 現在のAI状態:\n{:#?}", ai);
}); });
let talk_cmd = Command::new("talk") let talk_cmd = Command::new("talk")
.usage("talk") .usage("talk")
.action(|_c: &Context| { .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 now = Local::now().naive_local();
let mut state = AIState { let mut state = AIState {

View File

@ -1,7 +1,9 @@
// src/commands/db.rs // src/commands/db.rs
use seahorse::{Command, Context}; use seahorse::{Command, Context};
use crate::utils::load_config; use crate::utils::{load_config};
use crate::model::AiSystem; use crate::model::AiSystem;
use crate::config::ConfigPaths;
use rusqlite::Connection; use rusqlite::Connection;
use std::fs; use std::fs;
@ -9,8 +11,14 @@ pub fn save_cmd() -> Command {
Command::new("save") Command::new("save")
.usage("save") .usage("save")
.action(|_c: &Context| { .action(|_c: &Context| {
let ai = load_config("config/config.json"); let paths = ConfigPaths::new();
let conn = Connection::open("config/ai_state.db").expect("DB接続失敗");
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保存失敗"); ai.save_to_db(&conn).expect("DB保存失敗");
println!("💾 DBに保存完了"); println!("💾 DBに保存完了");
}) })
@ -20,13 +28,17 @@ pub fn export_cmd() -> Command {
Command::new("export") Command::new("export")
.usage("export [output.json]") .usage("export [output.json]")
.action(|c: &Context| { .action(|c: &Context| {
let path = c.args.get(0).map(|s| s.as_str()).unwrap_or("output.json"); let output_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 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 ai = AiSystem::load_from_db(&conn).expect("DB読み込み失敗");
let json = serde_json::to_string_pretty(&ai).expect("JSON変換失敗"); 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}");
}) })
} }

46
src/config.rs Normal file
View File

@ -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());
}
}
}
}

View File

@ -5,6 +5,7 @@ mod agent;
mod cli; mod cli;
mod utils; mod utils;
mod commands; mod commands;
mod config;
use cli::cli_app; use cli::cli_app;
use seahorse::App; use seahorse::App;