Skip to content

Commit

Permalink
Add configuration option for archive_url
Browse files Browse the repository at this point in the history
This allows for specifying a custom archive URL
as an alternative approach to Custom Pages.
  • Loading branch information
mipedja committed Aug 27, 2023
1 parent 1764cee commit 8daabd2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions docs/src/config_updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

12 changes: 11 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,19 +167,26 @@ 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 {
fn default() -> Self {
Self {
auto_update: false,
auto_update_interval_hours: DEFAULT_UPDATE_INTERVAL_HOURS,
archive_url: default_archive_url(),
}
}
}
Expand All @@ -190,6 +198,7 @@ impl From<RawUpdatesConfig> for UpdatesConfig {
auto_update_interval: Duration::from_secs(
raw_updates_config.auto_update_interval_hours * 3600,
),
archive_url: raw_updates_config.archive_url,
}
}
}
Expand Down Expand Up @@ -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)]
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8daabd2

Please sign in to comment.