style(package): fix formatting

This commit is contained in:
Federico Terzi 2021-10-06 18:40:16 +02:00
parent 308d1848be
commit 3fd8bfccab
13 changed files with 107 additions and 70 deletions

View File

@ -24,7 +24,7 @@ use fs_extra::dir::CopyOptions;
use crate::PackageSpecifier; use crate::PackageSpecifier;
use super::{PACKAGE_SOURCE_FILE, PackageSource}; use super::{PackageSource, PACKAGE_SOURCE_FILE};
// TODO: test // TODO: test
pub fn copy_dir_without_dot_files(source_dir: &Path, inside_dir: &Path) -> Result<()> { pub fn copy_dir_without_dot_files(source_dir: &Path, inside_dir: &Path) -> Result<()> {

View File

@ -34,7 +34,11 @@ pub use archive::{ArchivedPackage, Archiver, SaveOptions, StoredPackage};
pub use package::Package; pub use package::Package;
pub use provider::{PackageProvider, PackageSpecifier, ProviderOptions}; pub use provider::{PackageProvider, PackageSpecifier, ProviderOptions};
pub fn get_provider(package: &PackageSpecifier, runtime_dir: &Path, options: &ProviderOptions) -> Result<Box<dyn PackageProvider>> { pub fn get_provider(
package: &PackageSpecifier,
runtime_dir: &Path,
options: &ProviderOptions,
) -> Result<Box<dyn PackageProvider>> {
if let Some(git_repo_url) = package.git_repo_url.as_deref() { if let Some(git_repo_url) = package.git_repo_url.as_deref() {
if !package.use_native_git { if !package.use_native_git {
let matches_known_hosts = let matches_known_hosts =
@ -81,7 +85,10 @@ pub fn get_provider(package: &PackageSpecifier, runtime_dir: &Path, options: &Pr
Ok(Box::new(provider::git::GitPackageProvider::new())) Ok(Box::new(provider::git::GitPackageProvider::new()))
} else { } else {
// Download from the official espanso hub // Download from the official espanso hub
Ok(Box::new(provider::hub::EspansoHubPackageProvider::new(runtime_dir, options.force_index_update))) Ok(Box::new(provider::hub::EspansoHubPackageProvider::new(
runtime_dir,
options.force_index_update,
)))
} }
} }

View File

@ -21,7 +21,7 @@ use std::path::PathBuf;
use tempdir::TempDir; use tempdir::TempDir;
use crate::{Package, manifest::Manifest}; use crate::{manifest::Manifest, Package};
#[allow(dead_code)] #[allow(dead_code)]
pub struct DefaultPackage { pub struct DefaultPackage {
@ -34,11 +34,7 @@ pub struct DefaultPackage {
} }
impl DefaultPackage { impl DefaultPackage {
pub fn new( pub fn new(manifest: Manifest, temp_dir: TempDir, location: PathBuf) -> Self {
manifest: Manifest,
temp_dir: TempDir,
location: PathBuf,
) -> Self {
Self { Self {
manifest, manifest,
temp_dir, temp_dir,

View File

@ -17,14 +17,10 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::{ use super::PackageProvider;
package::DefaultPackage, use crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier};
resolver::{resolve_package},
Package, PackageSpecifier,
};
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use std::{path::Path, process::Command}; use std::{path::Path, process::Command};
use super::PackageProvider;
pub struct GitPackageProvider {} pub struct GitPackageProvider {}

View File

@ -17,11 +17,9 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::{
package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier,
};
use anyhow::{Result};
use super::PackageProvider; use super::PackageProvider;
use crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier};
use anyhow::Result;
pub struct GitHubPackageProvider { pub struct GitHubPackageProvider {
repo_author: String, repo_author: String,

View File

@ -17,11 +17,9 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::{
package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier,
};
use anyhow::{Result};
use super::PackageProvider; use super::PackageProvider;
use crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier};
use anyhow::Result;
pub struct GitLabPackageProvider { pub struct GitLabPackageProvider {
repo_author: String, repo_author: String,

View File

@ -21,10 +21,10 @@ use anyhow::Result;
use crate::Package; use crate::Package;
pub(crate) mod hub;
pub(crate) mod git; pub(crate) mod git;
pub(crate) mod github; pub(crate) mod github;
pub(crate) mod gitlab; pub(crate) mod gitlab;
pub(crate) mod hub;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct PackageSpecifier { pub struct PackageSpecifier {

View File

@ -17,16 +17,20 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use anyhow::{Context, Result, bail}; use anyhow::{bail, Context, Result};
use sha2::{Digest, Sha256};
use std::io::{copy, Cursor}; use std::io::{copy, Cursor};
use std::path::Path; use std::path::Path;
use sha2::{Sha256, Digest};
pub fn download_and_extract_zip(url: &str, dest_dir: &Path) -> Result<()> { pub fn download_and_extract_zip(url: &str, dest_dir: &Path) -> Result<()> {
download_and_extract_zip_verify_sha256(url, dest_dir, None) download_and_extract_zip_verify_sha256(url, dest_dir, None)
} }
pub fn download_and_extract_zip_verify_sha256(url: &str, dest_dir: &Path, sha256: Option<&str>) -> Result<()> { pub fn download_and_extract_zip_verify_sha256(
url: &str,
dest_dir: &Path,
sha256: Option<&str>,
) -> Result<()> {
let data = download(url).context("error downloading archive")?; let data = download(url).context("error downloading archive")?;
if let Some(sha256) = sha256 { if let Some(sha256) = sha256 {
info_println!("validating sha256 signature..."); info_println!("validating sha256 signature...");

View File

@ -23,7 +23,9 @@ use regex::Regex;
use reqwest::StatusCode; use reqwest::StatusCode;
lazy_static! { lazy_static! {
static ref GITHUB_REGEX: Regex = Regex::new(r"(https://github.com/|git@github.com:)(?P<author>.*?)/(?P<name>.*?)(\.|$)").unwrap(); static ref GITHUB_REGEX: Regex =
Regex::new(r"(https://github.com/|git@github.com:)(?P<author>.*?)/(?P<name>.*?)(\.|$)")
.unwrap();
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -49,14 +51,17 @@ pub struct ResolvedRepoScheme {
pub branch: String, pub branch: String,
} }
pub fn resolve_repo_scheme(parts: GitHubParts, force_branch: Option<&str>) -> Result<Option<ResolvedRepoScheme>> { pub fn resolve_repo_scheme(
parts: GitHubParts,
force_branch: Option<&str>,
) -> Result<Option<ResolvedRepoScheme>> {
if let Some(force_branch) = force_branch { if let Some(force_branch) = force_branch {
if check_repo_with_branch(&parts, force_branch)? { if check_repo_with_branch(&parts, force_branch)? {
return Ok(Some(ResolvedRepoScheme { return Ok(Some(ResolvedRepoScheme {
author: parts.author, author: parts.author,
name: parts.name, name: parts.name,
branch: force_branch.to_string(), branch: force_branch.to_string(),
})) }));
} }
} else { } else {
if check_repo_with_branch(&parts, "main")? { if check_repo_with_branch(&parts, "main")? {
@ -74,7 +79,6 @@ pub fn resolve_repo_scheme(parts: GitHubParts, force_branch: Option<&str>) -> Re
branch: "master".to_string(), branch: "master".to_string(),
})); }));
} }
} }
Ok(None) Ok(None)
@ -90,7 +94,10 @@ pub fn check_repo_with_branch(parts: &GitHubParts, branch: &str) -> Result<bool>
} }
fn generate_github_download_url(parts: &GitHubParts, branch: &str) -> String { fn generate_github_download_url(parts: &GitHubParts, branch: &str) -> String {
format!("https://github.com/{}/{}/archive/refs/heads/{}.zip", parts.author, parts.name, branch) format!(
"https://github.com/{}/{}/archive/refs/heads/{}.zip",
parts.author, parts.name, branch
)
} }
#[cfg(test)] #[cfg(test)]
@ -99,21 +106,32 @@ mod tests {
#[test] #[test]
fn test_extract_github_url_parts() { fn test_extract_github_url_parts() {
assert_eq!(extract_github_url_parts("https://github.com/federico-terzi/espanso").unwrap(), GitHubParts { assert_eq!(
extract_github_url_parts("https://github.com/federico-terzi/espanso").unwrap(),
GitHubParts {
author: "federico-terzi".to_string(), author: "federico-terzi".to_string(),
name: "espanso".to_string(), name: "espanso".to_string(),
}); }
);
assert_eq!(extract_github_url_parts("https://github.com/federico-terzi/espanso.git").unwrap(), GitHubParts { assert_eq!(
extract_github_url_parts("https://github.com/federico-terzi/espanso.git").unwrap(),
GitHubParts {
author: "federico-terzi".to_string(), author: "federico-terzi".to_string(),
name: "espanso".to_string(), name: "espanso".to_string(),
}); }
);
assert_eq!(extract_github_url_parts("git@github.com:federico-terzi/espanso.git").unwrap(), GitHubParts { assert_eq!(
extract_github_url_parts("git@github.com:federico-terzi/espanso.git").unwrap(),
GitHubParts {
author: "federico-terzi".to_string(), author: "federico-terzi".to_string(),
name: "espanso".to_string(), name: "espanso".to_string(),
}); }
);
assert!(extract_github_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").is_none()); assert!(
extract_github_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").is_none()
);
} }
} }

View File

@ -17,13 +17,15 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use anyhow::Result;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use anyhow::Result;
use reqwest::StatusCode; use reqwest::StatusCode;
lazy_static! { lazy_static! {
static ref GITLAB_REGEX: Regex = Regex::new(r"(https://gitlab.com/|git@gitlab.com:)(?P<author>.*?)/(?P<name>.*?)(/|\.|$)").unwrap(); static ref GITLAB_REGEX: Regex =
Regex::new(r"(https://gitlab.com/|git@gitlab.com:)(?P<author>.*?)/(?P<name>.*?)(/|\.|$)")
.unwrap();
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -49,14 +51,17 @@ pub struct ResolvedRepoScheme {
pub branch: String, pub branch: String,
} }
pub fn resolve_repo_scheme(parts: GitLabParts, force_branch: Option<&str>) -> Result<Option<ResolvedRepoScheme>> { pub fn resolve_repo_scheme(
parts: GitLabParts,
force_branch: Option<&str>,
) -> Result<Option<ResolvedRepoScheme>> {
if let Some(force_branch) = force_branch { if let Some(force_branch) = force_branch {
if check_repo_with_branch(&parts, force_branch)? { if check_repo_with_branch(&parts, force_branch)? {
return Ok(Some(ResolvedRepoScheme { return Ok(Some(ResolvedRepoScheme {
author: parts.author, author: parts.author,
name: parts.name, name: parts.name,
branch: force_branch.to_string(), branch: force_branch.to_string(),
})) }));
} }
} else { } else {
if check_repo_with_branch(&parts, "main")? { if check_repo_with_branch(&parts, "main")? {
@ -89,7 +94,10 @@ pub fn check_repo_with_branch(parts: &GitLabParts, branch: &str) -> Result<bool>
} }
fn generate_gitlab_download_url(parts: &GitLabParts, branch: &str) -> String { fn generate_gitlab_download_url(parts: &GitLabParts, branch: &str) -> String {
format!("https://gitlab.com/{}/{}/-/archive/{}/{}-{}.zip", parts.author, parts.name, branch, parts.name, branch) format!(
"https://gitlab.com/{}/{}/-/archive/{}/{}-{}.zip",
parts.author, parts.name, branch, parts.name, branch
)
} }
#[cfg(test)] #[cfg(test)]
@ -98,21 +106,33 @@ mod tests {
#[test] #[test]
fn test_extract_gitlab_url_parts() { fn test_extract_gitlab_url_parts() {
assert_eq!(extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").unwrap(), GitLabParts { assert_eq!(
extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").unwrap(),
GitLabParts {
author: "federicoterzi".to_string(), author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(), name: "espanso-test-package".to_string(),
}); }
);
assert_eq!(extract_gitlab_url_parts("git@gitlab.com:federicoterzi/espanso-test-package.git").unwrap(), GitLabParts { assert_eq!(
extract_gitlab_url_parts("git@gitlab.com:federicoterzi/espanso-test-package.git").unwrap(),
GitLabParts {
author: "federicoterzi".to_string(), author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(), name: "espanso-test-package".to_string(),
}); }
);
assert_eq!(extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package.git").unwrap(), GitLabParts { assert_eq!(
extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package.git")
.unwrap(),
GitLabParts {
author: "federicoterzi".to_string(), author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(), name: "espanso-test-package".to_string(),
}); }
);
assert!(extract_gitlab_url_parts("https://github.com/federicoterzi/espanso-test-package/").is_none()); assert!(
extract_gitlab_url_parts("https://github.com/federicoterzi/espanso-test-package/").is_none()
);
} }
} }