Skip to content

Commit

Permalink
Update to 2021 Edition, bump dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jul 15, 2023
1 parent 116e5b9 commit 15f931f
Show file tree
Hide file tree
Showing 11 changed files with 647 additions and 220 deletions.
633 changes: 527 additions & 106 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
[package]
name = "server-wrapper"
version = "0.1.0"
authors = ["Gegy <gegy1000@gmail.com>"]
edition = "2018"
authors = ["Gegy <gegy.dev@gmail.com>"]
edition = "2021"

[profile.release]
strip = "symbols"

[dependencies]
tokio = { version = "1.0", features = ["full"] }
tokio = { version = "1.25", features = ["full"] }
reqwest = { version = "0.11", features = ["rustls-tls", "stream", "gzip", "json"], default-features = false }
futures = "0.3"

bytes = "1.0"
bytes = "1.4"

chrono = { version = "0.4", features = ["serde"] }

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "0.5"
toml = "0.7"

zip = "0.5"
zip = "0.6"
glob = "0.3"
sha1 = "0.6"
sha1 = "0.10"

thiserror = "1.0"
12 changes: 6 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ where
}

async fn write_config<T: Serialize>(path: &Path, config: &T) -> io::Result<()> {
let mut file = File::create(path).await?;
let string = toml::to_string(config).expect("malformed config");

let bytes = toml::to_vec(config).expect("malformed config");
file.write_all(&bytes).await?;
let mut file = File::create(path).await?;
file.write_all(string.as_bytes()).await?;

Ok(())
}

async fn read_config<T: DeserializeOwned>(path: &Path) -> io::Result<T> {
let mut file = File::open(path).await?;

let mut bytes = Vec::new();
file.read_to_end(&mut bytes).await?;
let mut string = String::new();
file.read_to_string(&mut string).await?;

Ok(toml::from_slice::<T>(&bytes).expect("malformed config"))
Ok(toml::from_str::<T>(&string).expect("malformed config"))
}
91 changes: 8 additions & 83 deletions src/config/destinations.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::collections::HashMap;
use std::io;
use std::path::PathBuf;

use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::Error;

use crate::source;
use crate::transform;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(transparent)]
Expand All @@ -23,92 +22,18 @@ pub struct Destination {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceSet {
#[serde(default = "Default::default")]
pub transform: Transform,
pub transform: Option<Transform>,
#[serde(flatten)]
pub sources: HashMap<String, Source>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Transform {
Direct,
Unzip { unzip: Vec<Pattern> },
}

impl Default for Transform {
fn default() -> Self {
Transform::Direct
}
}

impl Transform {
pub async fn apply(&self, file: source::File) -> io::Result<Option<source::File>> {
match self {
Transform::Direct => Ok(Some(file)),
Transform::Unzip { unzip } => transform::unzip(file, &unzip).await,
}
}
}

mod transform {
use std::io;
use std::io::Read;

use bytes::Bytes;
use zip::ZipArchive;

use super::*;

// TODO: potentially support loading multiple files + directories
pub async fn unzip(
file: source::File,
patterns: &[Pattern],
) -> io::Result<Option<source::File>> {
let patterns: Vec<Pattern> = patterns.iter().cloned().collect();

tokio::task::spawn_blocking(move || {
let cursor = io::Cursor::new(file.bytes.as_ref());
let mut zip = ZipArchive::new(cursor)?;

let jar_names: Vec<String> = zip
.file_names()
.filter(|path| matches_all(path, &patterns))
.map(|path| path.to_owned())
.collect();

for name in jar_names {
let mut file = zip.by_name(&name)?;
if file.is_file() {
let mut bytes = Vec::with_capacity(file.size() as usize);
file.read_to_end(&mut bytes)?;

let bytes = Bytes::from(bytes);
return Ok(Some(source::File { name, bytes }));
}
}

Ok(None)
})
.await
.unwrap()
}

fn matches_all(path: &str, patterns: &[Pattern]) -> bool {
let mut include = patterns.iter().filter(|pattern| !pattern.exclude);
let mut exclude = patterns.iter().filter(|pattern| pattern.exclude);

include.all(|pattern| pattern.glob.matches(path))
&& !exclude.any(|pattern| pattern.glob.matches(path))
}
}

#[derive(Debug, Clone)]
pub struct Pattern {
pub glob: glob::Pattern,
pub exclude: bool,
Unzip { unzip: Vec<transform::Pattern> },
}

impl Serialize for Pattern {
impl Serialize for transform::Pattern {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.exclude {
serializer.serialize_str(&format!("!{}", self.glob))
Expand All @@ -118,7 +43,7 @@ impl Serialize for Pattern {
}
}

impl<'de> Deserialize<'de> for Pattern {
impl<'de> Deserialize<'de> for transform::Pattern {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let mut pattern: &str = Deserialize::deserialize(deserializer)?;
let mut exclude = false;
Expand All @@ -128,7 +53,7 @@ impl<'de> Deserialize<'de> for Pattern {
}

match glob::Pattern::new(pattern) {
Ok(glob) => Ok(Pattern { glob, exclude }),
Ok(glob) => Ok(transform::Pattern { glob, exclude }),
Err(err) => Err(D::Error::custom(err)),
}
}
Expand Down Expand Up @@ -169,7 +94,7 @@ impl Default for Destinations {

let mut source_sets = HashMap::new();
source_sets.insert("jars".to_owned(), SourceSet {
transform: Transform::Direct,
transform: None,
sources,
});

Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ pub use config::Config;
use executor::Executor;
use status::StatusWriter;

pub use crate::transform::Transform;

mod cache;
mod config;
mod executor;
mod source;
mod status;
mod transform;

const CACHE_ROOT: &str = "wrapper_cache";

Expand Down Expand Up @@ -172,9 +175,13 @@ async fn prepare_destination(
let stale_files = cache.drop_stale(cache_keys).await?;

for (_, source_set) in &destination.sources {
let transform = match &source_set.transform {
None => Transform::Direct,
Some(config::Transform::Unzip { unzip }) => { Transform::Unzip { unzip: unzip.clone() }},
};
for (key, source) in &source_set.sources {
let cache_entry = cache.entry(key.clone());
match source::load(ctx, cache_entry, source, &source_set.transform).await {
match source::load(ctx, cache_entry, source, &transform).await {
Ok(reference) => cache_files.push((key.clone(), reference)),
Err(err) => {
eprintln!("failed to load {}: {:?}! excluding.", key, err);
Expand Down
5 changes: 3 additions & 2 deletions src/source.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use bytes::Bytes;

use crate::{Error, Result};
use crate::cache;
use crate::config::{self, Source};
use crate::Context;
use crate::{Error, Result};
use crate::transform::Transform;

pub mod github;
pub mod http;
Expand All @@ -14,7 +15,7 @@ pub async fn load<'a>(
ctx: &Context,
cache: cache::Entry<'a>,
source: &config::Source,
transform: &config::Transform,
transform: &Transform,
) -> Result<cache::Reference> {
match source {
Source::GitHubArtifacts {
Expand Down
8 changes: 6 additions & 2 deletions src/source/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::sync::Arc;

use serde::Deserialize;

use crate::{cache, config, source, Error, Result};
use crate::{cache, Error, Result, source, Transform};

pub async fn load<'a>(
client: &Client,
cache: cache::Entry<'a>,
owner: &str,
repository: &str,
filter: Filter,
transform: &config::Transform,
transform: &Transform,
) -> Result<cache::Reference> {
let latest_artifact = get_latest_artifact(client, owner, repository, filter).await?;

Expand Down Expand Up @@ -190,12 +190,14 @@ impl Client {
}

#[derive(Deserialize, Debug)]
#[allow(unused)]
struct WorkflowRunsResponse {
total_count: usize,
workflow_runs: Vec<WorkflowRun>,
}

#[derive(Deserialize, Debug)]
#[allow(unused)]
struct WorkflowRun {
id: usize,
name: String,
Expand All @@ -207,12 +209,14 @@ struct WorkflowRun {
}

#[derive(Deserialize, Debug)]
#[allow(unused)]
struct ArtifactsResponse {
total_count: usize,
artifacts: Vec<Artifact>,
}

#[derive(Deserialize, Debug)]
#[allow(unused)]
struct Artifact {
id: usize,
node_id: String,
Expand Down
4 changes: 2 additions & 2 deletions src/source/http.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{cache, config, source, Error, Result};
use crate::{cache, source, Error, Result, Transform};

pub async fn load<'a>(
client: &reqwest::Client,
cache: cache::Entry<'a>,
url: &str,
transform: &config::Transform,
transform: &Transform,
) -> Result<cache::Reference> {
let response = client.get(url).send().await?;

Expand Down
12 changes: 5 additions & 7 deletions src/source/modrinth.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use crate::cache;
use crate::config;
use crate::source;
use crate::Error;
use crate::Result;
use std::sync::Arc;

use chrono::DateTime;
use chrono::Utc;
use serde::Deserialize;
use std::sync::Arc;

use crate::{cache, Error, Result, source, Transform};

pub async fn load<'a>(
client: &Client,
cache: cache::Entry<'a>,
project_id: &str,
game_version: &Option<String>,
transform: &config::Transform,
transform: &Transform,
) -> Result<cache::Reference> {
let latest_version = resolve_version(client, project_id, game_version).await?;
if let Some((hash, url, name)) = latest_version {
Expand Down
10 changes: 6 additions & 4 deletions src/source/path.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use std::path::PathBuf;

use bytes::Bytes;
use sha1::{Digest, Sha1};
use tokio::fs;

use crate::{cache, config, source, Error, Result};
use crate::{cache, Error, Result, source, Transform};

pub async fn load<'a>(
cache: cache::Entry<'a>,
path: &PathBuf,
transform: &config::Transform,
transform: &Transform,
) -> Result<cache::Reference> {
let bytes = fs::read(&path).await?;

let mut hasher = sha1::Sha1::new();
let mut hasher = Sha1::new();
hasher.update(&bytes);

let hash = hasher.digest().bytes();
let mut hash = [0u8; 20];
hash.copy_from_slice(&hasher.finalize());

use cache::UpdateResult::*;
match cache.try_update(cache::Token::Sha1(hash)) {
Expand Down
Loading

0 comments on commit 15f931f

Please sign in to comment.