Placement of actions by modules/functions + advanced logging

This commit is contained in:
Listum 2023-11-02 13:52:08 +03:00
parent c66a83d555
commit 9c17c780cc
5 changed files with 113 additions and 87 deletions

10
Cargo.lock generated
View File

@ -48,9 +48,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"aur-rpc", "aur-rpc",
"git2", "git2",
"reqwest", "glob",
"serde",
"serde_json",
"teloxide", "teloxide",
"tokio", "tokio",
] ]
@ -402,6 +400,12 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.21" version = "0.3.21"

56
src/build.rs Normal file
View File

@ -0,0 +1,56 @@
use git2::{Error, Repository};
use std::process::Command;
use glob::glob;
use std::fs;
use std::path::Path;
pub fn clone(pkg: String, dir: String) -> Result<Repository, Error> {
let repo = format!("https://aur.archlinux.org/{}.git", pkg);
Repository::clone(repo.as_str(), dir.as_str())
}
pub async fn copy(dir: String, local_repo: String) -> Result<(), Box<dyn std::error::Error>> {
let entries = fs::read_dir(dir)?;
for entry in entries {
let entry = entry?;
let file_path = entry.path();
if file_path.is_file() && file_path.extension().unwrap_or_default() == "zst" {
let file_name = file_path.file_name().unwrap();
let local_repo = Path::new(local_repo.as_str()).join(file_name);
fs::copy(&file_path, &local_repo)?;
}
}
Ok(())
}
pub async fn build() -> Result<(), Box<dyn std::error::Error>> {
Command::new("makepkg")
.arg("-s")
.arg("--noconfirm")
.output()?;
Ok(())
}
pub fn delete(dir: String) -> std::io::Result<()> {
fs::remove_dir_all(dir)
}
pub async fn repo_add() -> Result<(), Box<dyn std::error::Error>> {
let files: Vec<String> = glob("*.pkg.tar.zst")
.expect("Failed to read glob pattern")
.filter_map(|entry| entry.ok())
.filter_map(|path| path.to_str().map(String::from))
.collect();
if files.is_empty() {
println!("No matching files found.");
}
let vec_of_str_refs: Vec<&str> = files.iter().map(|s| s.as_str()).collect();
Command::new("repo-add")
.arg("repo.db.tar.gz")
.arg(format!("{}", vec_of_str_refs.join(", ")))
.output()?;
Ok(())
}

View File

@ -1,8 +1,9 @@
mod telegram; mod telegram;
use teloxide; mod build;
mod search;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
telegram::main(teloxide::Bot::from_env()).await; telegram::main(teloxide::Bot::from_env()).await;
} }

12
src/search.rs Normal file
View File

@ -0,0 +1,12 @@
use aur_rpc;
pub async fn search(pkg: String) -> String {
let mut packages = aur_rpc::search(pkg).await.unwrap();
packages.sort_by(|a, b| b.num_votes.cmp(&a.num_votes));
let mut result = Vec::new();
for (index, package) in packages.iter().enumerate().take(10) {
result.push(format!("{}. {}", index+1, package.name));
}
let response = result.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("\n");
return response;
}

View File

@ -1,108 +1,61 @@
use teloxide::{prelude::*, utils::command::BotCommands}; use teloxide::{prelude::*, utils::command::BotCommands};
use aur_rpc; use crate::build::{clone, copy, build, delete, repo_add};
use git2::Repository; use crate::search;
use std::process::Command;
use std::env; use std::env;
use glob::glob;
use std::fs;
use std::path::Path;
pub async fn main(bot: Bot){ pub async fn main(bot: Bot){
Commands::repl(bot, answer).await; Commands::repl(bot, answer).await;
} }
#[derive(BotCommands, Clone)] #[derive(BotCommands, Clone)]
#[command(rename_rule = "lowercase", description = "Commands:")] #[command(rename_rule = "lowercase", description = "Commands:")]
enum Commands{ pub(crate) enum Commands{
Upload(String), Upload(String),
Clean,
Search(String) Search(String)
} }
async fn answer(bot: Bot, msg: Message, cmd: Commands) -> ResponseResult<()> { async fn answer(bot: Bot, msg: Message, cmd: Commands) -> ResponseResult<()> {
match cmd { match cmd {
Commands::Upload(pkg) => { Commands::Upload(pkg) => {
let repo = format!("https://aur.archlinux.org/{}.git", pkg); let default_dir = env::current_dir()?;
let dir = format!("pkgs/{}", pkg); let pkg_dir = format!("pkgs/{}", pkg);
let repo_dir = format!("repo/");
match Repository::clone(repo.as_str(), dir.as_str()) { let clone = match clone(pkg, pkg_dir.clone()) {
Ok(_) => bot.send_message(msg.chat.id, format!("Succsess {}", pkg)).await?, Ok(..) => { format!("Cloned") },
Err(e) => bot.send_message(msg.chat.id, format!("Error: {}", e)).await?, Err(e) => { format!("Clone error: {}", e) }
}; };
bot.send_message(msg.chat.id, clone).await?;
let default_dir = env::current_dir().expect("Failed to get default directory"); env::set_current_dir(pkg_dir.clone())?;
env::set_current_dir(dir.clone())?; let build = match build().await {
let compile = Command::new("makepkg") Ok(..) => { format!("Builded") },
.arg("-s") Err(e) => { format!("Build error: {}", e) }
.arg("--noconfirm") };
.output()?; bot.send_message(msg.chat.id, build).await?;
if compile.status.success() {
bot.send_message(msg.chat.id, format!("Build succseed")).await?;
} else {
bot.send_message(msg.chat.id, format!("Build failed: {:?}", compile.status.to_string())).await?;
}
env::set_current_dir(default_dir.clone())?; env::set_current_dir(default_dir.clone())?;
let local_repo = "repo/"; let copy = match copy(pkg_dir.clone(), repo_dir.clone()).await {
let entries = fs::read_dir(dir.clone())?; Ok(..) => { format!("Copied") },
Err(e) => { format!("Copy error: {}", e) }
};
bot.send_message(msg.chat.id, copy).await?;
for entry in entries { let delete = match delete(pkg_dir) {
let entry = entry?; Ok(..) => { format!("Sources deleted")},
let file_path = entry.path(); Err(e) => { format!("Sources deletion error: {}", e) }
};
bot.send_message(msg.chat.id, delete).await?;
if file_path.is_file() && file_path.extension().unwrap_or_default() == "zst" { env::set_current_dir(repo_dir)?;
let file_name = file_path.file_name().unwrap(); let repo_add = match repo_add().await {
let local_repo = Path::new(local_repo).join(file_name); Ok(..) => { format!("Added to repo") },
fs::copy(&file_path, &local_repo).expect("Failed to copy"); Err(e) => { format!("Error adding package to repository: {}", e) }
bot.send_message(msg.chat.id, format!("Package copied to local repo")).await?; };
} bot.send_message(msg.chat.id, repo_add).await?;
}
match fs::remove_dir_all(dir) {
Ok(_) => println!("Sources successfully deleted."),
Err(err) => eprintln!("Error deleting sources: {}", err),
}
env::set_current_dir("repo/")?;
let files: Vec<String> = glob("*.pkg.tar.zst")
.expect("Failed to read glob pattern")
.filter_map(|entry| entry.ok())
.filter_map(|path| path.to_str().map(String::from))
.collect();
if files.is_empty() {
println!("No matching files found.");
}
let vec_of_str_refs: Vec<&str> = files.iter().map(|s| s.as_str()).collect();
let add = Command::new("repo-add")
.arg("repo.db.tar.gz")
.arg(format!("{}", vec_of_str_refs.join(", ")))
.output()?;
eprintln!("repo-add output: {:?}", String::from_utf8_lossy(&add.stderr));
if add.status.success() {
bot.send_message(msg.chat.id, format!("Added to repo successfuly")).await?;
} else {
bot.send_message(msg.chat.id, format!("Failed to add to repo: {:?}", compile.status.code())).await?;
}
env::set_current_dir(default_dir)?; env::set_current_dir(default_dir)?;
}
Commands::Clean => {
bot.send_message(msg.chat.id, format!("Done")).await?;
}
Commands::Search(name) => {
let mut packages = aur_rpc::search(format!("{}", name)).await.unwrap();
packages.sort_by(|a, b| b.num_votes.cmp(&a.num_votes));
let mut result = Vec::new();
for (index, package) in packages.iter().enumerate().take(10) { }
result.push(format!("{}. {}", index+1, package.name)); Commands::Search(pkg) => {
} bot.send_message(msg.chat.id, search::search(pkg).await).await?;
bot.send_message(msg.chat.id, format!("{}", result.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("\n"))).await?;
} }
} }
Ok(()) Ok(())