Compare commits
8 Commits
a1ff9a4074
...
cb905667f7
Author | SHA1 | Date | |
---|---|---|---|
cb905667f7 | |||
c6672f3ba6 | |||
3accc96550 | |||
84434f887c | |||
0749646060 | |||
cd544e71e3 | |||
681b3a96b7 | |||
c6f08d3365 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,6 +3,7 @@
|
|||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
debug/
|
debug/
|
||||||
target/
|
target/
|
||||||
|
crates/alhp_api/target
|
||||||
|
|
||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
1599
Cargo.lock
generated
1599
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -5,4 +5,5 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
alhp_api = {path = "crates/alhp_api"}
|
alhp_api = {path = "crates/alhp_api"}
|
||||||
|
alpm = "4.0.2"
|
||||||
|
serde_json = "1.0.140"
|
||||||
|
@@ -8,3 +8,4 @@ authors = ["mpuchstein"]
|
|||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_json = "1.0.140"
|
serde_json = "1.0.140"
|
||||||
reqwest = { version = "0.12.15" , features = ["blocking"] }
|
reqwest = { version = "0.12.15" , features = ["blocking"] }
|
||||||
|
log = "0.4.27"
|
||||||
|
@@ -1,6 +1,14 @@
|
|||||||
|
const API_BASE_URL: &str = "https://api.alhp.dev";
|
||||||
|
const API_PACKAGES_EXT: &str = "/packages?";
|
||||||
|
const API_GENERAL_EXT: &str = "/stats?";
|
||||||
|
|
||||||
|
pub mod packages {
|
||||||
|
use crate::{API_BASE_URL, API_PACKAGES_EXT};
|
||||||
|
use log::{error, info};
|
||||||
|
use reqwest::StatusCode;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
use reqwest::blocking::Client;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
@@ -18,21 +26,37 @@ pub enum PackageStatus{
|
|||||||
|
|
||||||
impl fmt::Display for PackageStatus {
|
impl fmt::Display for PackageStatus {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}", serde_json::to_string(self).unwrap().trim_matches('"'))
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
match self {
|
||||||
|
PackageStatus::Latest => "latest",
|
||||||
|
PackageStatus::Failed => "failed",
|
||||||
|
PackageStatus::Built => "build",
|
||||||
|
PackageStatus::Skipped => "skipped",
|
||||||
|
PackageStatus::Delayed => "delayed",
|
||||||
|
PackageStatus::Building => "building",
|
||||||
|
PackageStatus::Signing => "signing",
|
||||||
|
PackageStatus::Unknown => "unknown",
|
||||||
|
PackageStatus::Queued => "queued",
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Package {
|
pub struct Package {
|
||||||
pkgbase: String,
|
pub pkgbase: String,
|
||||||
repo: String,
|
pub repo: String,
|
||||||
split_packages: Vec<String>,
|
pub split_packages: Vec<String>,
|
||||||
status: PackageStatus,
|
pub status: PackageStatus,
|
||||||
lto: String,
|
pub skip_reason: Option<String>,
|
||||||
debug_symbols: String,
|
pub lto: String,
|
||||||
arch_version: String,
|
pub debug_symbols: String,
|
||||||
repo_version: String,
|
pub arch_version: String,
|
||||||
build_date: String,
|
pub repo_version: String,
|
||||||
|
pub build_date: Option<String>,
|
||||||
|
pub peak_mem: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -40,7 +64,7 @@ pub struct PackageResponse {
|
|||||||
pub total: usize,
|
pub total: usize,
|
||||||
pub offset: usize,
|
pub offset: usize,
|
||||||
pub limit: usize,
|
pub limit: usize,
|
||||||
pub packages: Vec<Package>
|
pub packages: Vec<Package>,
|
||||||
}
|
}
|
||||||
#[derive(Debug, Serialize, Default)]
|
#[derive(Debug, Serialize, Default)]
|
||||||
pub struct PackageRequest {
|
pub struct PackageRequest {
|
||||||
@@ -71,13 +95,110 @@ impl PackageRequest {
|
|||||||
params.push(format!("limit={}", self.limit));
|
params.push(format!("limit={}", self.limit));
|
||||||
params.join("&")
|
params.join("&")
|
||||||
}
|
}
|
||||||
pub fn response(&self) -> Result<PackageResponse, reqwest::Error> {
|
pub fn response(&self) -> Result<PackageResponse, Box<dyn Error>> {
|
||||||
let client = Client::new();
|
let query_url = format!(
|
||||||
let query_string = self.query_string();
|
"{}{}{}",
|
||||||
let url = format!("https://api.alhp.dev/packages?{}", query_string);
|
API_BASE_URL,
|
||||||
println!("{}", url);
|
API_PACKAGES_EXT,
|
||||||
let response = client.get(url).send()?.text()?;
|
self.query_string()
|
||||||
let response: PackageResponse = serde_json::from_str(&response).unwrap();
|
);
|
||||||
Ok(response)
|
info!("Fetching URL: {}", query_url);
|
||||||
|
let response = reqwest::blocking::get(query_url)?;
|
||||||
|
match response.status() {
|
||||||
|
StatusCode::OK => {
|
||||||
|
let response = response.text()?;
|
||||||
|
match serde_json::from_str(&response) {
|
||||||
|
Ok(json) => Ok(json),
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to deserialize JSON: {}", e);
|
||||||
|
error!("Response body: {}", response);
|
||||||
|
Err(Box::new(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StatusCode::NOT_FOUND => {
|
||||||
|
info!("No packages found");
|
||||||
|
Ok(PackageResponse {
|
||||||
|
total: 0,
|
||||||
|
offset: 0,
|
||||||
|
limit: 0,
|
||||||
|
packages: vec![],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR => {
|
||||||
|
panic!("Internal Server Error");
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let query_url = format!(
|
||||||
|
"{}{}{}",
|
||||||
|
API_BASE_URL,
|
||||||
|
API_PACKAGES_EXT,
|
||||||
|
self.query_string()
|
||||||
|
);
|
||||||
|
panic!(
|
||||||
|
"Unexpected server response: {:?} for query url: {}",
|
||||||
|
response, query_url
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod general {
|
||||||
|
use crate::{API_BASE_URL, API_GENERAL_EXT};
|
||||||
|
use log::error;
|
||||||
|
use reqwest::StatusCode;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct LtoStats {
|
||||||
|
pub enabled: usize,
|
||||||
|
pub disabled: usize,
|
||||||
|
pub unknown: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct GeneralResponse {
|
||||||
|
pub failed: usize,
|
||||||
|
pub skipped: usize,
|
||||||
|
pub latest: usize,
|
||||||
|
pub queued: usize,
|
||||||
|
pub lto: LtoStats,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct GeneralRequest {
|
||||||
|
//No params yet.
|
||||||
|
}
|
||||||
|
impl GeneralRequest {
|
||||||
|
pub fn response(&self) -> Result<GeneralResponse, Box<dyn Error>> {
|
||||||
|
let query_url = format!("{}{}", API_BASE_URL, API_GENERAL_EXT);
|
||||||
|
let response = reqwest::blocking::get(query_url)?;
|
||||||
|
match response.status() {
|
||||||
|
StatusCode::OK => {
|
||||||
|
let response = response.text()?;
|
||||||
|
match serde_json::from_str(&response) {
|
||||||
|
Ok(json) => Ok(json),
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to deserialize JSON: {}", e);
|
||||||
|
error!("Response body: {}", response);
|
||||||
|
Err(Box::new(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR => {
|
||||||
|
panic!("Internal Server Error");
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let query_url = format!("{}{}", API_BASE_URL, API_GENERAL_EXT,);
|
||||||
|
panic!(
|
||||||
|
"Unexpected server response: {:?} for query url: {}",
|
||||||
|
response, query_url
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
66
src/main.rs
66
src/main.rs
@@ -1,31 +1,55 @@
|
|||||||
use alhp_api;
|
use alhp_api;
|
||||||
use std::process::Command;
|
use alhp_api::general::{GeneralRequest, GeneralResponse};
|
||||||
use alhp_api::{PackageRequest, PackageStatus};
|
use alhp_api::packages::{Package, PackageRequest, PackageStatus};
|
||||||
|
use alpm::Alpm;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
fn pacman_query_installed_packages() -> Vec<String> {
|
fn query_installed_packages() -> HashSet<String> {
|
||||||
match Command::new("pacman").arg("-Qqn").output() {
|
let alpm = match Alpm::new("/", "/var/lib/pacman") {
|
||||||
Ok(packages) => String::from_utf8_lossy(&packages.stdout)
|
Ok(alpm) => alpm,
|
||||||
.lines()
|
Err(_) => panic!("Error establishing ALPM handle."),
|
||||||
.map(|s| s.to_string())
|
};
|
||||||
.collect(),
|
let local_db = alpm.localdb();
|
||||||
_ => {
|
let pkgs = local_db.pkgs();
|
||||||
panic!("pacman query failed");
|
pkgs
|
||||||
}
|
.into_iter()
|
||||||
}
|
.map(|pkg| pkg.name().to_owned())
|
||||||
|
.collect::<HashSet<String>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn get_stats() -> GeneralResponse {
|
||||||
let installed_packages = pacman_query_installed_packages();
|
let stats = GeneralRequest {};
|
||||||
println!("installed packages: {:?}", installed_packages);
|
stats.response().unwrap()
|
||||||
let status = vec!();
|
}
|
||||||
|
|
||||||
|
fn get_queued_building() -> Vec<Package> {
|
||||||
let pkg = PackageRequest {
|
let pkg = PackageRequest {
|
||||||
status,
|
status: vec![PackageStatus::Building, PackageStatus::Queued],
|
||||||
pkgbase: Some("go".to_string()),
|
pkgbase: None,
|
||||||
exact: true,
|
exact: false,
|
||||||
repo: Some("extra-x86-64-v3".to_string()),
|
// repo: Some("extra-x86-64-v3".to_string()),
|
||||||
|
repo: None,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 0,
|
limit: 0,
|
||||||
};
|
};
|
||||||
let pkg = pkg.response().unwrap();
|
let pkg = pkg.response().unwrap();
|
||||||
println!("pkg: {:?}", pkg);
|
pkg.packages
|
||||||
|
}
|
||||||
|
|
||||||
|
fn match_local_alhp() -> Vec<String> {
|
||||||
|
let local = query_installed_packages();
|
||||||
|
let remote = get_queued_building();
|
||||||
|
let mut matched = Vec::<String>::new();
|
||||||
|
for pkg in remote {
|
||||||
|
if local.contains(&pkg.pkgbase) {
|
||||||
|
matched.push(pkg.pkgbase.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
matched
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("{}", serde_json::to_string_pretty(&get_stats()).unwrap());
|
||||||
|
println!("{}", serde_json::to_string_pretty(&get_queued_building()).unwrap());
|
||||||
|
println!("{}", serde_json::to_string_pretty(&match_local_alhp()).unwrap());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user