add config
This commit is contained in:
parent
6824266a19
commit
f09fd0c020
@ -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 = "*"
|
||||||
|
35
src/cli.rs
35
src/cli.rs
@ -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 {
|
||||||
|
@ -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
46
src/config.rs
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user