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 super::{PACKAGE_SOURCE_FILE, PackageSource};
use super::{PackageSource, PACKAGE_SOURCE_FILE};
// TODO: test
pub fn copy_dir_without_dot_files(source_dir: &Path, inside_dir: &Path) -> Result<()> {
@ -57,4 +57,4 @@ pub fn create_package_source_file(specifier: &PackageSpecifier, target_dir: &Pat
let yaml = serde_yaml::to_string(&source)?;
std::fs::write(target_dir.join(PACKAGE_SOURCE_FILE), yaml)?;
Ok(())
}
}

View File

@ -34,7 +34,11 @@ pub use archive::{ArchivedPackage, Archiver, SaveOptions, StoredPackage};
pub use package::Package;
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 !package.use_native_git {
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()))
} else {
// 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

@ -20,4 +20,4 @@ macro_rules! error_eprintln {
eprintln!($($tts)*);
log::error!($($tts)*);
}
}
}

View File

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

View File

@ -48,4 +48,4 @@ impl std::fmt::Debug for dyn Package {
}
}
pub use default::DefaultPackage;
pub use default::DefaultPackage;

View File

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

View File

@ -17,11 +17,9 @@
* 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 crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier};
use anyhow::Result;
pub struct GitHubPackageProvider {
repo_author: String,

View File

@ -17,11 +17,9 @@
* 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 crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier};
use anyhow::Result;
pub struct GitLabPackageProvider {
repo_author: String,

View File

@ -21,10 +21,10 @@ use anyhow::Result;
use crate::Package;
pub(crate) mod hub;
pub(crate) mod git;
pub(crate) mod github;
pub(crate) mod gitlab;
pub(crate) mod hub;
#[derive(Debug, Default)]
pub struct PackageSpecifier {
@ -48,4 +48,4 @@ pub trait PackageProvider {
#[derive(Debug, Default)]
pub struct ProviderOptions {
pub force_index_update: bool,
}
}

View File

@ -17,16 +17,20 @@
* 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::path::Path;
use sha2::{Sha256, Digest};
pub fn download_and_extract_zip(url: &str, dest_dir: &Path) -> Result<()> {
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")?;
if let Some(sha256) = sha256 {
info_println!("validating sha256 signature...");

View File

@ -23,7 +23,9 @@ use regex::Regex;
use reqwest::StatusCode;
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)]
@ -36,7 +38,7 @@ pub fn extract_github_url_parts(url: &str) -> Option<GitHubParts> {
let captures = GITHUB_REGEX.captures(url)?;
let author = captures.name("author")?;
let name = captures.name("name")?;
Some(GitHubParts {
author: author.as_str().to_string(),
name: name.as_str().to_string(),
@ -49,14 +51,17 @@ pub struct ResolvedRepoScheme {
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 check_repo_with_branch(&parts, force_branch)? {
return Ok(Some(ResolvedRepoScheme {
author: parts.author,
name: parts.name,
branch: force_branch.to_string(),
}))
}));
}
} else {
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(),
}));
}
}
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 {
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)]
@ -99,21 +106,32 @@ mod tests {
#[test]
fn test_extract_github_url_parts() {
assert_eq!(extract_github_url_parts("https://github.com/federico-terzi/espanso").unwrap(), GitHubParts {
author: "federico-terzi".to_string(),
name: "espanso".to_string(),
});
assert_eq!(
extract_github_url_parts("https://github.com/federico-terzi/espanso").unwrap(),
GitHubParts {
author: "federico-terzi".to_string(),
name: "espanso".to_string(),
}
);
assert_eq!(extract_github_url_parts("https://github.com/federico-terzi/espanso.git").unwrap(), GitHubParts {
author: "federico-terzi".to_string(),
name: "espanso".to_string(),
});
assert_eq!(
extract_github_url_parts("https://github.com/federico-terzi/espanso.git").unwrap(),
GitHubParts {
author: "federico-terzi".to_string(),
name: "espanso".to_string(),
}
);
assert_eq!(extract_github_url_parts("git@github.com:federico-terzi/espanso.git").unwrap(), GitHubParts {
author: "federico-terzi".to_string(),
name: "espanso".to_string(),
});
assert_eq!(
extract_github_url_parts("git@github.com:federico-terzi/espanso.git").unwrap(),
GitHubParts {
author: "federico-terzi".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/>.
*/
use anyhow::Result;
use lazy_static::lazy_static;
use regex::Regex;
use anyhow::Result;
use reqwest::StatusCode;
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)]
@ -36,7 +38,7 @@ pub fn extract_gitlab_url_parts(url: &str) -> Option<GitLabParts> {
let captures = GITLAB_REGEX.captures(url)?;
let author = captures.name("author")?;
let name = captures.name("name")?;
Some(GitLabParts {
author: author.as_str().to_string(),
name: name.as_str().to_string(),
@ -49,14 +51,17 @@ pub struct ResolvedRepoScheme {
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 check_repo_with_branch(&parts, force_branch)? {
return Ok(Some(ResolvedRepoScheme {
author: parts.author,
name: parts.name,
branch: force_branch.to_string(),
}))
}));
}
} else {
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 {
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)]
@ -98,21 +106,33 @@ mod tests {
#[test]
fn test_extract_gitlab_url_parts() {
assert_eq!(extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").unwrap(), GitLabParts {
author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(),
});
assert_eq!(
extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").unwrap(),
GitLabParts {
author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(),
}
);
assert_eq!(extract_gitlab_url_parts("git@gitlab.com:federicoterzi/espanso-test-package.git").unwrap(), GitLabParts {
author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(),
});
assert_eq!(
extract_gitlab_url_parts("git@gitlab.com:federicoterzi/espanso-test-package.git").unwrap(),
GitLabParts {
author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(),
}
);
assert_eq!(extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package.git").unwrap(), GitLabParts {
author: "federicoterzi".to_string(),
name: "espanso-test-package".to_string(),
});
assert_eq!(
extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package.git")
.unwrap(),
GitLabParts {
author: "federicoterzi".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()
);
}
}

View File

@ -20,4 +20,4 @@
pub(crate) mod download;
pub(crate) mod git;
pub(crate) mod github;
pub(crate) mod gitlab;
pub(crate) mod gitlab;