Add proxy setting for install command. Fix #408
This commit is contained in:
parent
99f048fcd7
commit
8f8d5f2f1c
16
src/main.rs
16
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)
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -264,12 +264,13 @@ impl super::PackageManager for DefaultPackageManager {
|
|||
&self,
|
||||
name: &str,
|
||||
allow_external: bool,
|
||||
proxy: Option<String>,
|
||||
) -> Result<InstallResult, Box<dyn Error>> {
|
||||
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<String>,
|
||||
) -> Result<InstallResult, Box<dyn Error>> {
|
||||
// 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
|
||||
);
|
||||
|
|
|
@ -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<TempDir, Box<dyn Error>> {
|
||||
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());
|
||||
}
|
||||
}
|
|
@ -34,11 +34,13 @@ pub trait PackageManager {
|
|||
&self,
|
||||
name: &str,
|
||||
allow_external: bool,
|
||||
proxy: Option<String>,
|
||||
) -> Result<InstallResult, Box<dyn Error>>;
|
||||
fn install_package_from_repo(
|
||||
&self,
|
||||
name: &str,
|
||||
repo_url: &str,
|
||||
proxy: Option<String>,
|
||||
) -> Result<InstallResult, Box<dyn Error>>;
|
||||
|
||||
fn remove_package(&self, name: &str) -> Result<RemoveResult, Box<dyn Error>>;
|
||||
|
@ -47,7 +49,7 @@ pub trait PackageManager {
|
|||
}
|
||||
|
||||
pub trait PackageResolver {
|
||||
fn clone_repo_to_temp(&self, repo_url: &str) -> Result<TempDir, Box<dyn Error>>;
|
||||
fn clone_repo_to_temp(&self, repo_url: &str, proxy: Option<String>) -> Result<TempDir, Box<dyn Error>>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||
|
|
|
@ -13,13 +13,22 @@ impl ZipPackageResolver {
|
|||
}
|
||||
|
||||
impl super::PackageResolver for ZipPackageResolver {
|
||||
fn clone_repo_to_temp(&self, repo_url: &str) -> Result<TempDir, Box<dyn Error>> {
|
||||
fn clone_repo_to_temp(&self, repo_url: &str, proxy: Option<String>) -> Result<TempDir, Box<dyn Error>> {
|
||||
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());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user