Skip to content

Commit

Permalink
feat(cli/list): implement list --installed command
Browse files Browse the repository at this point in the history
  • Loading branch information
comfysage committed Jan 26, 2024
1 parent 268812c commit d9ee001
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
22 changes: 21 additions & 1 deletion saku-cli/src/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use saku_lib::pkg::data;
use saku_lib::pkg::flask::Flask;
use saku_lib::pkg::pkg::Pkg;
use saku_lib::prelude::*;
use saku_lib::util::msg;
use saku_lib::util::{msg, path};

pub fn list() -> Result<()> {
let flask_files = data::get_flasks()?;
Expand All @@ -21,3 +22,22 @@ pub fn list() -> Result<()> {

Ok(())
}

pub fn list_installed() -> Result<()> {
let dirs = path::get_store_dirs()?;

let pkgs: Vec<Result<Pkg>> = dirs
.iter()
.map(|name| data::get_pkg(&name))
.collect();

for p in &pkgs {
if let Ok(pkg) = p {
println!(" - {} from {}", msg::general::name_f(&pkg.name), msg::general::url_f(&pkg.url));
} else if let Err(err) = p {
warn!("error while listing pkg: {err}");
}
}

Ok(())
}
15 changes: 15 additions & 0 deletions saku-lib/util/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,18 @@ pub fn flasks() -> Result<Vec<String>> {

Ok(files)
}

pub fn get_store_dirs() -> Result<Vec<String>> {
let mut dirs = vec![];
for d in fs::read_dir(&*constants::STORE_DIR)? {
let d = d?;
let d_path_bind = d.path();
let d_path = match d_path_bind.to_str() {
Some(s) => Ok(s),
None => Err(Error::Unexpected),
}?;
let name = filepath::base_name(d_path)?;
dirs.push(name);
}
Ok(dirs)
}
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ fn main() -> Result<()> {
Some(("list", sub_matches)) => {
let flag = sub_matches.get_one::<u8>("installed").ok_or(make_err!())?;
if *flag > 0 {
info!("installed is true");

saku_cli::list::list_installed()?;
return Ok(());
}

Expand Down

0 comments on commit d9ee001

Please sign in to comment.