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
|
||||
debug/
|
||||
target/
|
||||
crates/alhp_api/target
|
||||
|
||||
# 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
|
||||
|
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]
|
||||
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_json = "1.0.140"
|
||||
reqwest = { version = "0.12.15" , features = ["blocking"] }
|
||||
log = "0.4.27"
|
||||
|
@@ -1,10 +1,18 @@
|
||||
use std::fmt;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use reqwest::blocking::Client;
|
||||
const API_BASE_URL: &str = "https://api.alhp.dev";
|
||||
const API_PACKAGES_EXT: &str = "/packages?";
|
||||
const API_GENERAL_EXT: &str = "/stats?";
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum PackageStatus{
|
||||
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;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum PackageStatus {
|
||||
Latest,
|
||||
Failed,
|
||||
Built,
|
||||
@@ -14,45 +22,61 @@ pub enum PackageStatus{
|
||||
Signing,
|
||||
Unknown,
|
||||
Queued,
|
||||
}
|
||||
|
||||
impl fmt::Display for PackageStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", serde_json::to_string(self).unwrap().trim_matches('"'))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Package {
|
||||
pkgbase: String,
|
||||
repo: String,
|
||||
split_packages: Vec<String>,
|
||||
status: PackageStatus,
|
||||
lto: String,
|
||||
debug_symbols: String,
|
||||
arch_version: String,
|
||||
repo_version: String,
|
||||
build_date: String,
|
||||
}
|
||||
impl fmt::Display for PackageStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
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)]
|
||||
pub struct PackageResponse {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Package {
|
||||
pub pkgbase: String,
|
||||
pub repo: String,
|
||||
pub split_packages: Vec<String>,
|
||||
pub status: PackageStatus,
|
||||
pub skip_reason: Option<String>,
|
||||
pub lto: String,
|
||||
pub debug_symbols: String,
|
||||
pub arch_version: String,
|
||||
pub repo_version: String,
|
||||
pub build_date: Option<String>,
|
||||
pub peak_mem: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PackageResponse {
|
||||
pub total: usize,
|
||||
pub offset: usize,
|
||||
pub limit: usize,
|
||||
pub packages: Vec<Package>
|
||||
}
|
||||
#[derive(Debug, Serialize, Default)]
|
||||
pub struct PackageRequest {
|
||||
pub packages: Vec<Package>,
|
||||
}
|
||||
#[derive(Debug, Serialize, Default)]
|
||||
pub struct PackageRequest {
|
||||
pub status: Vec<PackageStatus>,
|
||||
pub pkgbase: Option<String>,
|
||||
pub exact: bool,
|
||||
pub repo: Option<String>,
|
||||
pub offset: usize,
|
||||
pub limit: usize,
|
||||
}
|
||||
}
|
||||
|
||||
impl PackageRequest {
|
||||
impl PackageRequest {
|
||||
fn query_string(&self) -> String {
|
||||
let mut params = Vec::new();
|
||||
for status in &self.status {
|
||||
@@ -71,13 +95,110 @@ impl PackageRequest {
|
||||
params.push(format!("limit={}", self.limit));
|
||||
params.join("&")
|
||||
}
|
||||
pub fn response(&self) -> Result<PackageResponse, reqwest::Error> {
|
||||
let client = Client::new();
|
||||
let query_string = self.query_string();
|
||||
let url = format!("https://api.alhp.dev/packages?{}", query_string);
|
||||
println!("{}", url);
|
||||
let response = client.get(url).send()?.text()?;
|
||||
let response: PackageResponse = serde_json::from_str(&response).unwrap();
|
||||
Ok(response)
|
||||
pub fn response(&self) -> Result<PackageResponse, Box<dyn Error>> {
|
||||
let query_url = format!(
|
||||
"{}{}{}",
|
||||
API_BASE_URL,
|
||||
API_PACKAGES_EXT,
|
||||
self.query_string()
|
||||
);
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
68
src/main.rs
68
src/main.rs
@@ -1,31 +1,55 @@
|
||||
use alhp_api;
|
||||
use std::process::Command;
|
||||
use alhp_api::{PackageRequest, PackageStatus};
|
||||
use alhp_api::general::{GeneralRequest, GeneralResponse};
|
||||
use alhp_api::packages::{Package, PackageRequest, PackageStatus};
|
||||
use alpm::Alpm;
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn pacman_query_installed_packages() -> Vec<String> {
|
||||
match Command::new("pacman").arg("-Qqn").output() {
|
||||
Ok(packages) => String::from_utf8_lossy(&packages.stdout)
|
||||
.lines()
|
||||
.map(|s| s.to_string())
|
||||
.collect(),
|
||||
_ => {
|
||||
panic!("pacman query failed");
|
||||
}
|
||||
}
|
||||
fn query_installed_packages() -> HashSet<String> {
|
||||
let alpm = match Alpm::new("/", "/var/lib/pacman") {
|
||||
Ok(alpm) => alpm,
|
||||
Err(_) => panic!("Error establishing ALPM handle."),
|
||||
};
|
||||
let local_db = alpm.localdb();
|
||||
let pkgs = local_db.pkgs();
|
||||
pkgs
|
||||
.into_iter()
|
||||
.map(|pkg| pkg.name().to_owned())
|
||||
.collect::<HashSet<String>>()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let installed_packages = pacman_query_installed_packages();
|
||||
println!("installed packages: {:?}", installed_packages);
|
||||
let status = vec!();
|
||||
let pkg = PackageRequest{
|
||||
status,
|
||||
pkgbase: Some("go".to_string()),
|
||||
exact: true,
|
||||
repo: Some("extra-x86-64-v3".to_string()),
|
||||
fn get_stats() -> GeneralResponse {
|
||||
let stats = GeneralRequest {};
|
||||
stats.response().unwrap()
|
||||
}
|
||||
|
||||
fn get_queued_building() -> Vec<Package> {
|
||||
let pkg = PackageRequest {
|
||||
status: vec![PackageStatus::Building, PackageStatus::Queued],
|
||||
pkgbase: None,
|
||||
exact: false,
|
||||
// repo: Some("extra-x86-64-v3".to_string()),
|
||||
repo: None,
|
||||
offset: 0,
|
||||
limit: 0,
|
||||
};
|
||||
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