Skip to content

Commit

Permalink
do some work
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jul 10, 2024
1 parent b47099e commit e4cb7bd
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 75 deletions.
36 changes: 36 additions & 0 deletions src/api/app/actions/build/addons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::{path::Path, sync::Arc};

use anyhow::{Context, Result};
use futures::{stream, StreamExt, TryStreamExt};

use crate::api::{app::App, models::Addon};

impl App {
pub async fn action_install_addons(self: Arc<Self>, base: &Path) -> Result<()> {
let addons = self.collect_addons().await?;
let base = Arc::new(base.to_owned());

const MAX_CONCURRENT_TASKS: usize = 20;

Check warning on line 13 in src/api/app/actions/build/addons.rs

View workflow job for this annotation

GitHub Actions / clippy

adding items after statements is confusing, since items exist from the start of the scope

warning: adding items after statements is confusing, since items exist from the start of the scope --> src/api/app/actions/build/addons.rs:13:9 | 13 | const MAX_CONCURRENT_TASKS: usize = 20; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements = note: `-W clippy::items-after-statements` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::items_after_statements)]`

stream::iter(addons).map(Ok).try_for_each_concurrent(
Some(MAX_CONCURRENT_TASKS),
move |addon| {
let app = self.clone();
let base = base.clone();
async move {
app.action_install_addon(&base, &addon).await
.with_context(|| format!("{addon:#?}"))
}
}
).await?;

Ok(())
}

pub async fn action_install_addon(self: Arc<Self>, base: &Path, addon: &Addon) -> Result<()> {
let steps = addon.resolve_steps(&self).await?;
let dir = base.join(addon.target.as_str());
self.execute_steps(&dir, &steps).await?;
Ok(())
}
}
45 changes: 7 additions & 38 deletions src/api/app/actions/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,16 @@ use anyhow::Result;

use crate::api::{app::App, models::{Addon, Environment}};

Check warning on line 6 in src/api/app/actions/build/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `Addon`, `Environment`

warning: unused imports: `Addon`, `Environment` --> src/api/app/actions/build/mod.rs:6:37 | 6 | use crate::api::{app::App, models::{Addon, Environment}}; | ^^^^^ ^^^^^^^^^^^

impl App {
pub async fn action_install_jar(&self, base: &Path) -> Result<()> {
if let Some(jar) = self.server.read().await.as_ref().map(|(_, server)| {
server.jar.clone()
}).flatten() {
println!("Installing server jar");

let steps = jar.resolve_steps(&self, Environment::Server).await?;

println!("{steps:#?}");

self.execute_steps(base, &steps).await?;
}

Ok(())
}

pub async fn action_install_addons(self: Arc<Self>, base: &Path) -> Result<()> {
let addons = self.collect_addons().await?;
let base = Arc::new(base.to_owned());

const MAX_CONCURRENT_TASKS: usize = 20;
pub mod addons;
pub mod server_jar;

stream::iter(addons).map(Ok).try_for_each_concurrent(
Some(MAX_CONCURRENT_TASKS),
move |addon| {
let app = self.clone();
let base = base.clone();
async move {
app.action_install_addon(&base, &addon).await
}
}
).await?;
impl App {
pub async fn action_build(self: Arc<Self>, base: &Path) -> Result<()> {
self.action_install_jar(base).await?;
self.clone().action_install_addons(base).await?;

Ok(())
}
self.action_generate_script().await?;

pub async fn action_install_addon(self: Arc<Self>, base: &Path, addon: &Addon) -> Result<()> {
let steps = addon.resolve_steps(&self).await?;
let dir = base.join(addon.target.as_str());
self.execute_steps(&dir, &steps).await?;
Ok(())
}
}
21 changes: 21 additions & 0 deletions src/api/app/actions/build/server_jar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::path::Path;

use anyhow::Result;

use crate::api::{app::App, models::Environment};

impl App {
pub async fn action_install_jar(&self, base: &Path) -> Result<()> {
if let Some(jar) = self.server.read().await.as_ref().map(|(_, server)| {
server.jar.clone()
}).flatten() {

Check failure on line 11 in src/api/app/actions/build/server_jar.rs

View workflow job for this annotation

GitHub Actions / clippy

called `map(..).flatten()` on `Option`

error: called `map(..).flatten()` on `Option` --> src/api/app/actions/build/server_jar.rs:9:62 | 9 | if let Some(jar) = self.server.read().await.as_ref().map(|(_, server)| { | ______________________________________________________________^ 10 | | server.jar.clone() 11 | | }).flatten() { | |____________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten = note: `-D clippy::map-flatten` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::map_flatten)]` help: try replacing `map` with `and_then` and remove the `.flatten()` | 9 ~ if let Some(jar) = self.server.read().await.as_ref().and_then(|(_, server)| { 10 + server.jar.clone() 11 ~ }) { |
println!("Installing server jar");

let steps = jar.resolve_steps(&self, Environment::Server).await?;

Check failure on line 14 in src/api/app/actions/build/server_jar.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/api/app/actions/build/server_jar.rs:14:43 | 14 | let steps = jar.resolve_steps(&self, Environment::Server).await?; | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::needless_borrow)]`

self.execute_steps(base, &steps).await?;
}

Ok(())
}
}
33 changes: 25 additions & 8 deletions src/api/app/actions/markdown/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

use anyhow::Result;

use crate::api::{app::App, models::{markdown::{MarkdownOptions, MarkdownOutput}, metadata::{AddonMetadata, AddonMetadataSource}}};
use crate::api::{app::App, models::{markdown::MarkdownOptions, metadata::{AddonMetadata, MetadataContainer}}};

impl App {
pub async fn get_markdown_options(&self) -> Option<MarkdownOptions> {
Expand All @@ -13,7 +13,7 @@ impl App {
}
}

pub async fn get_all_metadata(self: Arc<Self>) -> Result<Vec<AddonMetadata>> {
pub async fn get_all_addon_metadata(self: Arc<Self>) -> Result<Vec<AddonMetadata>> {
let addons = self.collect_addons().await?;

let mut metadata = vec![];
Expand All @@ -28,18 +28,35 @@ impl App {
Ok(metadata)
}

pub async fn render_markdown_with(&self, metadata: Vec<AddonMetadata>) -> Result<String> {
pub async fn get_metadata(self: Arc<Self>) -> Result<MetadataContainer> {
let addons = self.get_all_addon_metadata().await?;

Ok(MetadataContainer {
addons
})
}

pub async fn render_metadata(&self, metadata: MetadataContainer) -> Result<HashMap<String, String>> {
let markdown_options = self.get_markdown_options().await.unwrap_or_default();

let mut map = HashMap::new();

map.insert(String::from("addons"), markdown_options.render_addons(metadata.addons));

Ok(map)
}

pub async fn render_addon_metadata(&self, metadata: Vec<AddonMetadata>) -> Result<String> {

Check warning on line 49 in src/api/app/actions/markdown/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

method `render_addon_metadata` is never used

warning: method `render_addon_metadata` is never used --> src/api/app/actions/markdown/mod.rs:49:18 | 7 | impl App { | -------- method in this implementation ... 49 | pub async fn render_addon_metadata(&self, metadata: Vec<AddonMetadata>) -> Result<String> { | ^^^^^^^^^^^^^^^^^^^^^
let markdown_options = self.get_markdown_options().await.unwrap_or_default();

let table = markdown_options.render(metadata, markdown_options.output_type);
let table = markdown_options.table_addons(metadata, markdown_options.output_type);

Ok(table.render(markdown_options.output_type))
}

pub async fn render_markdown_and_save(self: Arc<Self>) -> Result<()> {
let metadata = self.clone().get_all_metadata().await?;
let content = self.render_markdown_with(metadata).await?;
let options = self.get_markdown_options().await.unwrap_or_default();
let metadata = self.clone().get_metadata().await?;
let rendered = self.render_metadata(metadata).await?;

Check warning on line 59 in src/api/app/actions/markdown/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `rendered`

warning: unused variable: `rendered` --> src/api/app/actions/markdown/mod.rs:59:13 | 59 | let rendered = self.render_metadata(metadata).await?; | ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_rendered`

// TODO

Expand Down
43 changes: 40 additions & 3 deletions src/api/app/actions/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,45 @@ use anyhow::Result;
use crate::api::{app::App, models::server::Server, utils::script::Shell};

impl App {
pub fn get_server_execution_arguments(&self) -> Vec<String> {
todo!()
pub fn get_args(&self, server: &Server) -> Vec<String> {

Check warning on line 6 in src/api/app/actions/script/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

methods `get_args`, `get_args_exec`, `get_args_prefix_suffix`, and `get_script_lines_for` are never used

warning: methods `get_args`, `get_args_exec`, `get_args_prefix_suffix`, and `get_script_lines_for` are never used --> src/api/app/actions/script/mod.rs:6:12 | 5 | impl App { | -------- methods in this implementation 6 | pub fn get_args(&self, server: &Server) -> Vec<String> { | ^^^^^^^^ ... 13 | pub fn get_args_exec(&self, server: &Server) -> Vec<String> { | ^^^^^^^^^^^^^ ... 17 | pub fn get_args_prefix_suffix(&self, server: &Server) -> (Vec<String>, Vec<String>) { | ^^^^^^^^^^^^^^^^^^^^^^ ... 47 | pub fn get_script_lines_for(&self, shell: Shell, server: &Server) -> Vec<String> { | ^^^^^^^^^^^^^^^^^^^^
let (prefix, suffix) = self.get_args_prefix_suffix(server);
let exec = self.get_args_exec(server);

vec![prefix, exec, suffix].concat()

Check failure on line 10 in src/api/app/actions/script/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `vec!`

error: useless use of `vec!` --> src/api/app/actions/script/mod.rs:10:9 | 10 | vec![prefix, exec, suffix].concat() | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[prefix, exec, suffix]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec = note: `-D clippy::useless-vec` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::useless_vec)]`
}

pub fn get_args_exec(&self, server: &Server) -> Vec<String> {

Check warning on line 13 in src/api/app/actions/script/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused `self` argument

warning: unused `self` argument --> src/api/app/actions/script/mod.rs:13:26 | 13 | pub fn get_args_exec(&self, server: &Server) -> Vec<String> { | ^^^^^ | = help: consider refactoring to an associated function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_self = note: `-W clippy::unused-self` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::unused_self)]`
server.jar.as_ref().map(|s| s.get_exec_arguments()).unwrap_or_default()

Check warning on line 14 in src/api/app/actions/script/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant closure

warning: redundant closure --> src/api/app/actions/script/mod.rs:14:33 | 14 | server.jar.as_ref().map(|s| s.get_exec_arguments()).unwrap_or_default() | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `crate::api::models::server::server_type::ServerJar::get_exec_arguments` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls = note: `-W clippy::redundant-closure-for-method-calls` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::redundant_closure_for_method_calls)]`
}

pub fn get_args_prefix_suffix(&self, server: &Server) -> (Vec<String>, Vec<String>) {

Check warning on line 17 in src/api/app/actions/script/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused `self` argument

warning: unused `self` argument --> src/api/app/actions/script/mod.rs:17:35 | 17 | pub fn get_args_prefix_suffix(&self, server: &Server) -> (Vec<String>, Vec<String>) { | ^^^^^ | = help: consider refactoring to an associated function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_self
let mut prefix = vec![];

prefix.extend(server.launcher.jvm_args.split_whitespace().map(ToOwned::to_owned));

// TODO: -Xmx -Xms

prefix.extend(server.launcher.preset_flags.get_flags());

if server.launcher.eula_args && server.jar.as_ref().is_some_and(|x| x.flavor().supports_eula_args()) {
prefix.push(String::from("-Dcom.mojang.eula.agree=true"));
}

for (key, value) in &server.launcher.properties {
let value = serde_json::to_string(value).unwrap();

prefix.push(format!("-D{key}={value}"));
}

let mut suffix = vec![];

if server.launcher.nogui && server.jar.as_ref().is_some_and(|x| x.flavor().supports_nogui()) {
suffix.push(String::from("--nogui"));
}

suffix.extend(server.launcher.game_args.split_whitespace().map(ToOwned::to_owned));

(prefix, suffix)
}

pub fn get_script_lines_for(&self, shell: Shell, server: &Server) -> Vec<String> {
Expand All @@ -16,7 +53,7 @@ impl App {

lines.extend(server.launcher.prelaunch.clone());

todo!();
lines.push(self.get_args(server).join(" "));

lines.extend(server.launcher.postlaunch.clone());

Expand Down
4 changes: 3 additions & 1 deletion src/api/app/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
path::PathBuf,
};

use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use serde::de::DeserializeOwned;

use crate::api::step::CacheLocation;
Expand Down Expand Up @@ -49,6 +49,8 @@ impl Cache {
Some(base) => {
let fullpath = base.join(path);

std::fs::create_dir_all(fullpath.parent().ok_or(anyhow!("No parent"))?)?;

let writer = BufWriter::new(
File::create(&fullpath)
.context(format!("Creating cache file at: {}", fullpath.display()))?,
Expand Down
1 change: 1 addition & 0 deletions src/api/models/addon/addon_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum AddonType {
version: String,
},

#[serde(alias = "ghrel")]
GithubRelease {
#[serde(alias = "repository")]
repo: String,
Expand Down
6 changes: 5 additions & 1 deletion src/api/models/markdown/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use crate::api::{models::metadata::AddonMetadata, utils::markdown::{HeaderAlignm
use super::{MarkdownOptions, MarkdownOutput, MdColumn};

impl MarkdownOptions {
pub fn render(&self, list: Vec<AddonMetadata>, output: MarkdownOutput) -> MarkdownTable {
pub fn render_addons(&self, list: Vec<AddonMetadata>) -> String {
self.table_addons(list, self.output_type).render(self.output_type)
}

pub fn table_addons(&self, list: Vec<AddonMetadata>, output: MarkdownOutput) -> MarkdownTable {

Check warning on line 10 in src/api/models/markdown/render.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument is passed by value, but not consumed in the function body

warning: this argument is passed by value, but not consumed in the function body --> src/api/models/markdown/render.rs:10:38 | 10 | pub fn table_addons(&self, list: Vec<AddonMetadata>, output: MarkdownOutput) -> MarkdownTable { | ^^^^^^^^^^^^^^^^^^ help: consider changing the type to: `&[AddonMetadata]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value = note: `-W clippy::needless-pass-by-value` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::needless_pass_by_value)]`
let mut table = MarkdownTable::new();

for column in &self.columns {
Expand Down
7 changes: 5 additions & 2 deletions src/api/models/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use serde::{Deserialize, Serialize};

mod addon_metadata;

pub use addon_metadata::*;

pub enum MetadataBlock {
Addons(Vec<AddonMetadata>),
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MetadataContainer {
pub addons: Vec<AddonMetadata>,
}
2 changes: 1 addition & 1 deletion src/api/models/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

pub const NETWORK_TOML: &str = "network.toml";

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Network {
pub name: String,
}
14 changes: 14 additions & 0 deletions src/api/models/server/server_flavor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ impl ServerFlavor {
ServerFlavor::Proxy => true,
}
}

pub fn supports_nogui(&self) -> bool {

Check warning on line 37 in src/api/models/server/server_flavor.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)

warning: this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte) --> src/api/models/server/server_flavor.rs:37:27 | 37 | pub fn supports_nogui(&self) -> bool { | ^^^^^ help: consider passing by value instead: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
match self {
ServerFlavor::Proxy => false,
_ => true,
}

Check failure on line 41 in src/api/models/server/server_flavor.rs

View workflow job for this annotation

GitHub Actions / clippy

match expression looks like `matches!` macro

error: match expression looks like `matches!` macro --> src/api/models/server/server_flavor.rs:38:9 | 38 | / match self { 39 | | ServerFlavor::Proxy => false, 40 | | _ => true, 41 | | } | |_________^ help: try: `!matches!(self, ServerFlavor::Proxy)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
}

pub fn supports_eula_args(&self) -> bool {

Check warning on line 44 in src/api/models/server/server_flavor.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)

warning: this argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte) --> src/api/models/server/server_flavor.rs:44:31 | 44 | pub fn supports_eula_args(&self) -> bool { | ^^^^^ help: consider passing by value instead: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
match self {
ServerFlavor::Patched => true,
_ => false,
}

Check failure on line 48 in src/api/models/server/server_flavor.rs

View workflow job for this annotation

GitHub Actions / clippy

match expression looks like `matches!` macro

error: match expression looks like `matches!` macro --> src/api/models/server/server_flavor.rs:45:9 | 45 | / match self { 46 | | ServerFlavor::Patched => true, 47 | | _ => false, 48 | | } | |_________^ help: try: `matches!(self, ServerFlavor::Patched)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
}
}
17 changes: 16 additions & 1 deletion src/api/models/server/server_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub enum ServerType {

#[serde(default)]
flavor: ServerFlavor,

#[serde(default)]
exec: Option<String>,
},
}

Expand Down Expand Up @@ -131,6 +134,18 @@ impl ServerJar {
}
}

pub fn get_exec_arguments(&self) -> Vec<String> {
match &self.server_type {
ServerType::Forge { .. } => todo!(),
ServerType::Custom { exec, .. } => exec.clone()
.unwrap_or(String::from("-jar server.jar"))
.split_whitespace()
.map(ToOwned::to_owned)
.collect::<Vec<_>>(),
_ => vec![String::from("-jar"), String::from("server.jar")],
}
}

pub async fn update(&mut self, app: &App) -> Result<bool> {
match self.server_type.clone() {
ServerType::Vanilla { } => Ok(false),
Expand Down Expand Up @@ -160,7 +175,7 @@ impl ServerJar {
ServerType::NeoForge { loader } => todo!(),

Check warning on line 175 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `loader`

warning: unused variable: `loader` --> src/api/models/server/server_type.rs:175:36 | 175 | ServerType::NeoForge { loader } => todo!(), | ^^^^^^ help: try ignoring the field: `loader: _`
ServerType::Forge { loader } => todo!(),

Check warning on line 176 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `loader`

warning: unused variable: `loader` --> src/api/models/server/server_type.rs:176:33 | 176 | ServerType::Forge { loader } => todo!(), | ^^^^^^ help: try ignoring the field: `loader: _`
ServerType::BuildTools { .. } => Ok(false),

Check warning on line 177 in src/api/models/server/server_type.rs

View workflow job for this annotation

GitHub Actions / clippy

this match arm has an identical body to another arm

warning: this match arm has an identical body to another arm --> src/api/models/server/server_type.rs:177:13 | 177 | ServerType::BuildTools { .. } => Ok(false), | -----------------------------^^^^^^^^^^^^^ | | | help: try merging the arm patterns: `ServerType::BuildTools { .. } | ServerType::Vanilla { }` | = help: or try changing either arm body note: other arm here --> src/api/models/server/server_type.rs:151:13 | 151 | ServerType::Vanilla { } => Ok(false), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_same_arms
ServerType::Custom { inner, flavor } => todo!(),
ServerType::Custom { .. } => todo!(),
}
}
}
13 changes: 6 additions & 7 deletions src/api/sources/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ impl<'a> GithubAPI<'a> {
.http_get_with(format!("{}/{url}", self.0.options.api_urls.github), |req| {
req.headers(headers)
})
.await?;
.await
.with_context(|| format!("Github: HTTP GET /{url}"))?;

if response.status() == StatusCode::NOT_MODIFIED {
Ok(cached_data.unwrap().data)
} else {
let etag = response.headers().get("etag").cloned();

let json: T = response.json().await?;
let json: T = response.json().await
.with_context(|| format!("Github: JSON decoding: {url}"))?;

if let Some(etag) = etag {
self.0.cache.try_write_json(
Expand Down Expand Up @@ -96,8 +98,7 @@ impl<'a> GithubAPI<'a> {
"latest" => releases.first(),
tag => releases
.iter()
.find(|r| r.tag_name == tag)
.or_else(|| releases.iter().find(|r| r.tag_name.contains(tag))),
.find(|r| r.tag_name == tag),
}
.ok_or(anyhow!(
"Github release '{tag}' not found on repository '{repo}'"
Expand All @@ -118,9 +119,7 @@ impl<'a> GithubAPI<'a> {
"" | "first" | "any" => release.assets.first(),
id => {
let id = if id.contains('$') {
id.replace("${version}", &release.tag_name)
.replace("${tag}", &release.tag_name)
.replace("${release}", &release.tag_name)
id.replace("${tag}", release_tag)
} else {
id.to_owned()
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/sources/github/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct GithubRelease {
pub struct GithubAsset {
pub url: String,
pub name: String,
pub label: String,
pub label: Option<String>,
pub size: u64,
pub download_count: u64,
}
Expand Down
Loading

0 comments on commit e4cb7bd

Please sign in to comment.