installing works. not quite usable

This commit is contained in:
abbie 2023-08-10 09:50:41 +01:00
parent dbe7f73c37
commit 4511983e09
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
4 changed files with 1356 additions and 2 deletions

1187
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,3 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "4.3.21", features = ["derive"] }
home = "0.5.5"
json = "0.12.4"
reqwest = { version = "0.11.18", features = ["blocking"] }

35
src/gitea.rs Normal file
View file

@ -0,0 +1,35 @@
use reqwest::header::USER_AGENT;
pub fn get_data_from_endpoint(package_name: String, endpoint: String) -> Option<json::JsonValue> {
let version = env!("CARGO_PKG_VERSION");
let client = reqwest::blocking::Client::new();
let body = client.get(endpoint.replace("XVX", &package_name))
.header(USER_AGENT, format!("curze/{} (https://git.colean.cc/meta/curze)", version))
.send();
if body.is_ok() {
let json_text_wrapped = body.unwrap().text();
if json_text_wrapped.is_ok() {
let raw_json = json_text_wrapped.unwrap();
let parsed_json = json::parse(&raw_json);
if parsed_json.is_ok() {
return Some(parsed_json.unwrap());
}
else { return None; }
}
else { return None; }
}
else { return None; }
}
pub fn is_package_installable(package_name: String) -> bool {
let wrapped_data = get_data_from_endpoint(package_name, "https://git.colean.cc/api/v1/repos/XVX/topics".to_string());
if wrapped_data.is_some() {
let data = wrapped_data.unwrap();
for topic in data["topics"].members() {
if topic == "install-with-curze" { return true; }
}
return false;
} else {
return false;
}
}

View file

@ -1,3 +1,131 @@
fn main() { mod gitea;
println!("Hello, world!"); use clap::{Parser, Subcommand};
use std::path::Path;
use std::process::Command;
/// A package manager for git.colean.cc
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
/// Installs a list of packages.
Install {
/// Packages to install.
name: Vec<String>,
},
}
fn main() {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Install { name }) => {
if name.is_empty() {
println!("EROR: No packages named for install.");
} else {
for package in name {
install_package(package.to_string());
}
}
}
None => {println!("NOTE: Run curze help for subcommands and options.")}
}
}
fn install_package(package: String) {
println!("NOTE: Checking if {} can be installed.", package);
if gitea::is_package_installable(package.clone()) {
println!("NOTE: Preparing {}.", package);
let cache_result = handle_package_cache(package.clone());
if cache_result {
let realised_name = package.clone().replace("/",".");
let home_out = fetch_home_dir();
let home;
if home_out.is_some() {
home = home_out.unwrap().clone();
} else {
println!("EROR: Your home directory could not be found. Installation halted.");
return;
}
if Path::new(&format!("{}/.local/share/curze/cache/{}/.curze/build.sh", home, realised_name)).exists() {
println!("NOTE: Building {}.", package);
let result_of_build = Command::new("sh")
.arg("-c")
.arg(".curze/build.sh")
.current_dir(format!("{}/.local/share/curze/cache/{}", home, realised_name))
.output();
if result_of_build.is_ok() {} else {
println!("EROR: Build unsuccessful. Installation halted.");
return;
}
}
println!("NOTE: Installing {}.", package);
let result_of_install = Command::new("sh")
.arg("-c")
.arg(".curze/install.sh")
.current_dir(format!("{}/.local/share/curze/cache/{}", home, realised_name))
.output();
if result_of_install.is_ok() {
println!("NOTE: {} successfully installed!", package);
} else {
println!("EROR: Installation failed!");
}
} else {
return;
}
} else {
println!("EROR: {} is not a valid package! Installation halted!", package);
}
}
fn handle_package_cache(package: String) -> bool {
let realised_name = package.clone().replace("/",".");
let home_out = fetch_home_dir();
let home;
if home_out.is_some() {
home = home_out.unwrap().clone();
} else {
println!("EROR: Your home directory could not be found. Installation halted.");
return false;
}
if Path::new(&format!("{}/.local/share/curze/cache/{}/", home, realised_name)).exists() {
println!("NOTE: Pulling latest changes of {}.", package);
let result_of_pull = Command::new("git")
.arg("pull")
.current_dir(format!("{}/.local/share/curze/cache/{}", home, realised_name))
.output();
if result_of_pull.is_ok() {
println!("NOTE: Changes successfully pulled.");
return true;
} else {
println!("WARN: Pull failed. Upstream updates not downloaded. Installation will continue.");
return true;
}
} else {
println!("NOTE: Downloading source of {}.", package);
let result_of_clone = Command::new("git")
.args(["clone",&format!("https://git.colean.cc/{}", package),&realised_name])
.current_dir(format!("{}/.local/share/curze/cache", home))
.output();
if result_of_clone.is_ok() {
println!("NOTE: Package source successfully downloaded.");
return true;
} else {
println!("EROR: Package source not downloaded, installation halted.");
return false;
}
}
}
fn fetch_home_dir() -> Option<String> {
match home::home_dir() {
Some(path) => return Some(path.to_str().unwrap().to_string()),
None => return None,
};
} }