From 8f779d005ff5f0b7712403a031a8815ab0941733 Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Thu, 8 May 2025 23:50:52 +0100 Subject: [PATCH] refactor: Factor out file permission constraint from setup --- src/main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 191eea2..6b29d76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,16 +55,18 @@ fn fill_config_template(config_template: String, args: SetupArgs) -> String { formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap() } +// Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions. +fn constrain_unix_permissions(path: &PathBuf) -> Result<(), std::io::Error> { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)) +} + fn setup(args: SetupArgs) -> Result<(), std::io::Error> { let config_template = read_config_template()?; let filled_in_config = fill_config_template(config_template, args); let path = get_bot_config_path(); std::fs::write(&path, filled_in_config)?; - // Fix permissions on UNIX-like systems, since mwbot-rs doesn't like to read configs with loose permissions. - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?; - } + constrain_unix_permissions(&path)?; Ok(()) }