refactors

This commit is contained in:
Lilly Rosaline 2022-02-13 14:18:46 -06:00
parent 979cbacc5a
commit 7f616765b1
3 changed files with 62 additions and 36 deletions

View file

@ -9,22 +9,19 @@ mod package;
use crate::repository::find_package_in_repositories;
fn main() {
let operation = std::env::args().nth(1).expect("too few arguments!");
let args: String = std::env::args().collect();
let package_data = crate::package::read_package(std::fs::read_to_string("example-package/Package.toml").expect("Failed to read file"));
dbg!(package_data);
let operation = std::env::args().nth(1).expect("too few arguments!"); // Get the operation
match operation.as_str() {
"upgrade" | "u" => {
"upgrade" | "u" => { // Will upgrade the selected package(s).
}
"install" | "i" => {
if std::env::args().nth(2).expect("too few arguments!") == "--local" {
"install" | "i" => { // Install the selected package(s).
if std::env::args().nth(2).expect("too few arguments!") == "--archive" { // Indicates that the package(s) are archives instead of folders.
} else {
let package_name = std::env::args().nth(2).expect("too few arguments!");
find_package_in_repositories(&package_name, None);
let package_name = std::env::args().nth(2).expect("too few arguments!"); // Get the first package name
find_package_in_repositories(&package_name, None); // Go through and find the package
println!("Reading package list");
let package_list = sled::open("/var/gaypack/packages").expect("Failed to open package list");
let package_list = sled::open("/var/gaypk/packages").expect("Failed to open package list");
if package_list.contains_key(&package_name).expect("Unexpected error") {
println!("Package was found in package list, checking for updates");
}
@ -38,6 +35,9 @@ fn main() {
}
"query" | "q" => {
}
"config" | "c" => {
}
"repository" | "repo" => {

View file

@ -1,38 +1,64 @@
use futures::executor::block_on;
use reqwest::StatusCode;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufRead;
use futures::executor::block_on;
use reqwest::StatusCode;
pub fn find_package_in_repositories(package: &str, installed: Option<u64>) -> Option<Vec<String>> {
let mut hits: Vec<String> = vec!();
let client = reqwest::Client::new();
let repo_file = File::open("/var/gaypack/repositories.list").expect("Failed to open repository list");
let mut candidates: HashMap<String, u64> = HashMap::new();
/// Returns a map of the repositories that have a given package as well as the version that they have.
pub fn find_package_in_repositories(package: &str, installed: Option<u64>, ) -> Option<HashMap<String, u64>> {
let client = reqwest::Client::new(); // The reqwest client which will make GET requests to remove servers.
let repo_file =
File::open("/var/gaypk/repositories.list").expect("Failed to open repository list"); // The repositories.list file, which contains a list of all known and trusted repositories.
let mut candidates: HashMap<String, u64> = HashMap::new(); // This map will store a list of the repositories along with the latest version of the package they have.
if installed.is_some() {
candidates.insert("installed".to_string(), installed.unwrap());
} // If the package is installed, put the installed version in.
for repository in std::io::BufReader::new(repo_file).lines() {
let repository = repository.expect("Unexpected error"); // i find this line kinda funny for some reason
let resp = block_on(client.get(format!("{}/gaypack/{}/latest/Package.toml", repository, package)).send()).expect("GET request failed");
let status = resp.status();
let text = block_on(resp.text()).expect("Failed to receive packet body");
if installed.is_some() {
candidates.insert("installed".to_string(), installed.unwrap());
}
let package_data = &crate::package::read_package(text);
dbg!(package_data);
// Iterate through all the repositories in repositories.list
let repository = repository.unwrap();
let resp = block_on(
client
.get(format!("{repository}/gaypk/{package}/Package.toml.latest"))
.send(),
)
.expect("GET request failed"); // Send a GET request to the current repository, asking for the package's latest known package.toml. TODO protect against version number spoofing
let status = resp.status(); // Get the status code.
let text = block_on(resp.text()).expect("Failed to receive packet body"); // Get the message body.
let package_data = &crate::package::read_package(text); // Parse through TOML to a PackageData.
if status == StatusCode::from_u16(200).unwrap() {
// Only add repositories that return 200
candidates.insert(repository, package_data.revision);
} else {
println!("Repository {repository} returned {status}");
}
};
let mut sorted: Vec<_> = candidates.iter().collect();
sorted.sort_by_key(|entry| entry.1);
let mut highest_version: u64 = 0;
for (repository, version) in sorted.iter() {
if **version > highest_version {highest_version = **version};
hits.push(repository.as_str().to_string());
}
if candidates.get("installed") == Some(highest_version) {
let mut sorted: Vec<_> = candidates.iter().collect(); // Convert candidates into a vector for sorting
sorted.sort_by_key(|entry| entry.1); // Sort by version number, must be stable to sort in order of priority
let mut candidates = HashMap::new(); // Clear out the old candidates map
for (k, v) in sorted.iter() {
// Iterate through sorted and transfer everything to the new candidates map
candidates.insert(k.as_str().to_string(), **v); // This is an awful line of code and i hate it
}
let mut highest_version: u64 = 0; // The highest version so far
for (_, version) in candidates.iter() {
// Iterate through and find the highest version
if *version > highest_version {
highest_version = *version
};
}
if candidates.get("installed") == Some(&highest_version) {
// If the highest version is equal to the installed version, return None. If not, return the list of candidates for further processing
None
} else {
Some(hits)
Some(candidates)
}
}
}