feat: added search command with --log-metadata (or -l) flag

This commit is contained in:
Byson94
2025-08-22 19:53:15 +05:30
parent 44f82c82d2
commit 157edbe1ca
7 changed files with 101 additions and 26 deletions

View File

@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
### Added
- Support for unix glob patterns in files key (toml).
- **Search** command with `--log-metadata` (or `-l`) flag.
### Changed
@@ -32,11 +33,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
### Added
- **install** flag
- **uninstall** flag
- **update** flag
- **list** flag
- **clear-cache** flag
- **check-update** flag
- **install** command
- **uninstall** command
- **update** command
- **list** command
- **clear-cache** command
- **check-update** command
- **[git2](https://docs.rs/git2/latest/git2/)** based version control
- **Eiipm** not in path warning

View File

@@ -1,13 +1,18 @@
use colored::{Colorize};
use dirs;
use log::{debug, info, trace};
use reqwest::blocking::get;
use log::{info, trace};
use serde::{Deserialize};
use std::env;
use std::error::Error;
use std::fs;
use std::process::Command;
use super::{FileEntry, InstalledPackage, save_db, load_db};
use super::{
FileEntry,
InstalledPackage,
save_db,
load_db,
http_get_string
};
use glob::glob;
use crate::git::{
@@ -112,10 +117,7 @@ pub fn install_package(package_name: &str) -> Result<(), Box<dyn Error>> {
.map(|src_path| {
let tgt = match dest {
Some(d) => target_base_dir.join(d),
None => target_base_dir.join(
src_path.file_name()
.expect("Invalid file name"),
),
None => target_base_dir.join(src)
};
(src_path, tgt)
})
@@ -154,11 +156,3 @@ pub fn install_package(package_name: &str) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn http_get_string(url: &str) -> Result<String, Box<dyn Error>> {
debug!("Sending GET request to {}", url);
let response = get(url)?;
if !response.status().is_success() {
return Err(format!("Failed to fetch URL {}: HTTP {}", url, response.status()).into());
}
Ok(response.text()?)
}

View File

@@ -6,12 +6,14 @@ pub mod clearcache;
pub mod checkupdate;
pub mod listcache;
pub mod purgecache;
pub mod search;
use std::fs;
use std::error::Error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use dirs;
use reqwest::blocking::get;
pub const DB_FILE: &str = ".local/share/eiipm/installed.toml";
@@ -68,3 +70,14 @@ pub fn save_db(db: &PackageDB) -> Result<(), Box<dyn Error>> {
fs::write(db_path, content)?;
Ok(())
}
// Sending requests to url's
pub fn http_get_string(url: &str) -> Result<String, Box<dyn Error>> {
log::debug!("Sending GET request to {}", url);
let response = get(url)?;
if !response.status().is_success() {
return Err(format!("Failed to fetch URL {}: HTTP {}", url, response.status()).into());
}
Ok(response.text()?)
}

47
src/functions/search.rs Normal file
View File

@@ -0,0 +1,47 @@
use super::http_get_string;
use log::info;
use std::error::Error;
use colored::Colorize;
use crate::opts::SearchArgs;
pub fn search_package(
package_name: &str,
flags: SearchArgs
) -> Result<(), Box<dyn Error>> {
info!("> Searching for '{}'", package_name.yellow().bold());
let raw_manifest_url = format!(
"https://raw.githubusercontent.com/Ewwii-sh/eii-manifests/main/manifests/{}.toml",
package_name
);
if let Ok(response) = http_get_string(&raw_manifest_url) {
info!("{}", format!(
"\nPackage with name '{}' is found in eii-manifests!",
package_name
).green());
if flags.log_metadata {
info!("{}", format!(
"\n--- Metadata for '{}' ---\n{}",
package_name.cyan().bold(),
indent_lines(&response, 4)
));
}
} else {
info!("{}", format!(
"Package '{}' not found.",
package_name.red().bold()
));
}
Ok(())
}
fn indent_lines(s: &str, spaces: usize) -> String {
let padding = " ".repeat(spaces);
s.lines()
.map(|line| format!("{}{}", padding, line))
.collect::<Vec<_>>()
.join("\n")
}

View File

@@ -137,10 +137,7 @@ fn update_file(pkg: &mut InstalledPackage, package_name: &str) -> Result<(), Box
.map(|src_path| {
let tgt = match dest {
Some(d) => target_base_dir.join(d),
None => target_base_dir.join(
src_path.file_name()
.expect("Invalid file name"),
),
None => target_base_dir.join(src)
};
(src_path, tgt)
})

View File

@@ -13,6 +13,7 @@ use functions::{
checkupdate::check_package_updates,
listcache::list_all_cache,
purgecache::purge_cache,
search::search_package,
};
use other::{
confirm_action::confirm,
@@ -110,6 +111,11 @@ fn main() {
error!("Error purging cache: {}", e);
}
}
Commands::Search { package, flags } => {
if let Err(e) = search_package(&package, flags) {
error!("Error searching for '{}'. Error: {}", package, e);
}
}
}
check_eiipm_in_path();

View File

@@ -64,6 +64,16 @@ pub enum Commands {
/// Remove all broken/orphaned packages
#[command(alias = "pc")]
PurgeCache,
/// Search for a package in eii-manifests
#[command(alias = "s")]
Search {
/// Name of the package to search for
package: String,
#[command(flatten)]
flags: SearchArgs
},
}
#[derive(Args, Debug)]
@@ -86,4 +96,11 @@ pub struct ClearCacheArgs {
/// Bypass all confirmation
#[arg(long, short)]
pub force: bool,
}
}
#[derive(Args, Debug)]
pub struct SearchArgs {
/// Log the metadata of the searched file
#[arg(long, short)]
pub log_metadata: bool,
}