Skip to content

Commit

Permalink
check package extension
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani committed Sep 13, 2024
1 parent 22efb42 commit c8a01bc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn downgrade(package: String, version: Option<String>) -> Result<String> {

// Should check if present in local cache
let p = Package::parse(pkg[0])?;
let url = p.get_url();
let url = p.get_url()?;

let err = process::Command::new("pacman").args(["-U", &url]).exec();

Expand Down
39 changes: 32 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::{DateTime, Utc};
use core::panic;
use std::{
fmt, fs,
io::{Read, Write},
Expand All @@ -12,7 +13,8 @@ use xz::read::XzDecoder;

const ARCH_URL: &str = "https://archive.archlinux.org";
const INDEX_PATH: &str = "/packages/.all/index.0.xz";
const PKG_POSTFIX: &str = ".pkg.tar.zst";
const PKG_POSTFIX1: &str = ".pkg.tar.zst";
const PKG_POSTFIX2: &str = ".pkg.tar.xz";
const CACHE_DURATION: i64 = 5; // 5 minutes

pub type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
Expand Down Expand Up @@ -150,13 +152,36 @@ impl Package {
)
}

pub fn get_url(&self) -> String {
format!(
"{ARCH_URL}/packages/{}/{}{}",
/// The postfix can be of two types, so we return a tuple of
/// urls and the caller check which is correct
pub fn get_url(&self) -> Result<String> {
let mut base = format!(
"{ARCH_URL}/packages/{}/{}",
self.name.chars().next().unwrap(),
self.full_name(),
PKG_POSTFIX
)
self.name(),
);

let name1 = format!("{}{}", self.full_name(), PKG_POSTFIX1);
let name2 = format!("{}{}", self.full_name(), PKG_POSTFIX2);

let res = match minreq::get(&base).send() {
Ok(res) => res,
Err(e) => panic!("Getting base packages: {e}"),
};

let res = res.as_str()?;

if res.contains(&name1) {
base.push('/');
base.push_str(&name1);
Ok(base)
} else if res.contains(&name2) {
base.push('/');
base.push_str(&name2);
Ok(base)
} else {
Err("Could not find package extension".into())
}
}

pub fn name(&self) -> &str {
Expand Down

0 comments on commit c8a01bc

Please sign in to comment.