feat: Use default config_dir (from dirs crate) instead of hard-coded UNIX path

This commit is contained in:
Kiril Kovachev 2025-05-08 23:38:55 +01:00
parent 3a33a5d5fc
commit 54b303f632
3 changed files with 73 additions and 13 deletions

73
Cargo.lock generated
View File

@ -355,7 +355,16 @@ version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [ dependencies = [
"dirs-sys", "dirs-sys 0.4.1",
]
[[package]]
name = "dirs"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e"
dependencies = [
"dirs-sys 0.5.0",
] ]
[[package]] [[package]]
@ -366,10 +375,22 @@ checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [ dependencies = [
"libc", "libc",
"option-ext", "option-ext",
"redox_users", "redox_users 0.4.6",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "dirs-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
dependencies = [
"libc",
"option-ext",
"redox_users 0.5.0",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "displaydoc" name = "displaydoc"
version = "0.2.5" version = "0.2.5"
@ -912,6 +933,7 @@ name = "kovachevbot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"dirs 6.0.0",
"dotenvy", "dotenvy",
"formatx", "formatx",
"mwbot", "mwbot",
@ -1063,7 +1085,7 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror 1.0.69",
"tokio", "tokio",
"tracing", "tracing",
"wasm-bindgen-futures", "wasm-bindgen-futures",
@ -1101,7 +1123,7 @@ version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9bd2541dc62753f72872960a187ec77b0b4ca48aaaea00ecdbe8af7064e204b" checksum = "c9bd2541dc62753f72872960a187ec77b0b4ca48aaaea00ecdbe8af7064e204b"
dependencies = [ dependencies = [
"dirs", "dirs 5.0.1",
"libc", "libc",
"mwapi", "mwapi",
"mwapi_responses", "mwapi_responses",
@ -1113,7 +1135,7 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror 1.0.69",
"tokio", "tokio",
"toml", "toml",
"tracing", "tracing",
@ -1150,7 +1172,7 @@ dependencies = [
"bytemuck", "bytemuck",
"regex", "regex",
"serde", "serde",
"thiserror", "thiserror 1.0.69",
"tinyvec", "tinyvec",
] ]
@ -1255,7 +1277,7 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror 1.0.69",
"tokio", "tokio",
"tracing", "tracing",
"url", "url",
@ -1550,7 +1572,18 @@ checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
dependencies = [ dependencies = [
"getrandom 0.2.16", "getrandom 0.2.16",
"libredox", "libredox",
"thiserror", "thiserror 1.0.69",
]
[[package]]
name = "redox_users"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b"
dependencies = [
"getrandom 0.2.16",
"libredox",
"thiserror 2.0.12",
] ]
[[package]] [[package]]
@ -1836,7 +1869,7 @@ version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b1fdf65dd6331831494dd616b30351c38e96e45921a27745cf98490458b90bb" checksum = "8b1fdf65dd6331831494dd616b30351c38e96e45921a27745cf98490458b90bb"
dependencies = [ dependencies = [
"dirs", "dirs 5.0.1",
] ]
[[package]] [[package]]
@ -2002,7 +2035,16 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl 2.0.12",
] ]
[[package]] [[package]]
@ -2016,6 +2058,17 @@ dependencies = [
"syn 2.0.101", "syn 2.0.101",
] ]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.101",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.8" version = "1.1.8"

View File

@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.37", features = ["derive", "env"] } clap = { version = "4.5.37", features = ["derive", "env"] }
dirs = "6.0.0"
dotenvy = "0.15.7" dotenvy = "0.15.7"
formatx = "0.2.4" formatx = "0.2.4"
mwbot = "0.6.1" mwbot = "0.6.1"

View File

@ -1,9 +1,15 @@
use std::path::PathBuf;
use formatx::formatx; use formatx::formatx;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use mwbot::parsoid::WikiMultinode; use mwbot::parsoid::WikiMultinode;
use dirs::config_dir;
const CONFIG_TEMPLATE_PATH: &'static str = "mwbot_template.toml"; const CONFIG_TEMPLATE_PATH: &'static str = "mwbot_template.toml";
const BOT_CONFIG_PATH: &'static str = "~/.config/mwbot.toml"; const BOT_CONFIG_FILENAME: &'static str = "mwbot.toml";
fn get_bot_config_path() -> PathBuf {
config_dir().unwrap().join(BOT_CONFIG_FILENAME)
}
#[derive(Parser)] #[derive(Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@ -44,7 +50,7 @@ enum Action {
fn setup(args: SetupArgs) -> Result<(), std::io::Error> { fn setup(args: SetupArgs) -> Result<(), std::io::Error> {
let config_template = std::fs::read_to_string(CONFIG_TEMPLATE_PATH).or(Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Unable to find {}; did you execute the script from the same directory?", CONFIG_TEMPLATE_PATH))))?; let config_template = std::fs::read_to_string(CONFIG_TEMPLATE_PATH).or(Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Unable to find {}; did you execute the script from the same directory?", CONFIG_TEMPLATE_PATH))))?;
let filled_in_config = formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap(); let filled_in_config = formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap();
let path = shellexpand::tilde(BOT_CONFIG_PATH).into_owned(); let path = get_bot_config_path();
std::fs::write(&path, filled_in_config).unwrap(); std::fs::write(&path, filled_in_config).unwrap();
// Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions. // Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions.
{ {
@ -63,7 +69,7 @@ async fn main() -> Result<(), std::io::Error> {
match cli.action { match cli.action {
Action::Setup(args) => { Action::Setup(args) => {
setup(args)?; setup(args)?;
eprintln!("Successfully set up {BOT_CONFIG_PATH}.") eprintln!("Successfully set up {}.", get_bot_config_path().to_str().unwrap())
}, },
Action::Run => { Action::Run => {
let bot = mwbot::Bot::from_default_config().await.unwrap(); let bot = mwbot::Bot::from_default_config().await.unwrap();