Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration option for archive_source #337

Merged
merged 6 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 tldr pages archive. By default the pages are
niklasmohrin marked this conversation as resolved.
Show resolved Hide resolved
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!("{}{}", archive_source, "tldr.zip");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can be more cautious here and add the /. If is not present in archive_source, the archive_url will be unexpected, and if it was already present, having // does not change the behavior

Suggested change
let archive_url = format!("{}{}", archive_source, "tldr.zip");
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 @@ -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_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 @@ -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_source = config.updates.archive_source.as_str();
update_cache(&cache, archive_source, args.quiet, enable_styles);
true
} else {
false
Expand Down
Loading