rm shellexpand
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 11m53s
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 11m53s
This commit is contained in:
@ -17,7 +17,6 @@ path = "src/alias.rs"
|
|||||||
seahorse = "*"
|
seahorse = "*"
|
||||||
reqwest = { version = "*", features = ["blocking", "json"] }
|
reqwest = { version = "*", features = ["blocking", "json"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
shellexpand = "*"
|
|
||||||
config = "*"
|
config = "*"
|
||||||
serde = "*"
|
serde = "*"
|
||||||
serde_json = "*"
|
serde_json = "*"
|
||||||
|
134
src/data.rs
134
src/data.rs
@ -5,94 +5,51 @@ use std::fs;
|
|||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::Write;
|
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 {
|
pub fn data_file(s: &str) -> String {
|
||||||
// 新しい設定ディレクトリ(優先)
|
let path = expand_home_path("~/.config/ai");
|
||||||
let new_config_dir = "/.config/syui/ai/bot/";
|
|
||||||
let mut new_path = shellexpand::tilde("~").to_string();
|
|
||||||
new_path.push_str(&new_config_dir);
|
|
||||||
|
|
||||||
// 旧設定ディレクトリ(互換性のため)
|
if !path.is_dir() {
|
||||||
let old_config_dir = "/.config/ai/";
|
let _ = fs::create_dir_all(&path);
|
||||||
let mut old_path = shellexpand::tilde("~").to_string();
|
|
||||||
old_path.push_str(&old_config_dir);
|
|
||||||
|
|
||||||
// 新しいディレクトリを作成
|
|
||||||
let new_dir = Path::new(&new_path);
|
|
||||||
if !new_dir.is_dir() {
|
|
||||||
let _ = fs::create_dir_all(new_path.clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename = match &*s {
|
let mut path_str = path.to_string_lossy().to_string();
|
||||||
"toml" => "token.toml",
|
match &*s {
|
||||||
"json" => "token.json",
|
"toml" => path_str + "/token.toml",
|
||||||
"refresh" => "refresh.toml",
|
"json" => path_str + "/token.json",
|
||||||
_ => &format!(".{}", s),
|
"refresh" => path_str + "/refresh.toml",
|
||||||
};
|
_ => path_str + "/." + &s,
|
||||||
|
|
||||||
let new_file = new_path.clone() + filename;
|
|
||||||
let old_file = old_path + filename;
|
|
||||||
|
|
||||||
// 新しいパスにファイルが存在する場合は新しいパスを使用
|
|
||||||
if Path::new(&new_file).exists() {
|
|
||||||
return new_file;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 旧パスにファイルが存在し、新しいパスに存在しない場合は移行を試行
|
|
||||||
if Path::new(&old_file).exists() && !Path::new(&new_file).exists() {
|
|
||||||
if let Ok(_) = fs::copy(&old_file, &new_file) {
|
|
||||||
eprintln!("Migrated config file: {} -> {}", old_file, new_file);
|
|
||||||
return new_file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// デフォルトは新しいパス
|
|
||||||
new_file
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn log_file(s: &str) -> String {
|
pub fn log_file(s: &str) -> String {
|
||||||
// 新しい設定ディレクトリ(優先)
|
let path = expand_home_path("~/.config/ai/txt");
|
||||||
let new_log_dir = "/.config/syui/ai/bot/txt/";
|
|
||||||
let mut new_path = shellexpand::tilde("~").to_string();
|
|
||||||
new_path.push_str(&new_log_dir);
|
|
||||||
|
|
||||||
// 旧設定ディレクトリ(互換性のため)
|
if !path.is_dir() {
|
||||||
let old_log_dir = "/.config/ai/txt/";
|
let _ = fs::create_dir_all(&path);
|
||||||
let mut old_path = shellexpand::tilde("~").to_string();
|
|
||||||
old_path.push_str(&old_log_dir);
|
|
||||||
|
|
||||||
// 新しいディレクトリを作成
|
|
||||||
let new_dir = Path::new(&new_path);
|
|
||||||
if !new_dir.is_dir() {
|
|
||||||
let _ = fs::create_dir_all(new_path.clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename = match &*s {
|
let mut path_str = path.to_string_lossy().to_string();
|
||||||
"n1" => "notify_cid.txt",
|
match &*s {
|
||||||
"n2" => "notify_cid_run.txt",
|
"n1" => path_str + "/notify_cid.txt",
|
||||||
"c1" => "comment_cid.txt",
|
"n2" => path_str + "/notify_cid_run.txt",
|
||||||
_ => s,
|
"c1" => path_str + "/comment_cid.txt",
|
||||||
};
|
_ => path_str + "/" + &s,
|
||||||
|
|
||||||
let new_file = new_path.clone() + filename;
|
|
||||||
let old_file = old_path + filename;
|
|
||||||
|
|
||||||
// 新しいパスにファイルが存在する場合は新しいパスを使用
|
|
||||||
if Path::new(&new_file).exists() {
|
|
||||||
return new_file;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 旧パスにファイルが存在し、新しいパスに存在しない場合は移行を試行
|
|
||||||
if Path::new(&old_file).exists() && !Path::new(&new_file).exists() {
|
|
||||||
if let Ok(_) = fs::copy(&old_file, &new_file) {
|
|
||||||
eprintln!("Migrated log file: {} -> {}", old_file, new_file);
|
|
||||||
return new_file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// デフォルトは新しいパス
|
|
||||||
new_file
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Token {
|
impl Token {
|
||||||
@ -318,11 +275,9 @@ pub fn data_refresh(s: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn data_scpt(s: &str) -> String {
|
pub fn data_scpt(s: &str) -> String {
|
||||||
let s = String::from(s);
|
let mut path = expand_home_path("~/.config/ai/scpt");
|
||||||
let file = "/.config/ai/scpt/".to_owned() + &s + &".zsh";
|
path.push(format!("{}.zsh", s));
|
||||||
let mut f = shellexpand::tilde("~").to_string();
|
path.to_string_lossy().to_string()
|
||||||
f.push_str(&file);
|
|
||||||
return f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
@ -659,11 +614,10 @@ pub fn w_cid(cid: String, file: String, t: bool) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn c_follow_all() {
|
pub fn c_follow_all() {
|
||||||
let file = "/.config/ai/scpt/follow_all.zsh";
|
let path = expand_home_path("~/.config/ai/scpt/follow_all.zsh");
|
||||||
let mut f = shellexpand::tilde("~").to_string();
|
|
||||||
f.push_str(&file);
|
|
||||||
use std::process::Command;
|
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 = String::from_utf8_lossy(&output.stdout);
|
||||||
let d = "\n".to_owned() + &d.to_string();
|
let d = "\n".to_owned() + &d.to_string();
|
||||||
println!("{}", d);
|
println!("{}", d);
|
||||||
@ -673,9 +627,10 @@ pub fn c_openai_key(c: &Context) {
|
|||||||
let api = c.args[0].to_string();
|
let api = c.args[0].to_string();
|
||||||
let o = "api='".to_owned() + &api.to_string() + &"'".to_owned();
|
let o = "api='".to_owned() + &api.to_string() + &"'".to_owned();
|
||||||
let o = o.to_string();
|
let o = o.to_string();
|
||||||
let l = shellexpand::tilde("~") + "/.config/ai/openai.toml";
|
|
||||||
let l = l.to_string();
|
let path = expand_home_path("~/.config/ai/openai.toml");
|
||||||
let mut l = fs::File::create(l).unwrap();
|
|
||||||
|
let mut l = fs::File::create(&path).unwrap();
|
||||||
if o != "" {
|
if o != "" {
|
||||||
l.write_all(&o.as_bytes()).unwrap();
|
l.write_all(&o.as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
@ -684,9 +639,10 @@ pub fn c_openai_key(c: &Context) {
|
|||||||
|
|
||||||
impl Open {
|
impl Open {
|
||||||
pub fn new() -> Result<Self, ConfigError> {
|
pub fn new() -> Result<Self, ConfigError> {
|
||||||
let d = shellexpand::tilde("~") + "/.config/ai/openai.toml";
|
let path = expand_home_path("~/.config/ai/openai.toml");
|
||||||
|
|
||||||
let s = Config::builder()
|
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"))
|
.add_source(config::Environment::with_prefix("APP"))
|
||||||
.build()?;
|
.build()?;
|
||||||
s.try_deserialize()
|
s.try_deserialize()
|
||||||
|
Reference in New Issue
Block a user