Skip to content

feat: new "enable-tools" option #4784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions e2e/config/test_config_enable_tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

mkdir foo
cat <<EOF >mise.toml
[tools]
dummy = 'latest'

[settings]
enable_tools = ["foo"]
EOF

assert_not_contains "mise tool dummy 2>&1" "Config Source"
8 changes: 8 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@
}
}
},
"enable_tools": {
"default": [],
"description": "Tools defined in mise.toml that should be used - all other tools are ignored",
"type": "array",
"items": {
"type": "string"
}
},
"env": {
"default": [],
"description": "Env to use for mise.<MISE_ENV>.toml files.",
Expand Down
8 changes: 8 additions & 0 deletions settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ By default, mise will use the [nuget](https://api.nuget.org/v3/index.json) API t
However, you can set this to a different URL if you have a custom feed or want to use a different source.
"""

[enable_tools]
env = "MISE_ENABLE_TOOLS"
type = "ListString"
rust_type = "BTreeSet<String>"
default = []
parse_env = "list_by_comma"
description = "Tools defined in mise.toml that should be used - all other tools are ignored"

[env]
env = "MISE_ENV"
type = "ListString"
Expand Down
10 changes: 8 additions & 2 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::file::{display_path, remove_all, remove_all_with_warning};
use crate::install_context::InstallContext;
use crate::plugins::core::CORE_PLUGINS;
use crate::plugins::{Plugin, PluginType, VERSION_REGEX};
use crate::registry::REGISTRY;
use crate::registry::{REGISTRY, tool_disabled};
use crate::runtime_symlinks::is_runtime_symlink;
use crate::toolset::outdated_info::OutdatedInfo;
use crate::toolset::{ToolRequest, ToolVersion, Toolset, install_state, is_outdated_version};
Expand Down Expand Up @@ -76,7 +76,13 @@ fn load_tools() -> Arc<BackendMap> {
.flat_map(|ist| arg_to_backend(ist.clone().into())),
);
time!("load_tools install_state");
tools.retain(|backend| !SETTINGS.disable_tools().contains(backend.id()));
tools.retain(|backend| {
!tool_disabled(
&SETTINGS.enable_tools(),
&SETTINGS.disable_tools(),
&backend.id().to_string(),
)
});
tools.retain(|backend| {
!SETTINGS
.disable_backends
Expand Down
14 changes: 11 additions & 3 deletions src/cli/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::backend::backend_type::BackendType;
use crate::config::SETTINGS;
use crate::registry::{REGISTRY, RegistryTool};
use crate::registry::{REGISTRY, RegistryTool, tool_disabled};
use crate::ui::table::MiseTable;
use eyre::{Result, bail};
use itertools::Itertools;
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Registry {
let mut table = MiseTable::new(false, &["Tool", "Backends"]);
let data = REGISTRY
.iter()
.filter(|(short, _)| !SETTINGS.disable_tools.contains(**short))
.filter(|(short, _)| filter_disabled(short))
.filter(|(short, rt)| !self.hide_aliased || **short == rt.short)
.map(|(short, rt)| (short.to_string(), filter_backend(rt).join(" ")))
.filter(|(_, backends)| !backends.is_empty())
Expand All @@ -76,7 +76,7 @@ impl Registry {
fn complete(&self) -> Result<()> {
REGISTRY
.iter()
.filter(|(short, _)| !SETTINGS.disable_tools.contains(**short))
.filter(|(short, _)| filter_disabled(short))
.filter(|(short, rt)| !self.hide_aliased || **short == rt.short)
.map(|(short, rt)| {
(
Expand Down Expand Up @@ -110,3 +110,11 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(
asdf:mise-plugins/mise-poetry
"#
);

fn filter_disabled(short: &str) -> bool {
tool_disabled(
&SETTINGS.enable_tools,
&SETTINGS.disable_tools,
&short.to_string(),
)
}
7 changes: 7 additions & 0 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ impl Settings {
.collect()
}

pub fn enable_tools(&self) -> BTreeSet<String> {
self.enable_tools
.iter()
.map(|t| t.trim().to_string())
.collect()
}

pub fn partial_as_dict(partial: &SettingsPartial) -> eyre::Result<toml::Table> {
let s = toml::to_string(partial)?;
let table = toml::from_str(&s)?;
Expand Down
4 changes: 2 additions & 2 deletions src/lockfile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::{Config, SETTINGS};
use crate::file;
use crate::file::display_path;
use crate::registry::REGISTRY;
use crate::registry::{REGISTRY, tool_disabled};
use crate::toolset::{ToolSource, ToolVersion, ToolVersionList, Toolset};
use eyre::{Report, Result, bail};
use itertools::Itertools;
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn update_lockfiles(config: &Config, ts: &Toolset, new_versions: &[ToolVersi
// * tools inside a parent config but are overridden by a child config (we just keep what was in the lockfile before, if anything)
existing_lockfile.tools.retain(|k, _| {
all_tool_names.contains(k)
|| SETTINGS.disable_tools().contains(k)
|| tool_disabled(&SETTINGS.enable_tools(), &SETTINGS.disable_tools(), k)
|| REGISTRY
.get(&k.as_str())
.is_some_and(|rt| !rt.is_supported_os())
Expand Down
35 changes: 34 additions & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::backend::backend_type::BackendType;
use crate::cli::args::BackendArg;
use crate::config::SETTINGS;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::env::consts::{ARCH, OS};
use std::fmt::Display;
use std::iter::Iterator;
Expand Down Expand Up @@ -137,3 +137,36 @@ impl Display for RegistryTool {
write!(f, "{}", self.short)
}
}

pub fn tool_disabled<T: Ord>(
enable_tools: &BTreeSet<T>,
disable_tools: &BTreeSet<T>,
name: &T,
) -> bool {
if enable_tools.is_empty() {
disable_tools.contains(name)
} else {
!enable_tools.contains(name)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_tool_disabled() {
use super::*;
let name = "cargo";

assert!(!tool_disabled(&BTreeSet::new(), &BTreeSet::new(), &name));
assert!(!tool_disabled(
&BTreeSet::from(["cargo"]),
&BTreeSet::new(),
&name
));
assert!(tool_disabled(
&BTreeSet::new(),
&BTreeSet::from(["cargo"]),
&name
));
}
}
8 changes: 7 additions & 1 deletion src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::errors::Error;
use crate::hooks::Hooks;
use crate::install_context::InstallContext;
use crate::path_env::PathEnv;
use crate::registry::tool_disabled;
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::uv::UV_VENV;
use crate::{backend, config, env, hooks};
Expand Down Expand Up @@ -703,7 +704,12 @@ impl Toolset {
}

fn is_disabled(&self, ba: &BackendArg) -> bool {
!ba.is_os_supported() || SETTINGS.disable_tools().contains(&ba.short)
!ba.is_os_supported()
|| tool_disabled(
&SETTINGS.enable_tools(),
&SETTINGS.disable_tools(),
&ba.short.to_string(),
)
}

fn load_post_env(
Expand Down
7 changes: 5 additions & 2 deletions src/toolset/tool_request_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::backend::backend_type::BackendType;
use crate::cli::args::{BackendArg, ToolArg};
use crate::config::{Config, Settings};
use crate::env;
use crate::registry::REGISTRY;
use crate::registry::{REGISTRY, tool_disabled};
use crate::toolset::{ToolRequest, ToolSource, Toolset};
use indexmap::IndexMap;
use itertools::Itertools;
Expand Down Expand Up @@ -127,13 +127,16 @@ pub struct ToolRequestSetBuilder {
default_to_latest: bool,
/// tools which will be disabled
disable_tools: BTreeSet<BackendArg>,
/// tools which will be enabled
enable_tools: BTreeSet<BackendArg>,
}

impl ToolRequestSetBuilder {
pub fn new() -> Self {
let settings = Settings::get();
Self {
disable_tools: settings.disable_tools().iter().map(|s| s.into()).collect(),
enable_tools: settings.enable_tools().iter().map(|s| s.into()).collect(),
..Default::default()
}
}
Expand Down Expand Up @@ -172,7 +175,7 @@ impl ToolRequestSetBuilder {
backend_type == BackendType::Unknown
|| (cfg!(windows) && backend_type == BackendType::Asdf)
|| !ba.is_os_supported()
|| self.disable_tools.contains(ba)
|| tool_disabled(&self.enable_tools, &self.disable_tools, ba)
}

fn load_config_files(&self, trs: &mut ToolRequestSet) -> eyre::Result<()> {
Expand Down
Loading