diff --git a/src/main.rs b/src/main.rs index 6158d38..f35abe6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,9 @@ fn main() { .help("(Optional) Link to GitHub repository") .required(false) .default_value("hub"), - ); + ) + .arg(Arg::with_name("proxy").help("Use a proxy, should be used as --proxy=https://proxy:1234") + .required(false).long("proxy").takes_value(true)); let uninstall_subcommand = SubCommand::with_name("uninstall") .about("Remove an installed package. Equivalent to 'espanso package uninstall'") @@ -1096,6 +1098,14 @@ fn install_main(_config_set: ConfigSet, matches: &ArgMatches) { repository = repository.trim_end_matches(".git") } + let proxy = match matches.value_of("proxy") { + Some(proxy) => { + println!("Using proxy: {}", proxy); + Some(proxy.to_string()) + } + None => {None} + }; + let package_resolver = Box::new(ZipPackageResolver::new()); let allow_external: bool = if matches.is_present("external") { @@ -1131,7 +1141,7 @@ fn install_main(_config_set: ConfigSet, matches: &ArgMatches) { println!("Using cached package index, run 'espanso package refresh' to update it.") } - package_manager.install_package(package_name, allow_external) + package_manager.install_package(package_name, allow_external, proxy) } else { // Make sure the repo is a valid github url lazy_static! { @@ -1147,7 +1157,7 @@ fn install_main(_config_set: ConfigSet, matches: &ArgMatches) { if !allow_external { Ok(InstallResult::BlockedExternalPackage(repository.to_owned())) } else { - package_manager.install_package_from_repo(package_name, repository) + package_manager.install_package_from_repo(package_name, repository, proxy) } }; diff --git a/src/package/default.rs b/src/package/default.rs index e2cc3c9..69182e3 100644 --- a/src/package/default.rs +++ b/src/package/default.rs @@ -264,12 +264,13 @@ impl super::PackageManager for DefaultPackageManager { &self, name: &str, allow_external: bool, + proxy: Option, ) -> Result> { let package = self.get_package(name); match package { Some(package) => { if package.is_core || allow_external { - self.install_package_from_repo(name, &package.repo) + self.install_package_from_repo(name, &package.repo, proxy) } else { Ok(BlockedExternalPackage(package.original_repo)) } @@ -282,6 +283,7 @@ impl super::PackageManager for DefaultPackageManager { &self, name: &str, repo_url: &str, + proxy: Option, ) -> Result> { // Check if package is already installed let packages = self.list_local_packages_names(); @@ -294,7 +296,7 @@ impl super::PackageManager for DefaultPackageManager { .package_resolver .as_ref() .unwrap() - .clone_repo_to_temp(repo_url)?; + .clone_repo_to_temp(repo_url, proxy)?; let temp_package_dir = temp_dir.path().join(name); if !temp_package_dir.exists() { @@ -532,7 +534,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("doesnotexist", false) + .install_package("doesnotexist", false, None) .unwrap(), NotFoundInIndex ); @@ -548,7 +550,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("italian-accents", false) + .install_package("italian-accents", false, None) .unwrap(), AlreadyInstalled ); @@ -563,7 +565,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("dummy-package", false) + .install_package("dummy-package", false, None) .unwrap(), Installed ); @@ -589,7 +591,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("not-existing", false) + .install_package("not-existing", false, None) .unwrap(), NotFoundInRepo ); @@ -604,7 +606,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("dummy-package2", false) + .install_package("dummy-package2", false, None) .unwrap(), MissingPackageVersion ); @@ -619,7 +621,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("dummy-package3", false) + .install_package("dummy-package3", false, None) .unwrap(), UnableToParsePackageInfo ); @@ -634,7 +636,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("dummy-package4", false) + .install_package("dummy-package4", false, None) .unwrap(), UnableToParsePackageInfo ); @@ -649,7 +651,7 @@ mod tests { assert_eq!( temp.package_manager - .install_package("dummy-package", false) + .install_package("dummy-package", false, None) .unwrap(), Installed ); diff --git a/src/package/git.rs b/src/package/git.rs deleted file mode 100644 index f8d132b..0000000 --- a/src/package/git.rs +++ /dev/null @@ -1,33 +0,0 @@ -use tempfile::TempDir; -use std::error::Error; -use git2::Repository; -use super::PackageResolver; - -pub struct GitPackageResolver; - -impl GitPackageResolver { - pub fn new() -> GitPackageResolver { - return GitPackageResolver{}; - } -} - -impl super::PackageResolver for GitPackageResolver { - fn clone_repo_to_temp(&self, repo_url: &str) -> Result> { - let temp_dir = TempDir::new()?; - let _repo = Repository::clone(repo_url, temp_dir.path())?; - Ok(temp_dir) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use tempfile::{TempDir, NamedTempFile}; - - #[test] - fn test_clone_temp_repository() { - let resolver = GitPackageResolver::new(); - let cloned_dir = resolver.clone_repo_to_temp("https://github.com/federico-terzi/espanso-hub-core").unwrap(); - assert!(cloned_dir.path().join("LICENSE").exists()); - } -} \ No newline at end of file diff --git a/src/package/mod.rs b/src/package/mod.rs index 1d99210..8906c8c 100644 --- a/src/package/mod.rs +++ b/src/package/mod.rs @@ -34,11 +34,13 @@ pub trait PackageManager { &self, name: &str, allow_external: bool, + proxy: Option, ) -> Result>; fn install_package_from_repo( &self, name: &str, repo_url: &str, + proxy: Option, ) -> Result>; fn remove_package(&self, name: &str) -> Result>; @@ -47,7 +49,7 @@ pub trait PackageManager { } pub trait PackageResolver { - fn clone_repo_to_temp(&self, repo_url: &str) -> Result>; + fn clone_repo_to_temp(&self, repo_url: &str, proxy: Option) -> Result>; } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] diff --git a/src/package/zip.rs b/src/package/zip.rs index 52fcf9d..8b90ab4 100644 --- a/src/package/zip.rs +++ b/src/package/zip.rs @@ -13,13 +13,22 @@ impl ZipPackageResolver { } impl super::PackageResolver for ZipPackageResolver { - fn clone_repo_to_temp(&self, repo_url: &str) -> Result> { + fn clone_repo_to_temp(&self, repo_url: &str, proxy: Option) -> Result> { let temp_dir = TempDir::new()?; let zip_url = repo_url.to_owned() + "/archive/master.zip"; + let mut client = reqwest::Client::builder(); + + if let Some(proxy) = proxy { + let proxy = reqwest::Proxy::https(&proxy).expect("unable to setup https proxy"); + client = client.proxy(proxy); + }; + + let client = client.build().expect("unable to create http client"); + // Download the archive from GitHub - let mut response = reqwest::get(&zip_url)?; + let mut response = client.get(&zip_url).send()?; // Extract zip file let mut buffer = Vec::new(); @@ -90,7 +99,7 @@ mod tests { fn test_clone_temp_repository() { let resolver = ZipPackageResolver::new(); let cloned_dir = resolver - .clone_repo_to_temp("https://github.com/federico-terzi/espanso-hub-core") + .clone_repo_to_temp("https://github.com/federico-terzi/espanso-hub-core", None) .unwrap(); assert!(cloned_dir.path().join("LICENSE").exists()); }