Skip to content

Commit

Permalink
feat: cli command for listing backends (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
roele authored Apr 29, 2024
1 parent ad9f803 commit 5f8a3ce
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
41 changes: 41 additions & 0 deletions src/cli/backends/ls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use eyre::Result;

use crate::forge::{self, ForgeType};

/// List built-in backends
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "list", after_long_help = AFTER_LONG_HELP, verbatim_doc_comment)]
pub struct BackendsLs {}

impl BackendsLs {
pub fn run(self) -> Result<()> {
let mut forges = forge::list_forge_types();
forges.retain(|f| *f != ForgeType::Asdf);

for forge in forges {
miseprintln!("{}", forge);
}
Ok(())
}
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>mise backends ls</bold>
cargo
go
npm
pipx
ubi
"#
);

#[cfg(test)]
mod tests {

#[test]
fn test_backends_list() {
assert_cli_snapshot!("backends", "list");
}
}
32 changes: 32 additions & 0 deletions src/cli/backends/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use clap::Subcommand;
use eyre::Result;

mod ls;

#[derive(Debug, clap::Args)]
#[clap(about = "Manage backends", visible_alias = "b", aliases = ["backend", "backend-list"])]
pub struct Backends {
#[clap(subcommand)]
command: Option<Commands>,
}

#[derive(Debug, Subcommand)]
enum Commands {
Ls(ls::BackendsLs),
}

impl Commands {
pub fn run(self) -> Result<()> {
match self {
Self::Ls(cmd) => cmd.run(),
}
}
}

impl Backends {
pub fn run(self) -> Result<()> {
let cmd = self.command.unwrap_or(Commands::Ls(ls::BackendsLs {}));

cmd.run()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: src/cli/backends/ls.rs
expression: output
---
cargo
go
npm
pipx
ubi
15 changes: 14 additions & 1 deletion src/cli/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::cli::version;
use crate::cli::version::VERSION;
use crate::config::{Config, Settings};
use crate::file::display_path;
use crate::forge::ForgeType;
use crate::git::Git;
use crate::plugins::core::CORE_PLUGINS;
use crate::plugins::PluginType;
Expand Down Expand Up @@ -100,6 +101,7 @@ impl Doctor {
let config = config.as_ref();

section("config_files", render_config_files(config))?;
section("backends", render_backends())?;
section("plugins", render_plugins())?;

for plugin in forge::list() {
Expand Down Expand Up @@ -239,11 +241,22 @@ fn render_config_files(config: &Config) -> String {
.join("\n")
}

fn render_backends() -> String {
let mut s = vec![];
let backends = forge::list_forge_types()
.into_iter()
.filter(|f| *f != ForgeType::Asdf);
for b in backends {
s.push(format!("{}", b));
}
s.join("\n")
}

fn render_plugins() -> String {
let mut s = vec![];
let plugins = forge::list()
.into_iter()
.filter(|p| p.is_installed())
.filter(|p| p.is_installed() && p.get_type() == ForgeType::Asdf)
.collect::<Vec<_>>();
let max_plugin_name_len = plugins
.iter()
Expand Down
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod activate;
mod alias;
pub mod args;
mod asdf;
pub mod backends;
mod bin_paths;
mod cache;
mod completion;
Expand Down Expand Up @@ -62,6 +63,7 @@ pub enum Commands {
Activate(activate::Activate),
Alias(alias::Alias),
Asdf(asdf::Asdf),
Backends(backends::Backends),
BinPaths(bin_paths::BinPaths),
Cache(cache::Cache),
Completion(completion::Completion),
Expand Down Expand Up @@ -117,6 +119,7 @@ impl Commands {
Self::Activate(cmd) => cmd.run(),
Self::Alias(cmd) => cmd.run(),
Self::Asdf(cmd) => cmd.run(),
Self::Backends(cmd) => cmd.run(),
Self::BinPaths(cmd) => cmd.run(),
Self::Cache(cmd) => cmd.run(),
Self::Completion(cmd) => cmd.run(),
Expand Down
17 changes: 15 additions & 2 deletions src/forge/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use std::fmt::{Debug, Display};
use std::fmt::{Debug, Display, Formatter};
use std::fs::File;
use std::hash::Hash;
use std::path::{Path, PathBuf};
Expand All @@ -12,6 +12,7 @@ use eyre::WrapErr;
use itertools::Itertools;
use rayon::prelude::*;
use regex::Regex;
use strum::IntoEnumIterator;
use versions::Versioning;

use crate::cli::args::ForgeArg;
Expand Down Expand Up @@ -41,7 +42,9 @@ pub type AForge = Arc<dyn Forge>;
pub type ForgeMap = BTreeMap<ForgeArg, AForge>;
pub type ForgeList = Vec<AForge>;

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, EnumString, AsRefStr, Ord, PartialOrd)]
#[derive(
Debug, PartialEq, Eq, Hash, Clone, Copy, EnumString, EnumIter, AsRefStr, Ord, PartialOrd,
)]
#[strum(serialize_all = "snake_case")]
pub enum ForgeType {
Asdf,
Expand All @@ -52,6 +55,12 @@ pub enum ForgeType {
Ubi,
}

impl Display for ForgeType {
fn fmt(&self, formatter: &mut Formatter) -> std::fmt::Result {
write!(formatter, "{}", format!("{:?}", self).to_lowercase())
}
}

static FORGES: Mutex<Option<ForgeMap>> = Mutex::new(None);

fn load_forges() -> ForgeMap {
Expand Down Expand Up @@ -95,6 +104,10 @@ pub fn list() -> ForgeList {
load_forges().values().cloned().collect()
}

pub fn list_forge_types() -> Vec<ForgeType> {
ForgeType::iter().collect()
}

pub fn get(fa: &ForgeArg) -> AForge {
if let Some(forge) = load_forges().get(fa) {
forge.clone()
Expand Down

0 comments on commit 5f8a3ce

Please sign in to comment.