From a5958342101460ec021b40c1fd132e78c8e17c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20St=C3=A9phenne?= Date: Sat, 9 Nov 2024 11:47:16 -0500 Subject: [PATCH] Feature: You can now specify a package version --- src/commands/pen_add.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/commands/pen_add.rs b/src/commands/pen_add.rs index 208bb51..e933b93 100644 --- a/src/commands/pen_add.rs +++ b/src/commands/pen_add.rs @@ -8,19 +8,32 @@ use crate::{ utils::abort, PACKAGES_DIR, }; +use semver::Version; use serde::Deserialize; -use ureq::Response; use zip::ZipArchive; -// TODO: use the version -pub fn add(name: &String, _version: Option<&String>) { +pub fn add(name: &String, version: Option<&String>) { let projet_path = find_project(); let mut config = read_config(&projet_path); - let response: Response = ureq::get(&format!("https://pypi.org/pypi/{name}/json")) + let info_url = match version { + Some(version_str) => { + let version = match Version::parse(version_str) { + Ok(version) => version, + Err(e) => abort("Provided version is invalid.", Some(&e)), + }; + format!("https://pypi.org/pypi/{}/{}/json", name, version) + } + None => format!("https://pypi.org/pypi/{}/json", name), + }; + + let response = match ureq::get(&info_url) .set("Accept", "application/json") .call() - .unwrap(); + { + Ok(res) => res, + Err(e) => abort("Couldn't request PyPi.", Some(&e)), + }; if response.status() != 200 { abort( @@ -39,7 +52,13 @@ pub fn add(name: &String, _version: Option<&String>) { }; println!("Downloading: {} v{}", json.info.name, json.info.version); - let response: Response = ureq::get(&json.urls[0].url).call().unwrap(); + let response = match ureq::get(&json.urls[0].url) + .set("Accept", "application/json") + .call() + { + Ok(res) => res, + Err(e) => abort("Couldn't request PyPi.", Some(&e)), + }; if response.status() != 200 { abort(