Skip to content

Commit

Permalink
Add configuration option for archive_source (#337)
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.

Co-authored-by: Niklas Mohrin <[email protected]>
  • Loading branch information
mipedja and niklasmohrin authored Jan 12, 2025
1 parent 09d4411 commit 120e2a9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
8 changes: 8 additions & 0 deletions docs/src/config_updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ is set to `false`.
auto_update = true
auto_update_interval_hours = 24

### archive_source

URL for the location of the tldr pages archive. By default the pages are
fetched from the latest `tldr-pages/tldr` GitHub release.

[updates]
archive_source = https://my-company.example.com/tldr/

6 changes: 4 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ impl Cache {
}

/// Update the pages cache from the specified URL.
pub fn update(&self, archive_url: &str) -> Result<()> {
pub fn update(&self, archive_source: &str) -> Result<()> {
self.ensure_cache_dir_exists()?;

let archive_url = format!("{}/tldr.zip", archive_source);

// First, download the compressed data
let bytes: Vec<u8> = Self::download(archive_url)?;
let bytes: Vec<u8> = Self::download(&archive_url)?;

// Decompress the response body into an `Archive`
let mut archive = ZipArchive::new(Cursor::new(bytes))
Expand Down
11 changes: 10 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,26 @@ const fn default_auto_update_interval_hours() -> u64 {
DEFAULT_UPDATE_INTERVAL_HOURS
}

fn default_archive_source() -> String {
"https://github.com/tldr-pages/tldr/releases/latest/download/".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_source")]
pub archive_source: String,
}

impl Default for RawUpdatesConfig {
fn default() -> Self {
Self {
auto_update: false,
auto_update_interval_hours: DEFAULT_UPDATE_INTERVAL_HOURS,
archive_source: default_archive_source(),
}
}
}
Expand All @@ -190,6 +197,7 @@ impl From<RawUpdatesConfig> for UpdatesConfig {
auto_update_interval: Duration::from_secs(
raw_updates_config.auto_update_interval_hours * 3600,
),
archive_source: raw_updates_config.archive_source,
}
}
}
Expand Down Expand Up @@ -252,10 +260,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_source: 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 @@ -64,7 +64,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 @@ -136,8 +135,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_source: &str, quietly: bool, enable_styles: bool) {
cache.update(archive_source).unwrap_or_else(|e| {
print_error(enable_styles, &e.context("Could not update cache"));
process::exit(1);
});
Expand Down Expand Up @@ -305,7 +304,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_source = config.updates.archive_source.as_str();
update_cache(&cache, archive_source, args.quiet, enable_styles);
true
} else {
false
Expand Down

0 comments on commit 120e2a9

Please sign in to comment.