diff --git a/.config/ai/scpt b/.config/ai/scpt index 7a4d642..a1905d1 160000 --- a/.config/ai/scpt +++ b/.config/ai/scpt @@ -1 +1 @@ -Subproject commit 7a4d642e418bbe43d1b5cfaa8cf7ead45363e5dd +Subproject commit a1905d104b9e7a5a9799d32febb27289a4ff420f diff --git a/Cargo.toml b/Cargo.toml index 8f34ec0..fbc61fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ description = "latest@2024-08-18" seahorse = "*" reqwest = { version = "*", features = ["blocking", "json"] } tokio = { version = "1", features = ["full"] } -shellexpand = "*" config = "*" serde = "*" serde_json = "*" diff --git a/src/data.rs b/src/data.rs index f7835b4..0a49911 100644 --- a/src/data.rs +++ b/src/data.rs @@ -5,37 +5,50 @@ use std::fs; use std::fs::OpenOptions; use std::io::Read; use std::io::Write; -use std::path::Path; +use std::path::{Path, PathBuf}; +use std::env; + +/// ホームディレクトリパスを展開するユーティリティ関数 +/// "~"で始まるパスをユーザーのホームディレクトリに展開します +fn expand_home_path(path: &str) -> PathBuf { + if path.starts_with("~") { + let home = env::var("HOME").unwrap_or_else(|_| ".".to_string()); + let path_without_tilde = path.strip_prefix("~/").unwrap_or(&path[1..]); + PathBuf::from(home).join(path_without_tilde) + } else { + PathBuf::from(path) + } +} pub fn data_file(s: &str) -> String { - let file = "/.config/ai/"; - let mut f = shellexpand::tilde("~").to_string(); - f.push_str(&file); - let path = Path::new(&f); - if path.is_dir() == false { - let _ = fs::create_dir_all(f.clone()); + let path = expand_home_path("~/.config/ai"); + + if !path.is_dir() { + let _ = fs::create_dir_all(&path); } + + let mut path_str = path.to_string_lossy().to_string(); match &*s { - "toml" => f + &"token.toml", - "json" => f + &"token.json", - "refresh" => f + &"refresh.toml", - _ => f + &"." + &s, + "toml" => path_str + "/token.toml", + "json" => path_str + "/token.json", + "refresh" => path_str + "/refresh.toml", + _ => path_str + "/." + &s, } } pub fn log_file(s: &str) -> String { - let file = "/.config/ai/txt/"; - let mut f = shellexpand::tilde("~").to_string(); - f.push_str(&file); - let path = Path::new(&f); - if path.is_dir() == false { - let _ = fs::create_dir_all(f.clone()); + let path = expand_home_path("~/.config/ai/txt"); + + if !path.is_dir() { + let _ = fs::create_dir_all(&path); } + + let mut path_str = path.to_string_lossy().to_string(); match &*s { - "n1" => f + &"notify_cid.txt", - "n2" => f + &"notify_cid_run.txt", - "c1" => f + &"comment_cid.txt", - _ => f + &s, + "n1" => path_str + "/notify_cid.txt", + "n2" => path_str + "/notify_cid_run.txt", + "c1" => path_str + "/comment_cid.txt", + _ => path_str + "/" + &s, } } @@ -262,11 +275,9 @@ pub fn data_refresh(s: &str) -> String { } pub fn data_scpt(s: &str) -> String { - let s = String::from(s); - let file = "/.config/ai/scpt/".to_owned() + &s + &".zsh"; - let mut f = shellexpand::tilde("~").to_string(); - f.push_str(&file); - return f; + let mut path = expand_home_path("~/.config/ai/scpt"); + path.push(format!("{}.zsh", s)); + path.to_string_lossy().to_string() } #[derive(Serialize, Deserialize)] @@ -603,11 +614,10 @@ pub fn w_cid(cid: String, file: String, t: bool) -> bool { } pub fn c_follow_all() { - let file = "/.config/ai/scpt/follow_all.zsh"; - let mut f = shellexpand::tilde("~").to_string(); - f.push_str(&file); + let path = expand_home_path("~/.config/ai/scpt/follow_all.zsh"); + use std::process::Command; - let output = Command::new(&f).output().expect("zsh"); + let output = Command::new(path.to_str().unwrap()).output().expect("zsh"); let d = String::from_utf8_lossy(&output.stdout); let d = "\n".to_owned() + &d.to_string(); println!("{}", d); @@ -617,9 +627,10 @@ pub fn c_openai_key(c: &Context) { let api = c.args[0].to_string(); let o = "api='".to_owned() + &api.to_string() + &"'".to_owned(); let o = o.to_string(); - let l = shellexpand::tilde("~") + "/.config/ai/openai.toml"; - let l = l.to_string(); - let mut l = fs::File::create(l).unwrap(); + + let path = expand_home_path("~/.config/ai/openai.toml"); + + let mut l = fs::File::create(&path).unwrap(); if o != "" { l.write_all(&o.as_bytes()).unwrap(); } @@ -628,9 +639,10 @@ pub fn c_openai_key(c: &Context) { impl Open { pub fn new() -> Result { - let d = shellexpand::tilde("~") + "/.config/ai/openai.toml"; + let path = expand_home_path("~/.config/ai/openai.toml"); + let s = Config::builder() - .add_source(File::with_name(&d)) + .add_source(File::with_name(path.to_str().unwrap())) .add_source(config::Environment::with_prefix("APP")) .build()?; s.try_deserialize()