From 8daabd2b995e97e40176bcc405c676ea5d963592 Mon Sep 17 00:00:00 2001 From: Predrag Minic Date: Sat, 26 Aug 2023 17:49:12 -0700 Subject: [PATCH] Add configuration option for archive_url This allows for specifying a custom archive URL as an alternative approach to Custom Pages. --- docs/src/config_updates.md | 10 ++++++++++ src/config.rs | 12 +++++++++++- src/main.rs | 8 ++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/docs/src/config_updates.md b/docs/src/config_updates.md index f010cc6e..49aabfd9 100644 --- a/docs/src/config_updates.md +++ b/docs/src/config_updates.md @@ -24,3 +24,13 @@ is set to `false`. auto_update = true auto_update_interval_hours = 24 +### archive_url + +URL for the location of tldr pages archive. Default is the main `thdr.sh` +archive location. + + [updates] + auto_update = true + auto_update_interval_hours = 24 + archive_url = https://tldr.infra.local/assets/tldr.zip + diff --git a/src/config.rs b/src/config.rs index 98d4e465..4e4da920 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,7 @@ use crate::types::PathSource; pub const CONFIG_FILE_NAME: &str = "config.toml"; pub const MAX_CACHE_AGE: Duration = Duration::from_secs(2_592_000); // 30 days const DEFAULT_UPDATE_INTERVAL_HOURS: u64 = MAX_CACHE_AGE.as_secs() / 3600; // 30 days +const ARCHIVE_URL: &str = "https://tldr.sh/assets/tldr.zip"; fn default_underline() -> bool { false @@ -166,12 +167,18 @@ const fn default_auto_update_interval_hours() -> u64 { DEFAULT_UPDATE_INTERVAL_HOURS } +fn default_archive_url() -> String { + ARCHIVE_URL.to_owned() +} + #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] struct RawUpdatesConfig { #[serde(default)] pub auto_update: bool, #[serde(default = "default_auto_update_interval_hours")] pub auto_update_interval_hours: u64, + #[serde(default = "default_archive_url")] + pub archive_url: String, } impl Default for RawUpdatesConfig { @@ -179,6 +186,7 @@ impl Default for RawUpdatesConfig { Self { auto_update: false, auto_update_interval_hours: DEFAULT_UPDATE_INTERVAL_HOURS, + archive_url: default_archive_url(), } } } @@ -190,6 +198,7 @@ impl From for UpdatesConfig { auto_update_interval: Duration::from_secs( raw_updates_config.auto_update_interval_hours * 3600, ), + archive_url: raw_updates_config.archive_url, } } } @@ -252,10 +261,11 @@ pub struct DisplayConfig { pub use_pager: bool, } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct UpdatesConfig { pub auto_update: bool, pub auto_update_interval: Duration, + pub archive_url: String, } #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/src/main.rs b/src/main.rs index 08afe204..5235a587 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,6 @@ const APP_INFO: AppInfo = AppInfo { name: NAME, author: NAME, }; -const ARCHIVE_URL: &str = "https://tldr.sh/assets/tldr.zip"; /// The cache should be updated if it was explicitly requested, /// or if an automatic update is due and allowed. @@ -133,8 +132,8 @@ fn clear_cache(cache: &Cache, quietly: bool, enable_styles: bool) { } /// Update the cache -fn update_cache(cache: &Cache, quietly: bool, enable_styles: bool) { - cache.update(ARCHIVE_URL).unwrap_or_else(|e| { +fn update_cache(cache: &Cache, archive_url: &str, quietly: bool, enable_styles: bool) { + cache.update(archive_url).unwrap_or_else(|e| { print_error(enable_styles, &e.context("Could not update cache")); process::exit(1); }); @@ -307,7 +306,8 @@ fn main() { // Cache update, pass through let cache_updated = if should_update_cache(&cache, &args, &config) { - update_cache(&cache, args.quiet, enable_styles); + let archive_url = config.updates.archive_url.as_str(); + update_cache(&cache, archive_url, args.quiet, enable_styles); true } else { false