Skip to content

Commit

Permalink
Implement list command (#27)
Browse files Browse the repository at this point in the history
* Implement list command

* Sort tools

Co-authored-by: Lucien Greathouse <me@lpghatguy.com>
  • Loading branch information
nezuo and LPGhatguy committed Aug 12, 2022
1 parent 20aad7c commit e09e908
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::env::current_dir;
use std::path::PathBuf;

Expand All @@ -9,7 +10,7 @@ use crate::manifest::Manifest;
use crate::tool_alias::ToolAlias;
use crate::tool_name::ToolName;
use crate::tool_spec::ToolSpec;
use crate::tool_storage::ToolStorage;
use crate::tool_storage::{InstalledToolsCache, ToolStorage};
use crate::trust::{TrustCache, TrustMode};

#[derive(Debug, StructOpt)]
Expand All @@ -22,12 +23,12 @@ impl Args {
pub fn run(self, home: &Home, tools: ToolStorage) -> anyhow::Result<()> {
match self.subcommand {
Subcommand::Init(sub) => sub.run(),
Subcommand::List(sub) => sub.run(home),
Subcommand::Add(sub) => sub.run(tools),
Subcommand::Install(sub) => sub.run(tools),
Subcommand::Trust(sub) => sub.run(home),
Subcommand::SelfInstall(sub) => sub.run(home, tools),

Subcommand::List(_) => bail!("This command is not yet implemented."),
Subcommand::Update(_) => bail!("This command is not yet implemented."),
Subcommand::SelfUpdate(_) => bail!("This command is not yet implemented."),
}
Expand Down Expand Up @@ -71,6 +72,39 @@ impl InitSubcommand {
#[derive(Debug, StructOpt)]
pub struct ListSubcommand {}

impl ListSubcommand {
pub fn run(self, home: &Home) -> anyhow::Result<()> {
let installed_path = home.path().join("tool-storage").join("installed.txt");
let installed = InstalledToolsCache::read(&installed_path)?;

let mut tools = BTreeMap::new();

for tool in installed.tools {
tools
.entry(tool.name().to_string())
.or_insert(Vec::new())
.push(tool.version().clone());
}

for (tool, mut versions) in tools {
println!("{tool}");

versions.sort();
versions.reverse();

let versions = versions
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ");

println!(" {versions}");
}

Ok(())
}
}

/// Adds a new tool to Aftman and installs it.
#[derive(Debug, StructOpt)]
pub struct AddSubcommand {
Expand Down

0 comments on commit e09e908

Please sign in to comment.