Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 121 additions & 137 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ required-features = []


[features]
default = ["templatr", "network_test", "cli", "gitoxide"]
default = ["templatr", "network_test", "cli", "gitoxide", "reqwest"]
templatr = ["dep:templatr"]

reqwest = ["dep:reqwest"]
ureq = ["dep:ureq"]

cli = ["dep:clap", "dep:clap_complete", "dep:vergen", "dep:pbr"]
gitoxide = ["dep:gix"]
network_test = []
Expand Down Expand Up @@ -49,14 +53,15 @@ color-eyre = { version = "0.6", default-features = false }
pbr = { version = "*", optional = true } #{ git = "https://github.com/a8m/pb.git" }

bytes = "*"
ureq = { version = "2", features = ["json"], optional = true }
reqwest = { version = "0.12", features = [
"blocking",
"json",
"gzip",
"deflate",
"brotli",
"rustls-tls",
], default-features = false }
], default-features = false, optional = true }

clap = { version = "4", features = ["derive"], optional = true }
clap_complete = { version = "4", optional = true }
Expand All @@ -80,7 +85,7 @@ gix = { version = "*", features = [
"worktree-mutation",
"blocking-network-client",
"blocking-http-transport-reqwest-rust-tls",
], optional = true, default-features = false}
], optional = true, default-features = false }

# Use PR with symlink fix for Unix systems.
zip = "2"
Expand All @@ -90,6 +95,7 @@ symlink = "0.1.0"
fs_extra = "1.2"
itertools = "0.14"
schemars = { version = "0.8", features = ["semver"] }
thiserror = "2.0.12"

[target.aarch64-apple-darwin.dependencies]
# Allow cross compiles
Expand Down
7 changes: 5 additions & 2 deletions src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use owo_colors::OwoColorize;
use qpm_package::models::{dependency::SharedPackageConfig, package::PackageConfig};

use crate::{
models::{config::get_publish_keyring, package::PackageConfigExtensions},
repository::{Repository, qpackages::QPMRepository},
models::{
config::get_publish_keyring,
package::{PackageConfigExtensions},
},
repository::{qpackages::QPMRepository, Repository},
terminal::colors::QPMColor,
};

Expand Down
4 changes: 3 additions & 1 deletion src/commands/qmod/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::{Args, Subcommand};
use color_eyre::{Result, owo_colors::OwoColorize};
use color_eyre::Result;
use owo_colors::OwoColorize;


use super::Command;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/scripts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::Path, process::Stdio};
use std::process::Stdio;

use clap::Args;

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ fn main() -> Result<()> {
}

Ok(())
}
}
116 changes: 0 additions & 116 deletions src/network/agent.rs
Original file line number Diff line number Diff line change
@@ -1,116 +0,0 @@
use std::{
env,
io::{ErrorKind, Read, Write},
sync,
thread::sleep,
time::Duration,
};

use color_eyre::{
Result,
eyre::{Context, ensure},
};

use crate::models::config::get_combine_config;

static AGENT: sync::OnceLock<reqwest::blocking::Client> = sync::OnceLock::new();

pub fn get_agent() -> &'static reqwest::blocking::Client {
let timeout = get_combine_config().timeout.unwrap_or(5000);

AGENT.get_or_init(|| {
reqwest::blocking::ClientBuilder::new()
.connect_timeout(Duration::from_millis(timeout.into()))
.tcp_keepalive(Duration::from_secs(5))
.tcp_nodelay(false)
.https_only(true)
.user_agent(format!("questpackagemanager-rust2/{}", env!("CARGO_PKG_VERSION")).as_str())
.build()
.expect("Client agent was not buildable")
})
}

pub fn download_file<F>(url: &str, buffer: &mut impl Write, mut callback: F) -> Result<usize>
where
F: FnMut(usize, usize),
{
let mut request = get_agent().get(url).build()?;

request.timeout_mut().take(); // Set to none

let mut response = get_agent()
.execute(request)
.with_context(|| format!("Unable to download file {url}"))?
.error_for_status()?;

let expected_amount = response.content_length().unwrap_or(0) as usize;
let mut written: usize = 0;

let mut temp_buf = vec![0u8; 1024];

loop {
let read = response.read(&mut temp_buf);

match read {
// EOF
Ok(0) => break,

Ok(amount) => {
written += amount;
buffer.write_all(&temp_buf[0..amount])?;
callback(written, expected_amount);
}
Err(e) if e.kind() == ErrorKind::Interrupted => {
sleep(Duration::from_millis(1));
}
Err(e) => {
return Err(e)
.with_context(|| format!("Failed to continue reading bytes from {url}"));
}
}
}

ensure!(
written == expected_amount,
"Read: 0x{written:x} Expected: 0x{expected_amount:x}"
);

Ok(expected_amount)
}

#[inline(always)]
#[cfg(not(feature = "cli"))]
pub fn download_file_report<F>(url: &str, buffer: &mut impl Write, callback: F) -> Result<usize>
where
F: FnMut(usize, usize),
{
download_file(url, buffer, callback)
}

#[inline(always)]
#[cfg(feature = "cli")]
pub fn download_file_report<F>(url: &str, buffer: &mut impl Write, mut callback: F) -> Result<usize>
where
F: FnMut(usize, usize),
{
use pbr::ProgressBar;

let mut progress_bar = ProgressBar::new(0);
progress_bar.set_units(pbr::Units::Bytes);

if env::var("CI") == Ok("true".to_string()) {
progress_bar.set_max_refresh_rate(Some(Duration::from_millis(500)));
}

let result = download_file(url, buffer, |current, expected| {
progress_bar.total = expected as u64;
progress_bar.set(current as u64);

callback(current, expected)
});

progress_bar.finish_println("Finished download!");
println!();

result
}
11 changes: 11 additions & 0 deletions src/network/agent_common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum AgentError<E> {
#[error("Agent error")]
AgentError(Box<E>),
#[error("IO Error")]
IoError(std::io::Error),
#[error("Unauthorized")]
Unauthorized,
}
24 changes: 9 additions & 15 deletions src/network/github.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_eyre::Result;
use serde::{Deserialize, Serialize};

use super::agent::get_agent;
use super::agent::{self};

const GITHUB_OWNER: &str = "QuestPackageManager";
const GITHUB_REPO: &str = "QPM.CLI";
Expand Down Expand Up @@ -46,24 +46,18 @@ pub struct GithubCommitDiffCommitDataResponse {
pub message: String,
}

pub fn get_github_branch(branch: &str) -> Result<GithubBranchResponse, reqwest::Error> {
get_agent()
.get(format!(
"https://api.github.com/repos/{GITHUB_OWNER}/{GITHUB_REPO}/branches/{branch}"
))
.send()?
.json()
pub fn get_github_branch(branch: &str) -> Result<GithubBranchResponse, agent::AgentError> {
agent::get(&format!(
"https://api.github.com/repos/{GITHUB_OWNER}/{GITHUB_REPO}/branches/{branch}"
))
}
pub fn get_github_commit_diff(
old: &str,
new: &str,
) -> Result<GithubCommitDiffResponse, reqwest::Error> {
get_agent()
.get(format!(
"https://api.github.com/repos/{GITHUB_OWNER}/{GITHUB_REPO}/compare/{old}...{new}"
))
.send()?
.json()
) -> Result<GithubCommitDiffResponse, agent::AgentError> {
agent::get(&format!(
"https://api.github.com/repos/{GITHUB_OWNER}/{GITHUB_REPO}/compare/{old}...{new}"
))
}

pub fn download_github_artifact_url(sha: &str) -> String {
Expand Down
14 changes: 13 additions & 1 deletion src/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
pub mod agent;
pub mod github;


#[cfg(all(feature = "reqwest", feature = "ureq"))]
compile_error!("feature \"reqwest\" and feature \"ureq\" cannot be enabled at the same time");

#[cfg(not(any(feature = "reqwest", feature = "ureq")))]
compile_error!("feature \"reqwest\" or feature \"ureq\" must be enabled, though not both simultaneously");

#[cfg_attr(feature = "ureq", path = "ureq_agent.rs")]
#[cfg_attr(feature = "reqwest", path = "reqwest_agent.rs")]
pub mod agent;

pub mod agent_common;
Loading
Loading