Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
feat(arg,deps): fixed spinner on conflict detect // subcommand to che…
Browse files Browse the repository at this point in the history
…ck for installed pkgs
  • Loading branch information
pwnwriter committed Nov 22, 2023
1 parent a1c5b2f commit 6259ee1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::engine::dirs;
use std::fs;
use std::io;
use std::path::Path;
use std::sync::Arc;

pub fn print_installed_pkgs() -> Result<(), anyhow::Error> {
let installed_pkgs = get_installed_pkgs()?;
for pkg in installed_pkgs.iter() {
println!("{}", pkg);
}
Ok(())
}

fn get_installed_pkgs() -> Result<Arc<Vec<String>>, io::Error> {
let bin_dir: &Path = dirs::HYSP_BIN_DIR.as_ref();
let mut binaries = Vec::new();

if let Ok(entries) = fs::read_dir(bin_dir) {
for entry in entries {
if let Ok(entry) = entry {
if let Some(file_name) = entry.file_name().to_str() {
binaries.push(file_name.to_string());
}
}
}
}
Ok(Arc::new(binaries))
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod install;
pub mod list;
pub mod search;
pub mod uninstall;

Expand Down
6 changes: 5 additions & 1 deletion src/engine/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::engine::show_splashes;
use clap::{Args, Parser, Subcommand};

/// The SEREN CLI.
/// The HYSP CLI.
#[derive(Parser)]
#[command(author, version, about = show_splashes(), long_about = show_splashes())]
#[command(propagate_version = true)]
Expand All @@ -24,6 +24,10 @@ pub enum CommandChoice {
#[clap(name = "remove")]
Remove(RemoveArgs),

/// List installed pkgs
#[clap(name = "list")]
List,

/// Search a package
#[command(arg_required_else_help = true)]
#[clap(name = "search")]
Expand Down
44 changes: 29 additions & 15 deletions src/engine/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
use lazy_static::lazy_static;
use std::env;
use std::fs;
use std::path::PathBuf;

pub fn get_xdg_path(key: &str, default: &str) -> PathBuf {
match env::var(key) {
Ok(val) => PathBuf::from(val),
Err(_) => {
let xdg_home = env::var("XDG_DATA_HOME").unwrap_or_else(|_| {
format!("{}/.local/share", env::var("HOME").unwrap_or_default())
});
PathBuf::from(format!("{}/{}", xdg_home, default))
}
}
lazy_static::lazy_static! {
pub static ref HYSP_HOME_DIR: PathBuf = {
let hysp_home = match env::var("HYSP_HOME_DIR") {
Ok(val) => PathBuf::from(val),
Err(_) => {
let xdg_home = env::var("XDG_DATA_HOME").unwrap_or_else(|_| {
format!("{}/.local/share", env::var("HOME").unwrap_or_default())
});
PathBuf::from(format!("{}/{}", xdg_home, "hysp"))
}
};
create_if_not_exists(&hysp_home);
hysp_home
};
pub static ref HYSP_BIN_DIR: PathBuf = {
let bin_dir = HYSP_HOME_DIR.join("bin");
create_if_not_exists(&bin_dir);
bin_dir
};
pub static ref HYSP_DATA_DIR: PathBuf = {
let data_dir = HYSP_HOME_DIR.join("data");
create_if_not_exists(&data_dir);
data_dir
};
}

lazy_static! {
pub static ref HYSP_HOME_DIR: PathBuf = get_xdg_path("HYSP_HOME_DIR", "hysp");
pub static ref HYSP_BIN_DIR: PathBuf = get_xdg_path(" HYSP_BIN_DIR", "hysp/bin");
pub static ref HYSP_DATA_DIR: PathBuf = get_xdg_path("HYSP_DATA_DIR", "hysp/data");
fn create_if_not_exists(dir: &PathBuf) {
if let Err(_) = fs::create_dir_all(&dir) {
println!("Failed to create directory: {:?}", dir);
}
}

#[cfg(test)]
Expand Down
19 changes: 9 additions & 10 deletions src/engine/essentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ pub async fn check_conflicts(install_pkgs: InstallArgs) -> Result<()> {
let pkgname = &install_pkgs.package;

let toml_info = fetch_package_info(pkgname).await?;
let mut spinner_02 = Spinner::new_with_stream(
spinners::Runner,
"Checking for conflicts ...",
Color::Blue,
Streams::Stderr,
);
if let Some(conflicts) = toml_info.package.conditions.and_then(|c| c.conflicts) {
let mut spinner = Spinner::new_with_stream(
spinners::Line,
"Checking for conflicts ...",
Color::Blue,
Streams::Stderr,
);

for conflict_pkg in conflicts {
spinner_02.stop_and_persist("  ", "Done");
if is_pkg_installed(&conflict_pkg) {
spinner.stop_and_persist("  ", "Done");
return Err(anyhow::Error::msg(format!(
"Confliction detected aborting .. {conflict_pkg}"
)));
Expand All @@ -39,7 +38,7 @@ pub async fn check_dependencies(install_pkgs: &InstallArgs) -> Result<()> {

let toml_info = fetch_package_info(pkgname).await?;
if let Some(dependencies) = toml_info.package.conditions.and_then(|c| c.dependencies) {
let mut spinner = Spinner::new_with_stream(
let mut spinner_04 = Spinner::new_with_stream(
spinners::Line,
"Checking for dependencies ...",
Color::Green,
Expand All @@ -48,7 +47,7 @@ pub async fn check_dependencies(install_pkgs: &InstallArgs) -> Result<()> {

for dependency_pkg in dependencies {
if !is_pkg_installed(&dependency_pkg) {
spinner.stop_and_persist("  ", "Done");
spinner_04.stop_and_persist("  ", "Done");
return Err(anyhow::anyhow!("Dependency '{}' not found", dependency_pkg));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub async fn start() {
crate::engine::CommandChoice::Remove(pkg_uninstall_args) => {
crate::commands::uninstall::remove_pkgs(pkg_uninstall_args).await
}

crate::engine::CommandChoice::Search(pkg_search_args) => {
crate::commands::search::fetch_available(pkg_search_args).await
}
crate::engine::CommandChoice::List => crate::commands::list::print_installed_pkgs()
};

if let Err(err) = result {
Expand Down

0 comments on commit 6259ee1

Please sign in to comment.