Skip to content
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
1 change: 1 addition & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export { configureCacheCommand } from "./cache";
export { configureSignCommand } from "./sign";
export { configureReportCommand } from "./report";
export { configureInspectCommand } from "./inspect";
export { configureSetCommand } from "./set";

// API v2 migration commands
export { configureYankCommand } from "./yank";
Expand Down
41 changes: 41 additions & 0 deletions packages/cli/src/commands/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,47 @@ function displayResult(result: ExecutionResult, options: RunOptions): void {
* Run command handler
*/
async function runHandler(tool: string, options: RunOptions, ctx: CommandContext): Promise<void> {
// === Active toolset restriction logic (global ~/.enact/tools.json) ===
// Only applies if resolving by name (not by path)
const isPath =
tool.startsWith("/") ||
tool.startsWith("./") ||
tool.startsWith("../") ||
tool.includes("\\") ||
existsSync(tool);

if (!isPath && !options.remote) {
try {
const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const homeDir = os.homedir();
const toolsJsonPath = path.join(homeDir, ".enact", "tools.json");
if (fs.existsSync(toolsJsonPath)) {
const toolsJson = JSON.parse(fs.readFileSync(toolsJsonPath, "utf-8"));
const activeToolset = toolsJson.activeToolset;
if (
activeToolset &&
toolsJson.toolsets &&
Array.isArray(toolsJson.toolsets[activeToolset])
) {
const allowedTools = toolsJson.toolsets[activeToolset].map((t: string) =>
t.toLowerCase().replace(/\\/g, "/").trim()
);
const requestedTool = tool.toLowerCase().replace(/\\/g, "/").trim();
if (!allowedTools.includes(requestedTool)) {
error(
`Tool '${tool}' is not in the active toolset '${activeToolset}'. Allowed: ${allowedTools.join(", ")}`
);
process.exit(EXIT_EXECUTION_ERROR);
}
}
}
} catch (_err) {
// Ignore errors in toolset restriction logic, fallback to normal resolution
}
}

let resolution: ToolResolution | null = null;
let resolveResult: ReturnType<typeof tryResolveToolDetailed> | null = null;

Expand Down
48 changes: 40 additions & 8 deletions packages/cli/src/commands/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ interface LocalToolInfo {
version: string;
description: string;
location: string;
scope: "project" | "global";
scope: "project" | "global" | "toolset";
}

/**
* Search installed tools locally
*/
function searchLocalTools(
query: string,
scope: "project" | "global",
scope: "project" | "global" | "toolset",
cwd: string
): LocalToolInfo[] {
const tools: LocalToolInfo[] = [];
const queryLower = query.toLowerCase();

if (scope === "global") {
if (scope === "global" || scope === "toolset") {
// Search global tools via tools.json
const installedTools = listInstalledTools("global");

Expand All @@ -109,7 +109,7 @@ function searchLocalTools(
version: tool.version,
description: loaded?.manifest.description ?? "-",
location: tool.cachePath,
scope: "global",
scope: scope,
});
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ function searchLocalTools(
version: manifest.version ?? "-",
description: manifest.description ?? "-",
location: entryPath,
scope: "project",
scope: scope,
});
}
} else {
Expand All @@ -177,9 +177,41 @@ async function searchHandler(
ctx: CommandContext
): Promise<void> {
// Handle local search (--local or -g)
if (options.local || options.global) {
const scope = options.global ? "global" : "project";
const results = searchLocalTools(query, scope, ctx.cwd);
let activeToolsetAvailable = false;
let restrictToToolset: string[] | undefined = undefined;

try {
const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const homeDir = os.homedir();
const toolsJsonPath = path.join(homeDir, ".enact", "tools.json");
if (fs.existsSync(toolsJsonPath)) {
const toolsJson = JSON.parse(fs.readFileSync(toolsJsonPath, "utf-8"));
const activeToolset = toolsJson.activeToolset;
if (activeToolset && toolsJson.toolsets && Array.isArray(toolsJson.toolsets[activeToolset])) {
restrictToToolset = toolsJson.toolsets[activeToolset].map((t: string) =>
t.toLowerCase().replace(/\\/g, "/").trim()
);
activeToolsetAvailable = true;
}
}
} catch (_err) {
// Ignore errors on loading toolsets for filter
}

if (options.local || options.global || activeToolsetAvailable) {
let scope: "project" | "global" | "toolset" = options.global ? "global" : "project";
if (activeToolsetAvailable) {
scope = "toolset";
}

let results = searchLocalTools(query, scope, ctx.cwd);
if (restrictToToolset) {
results = results.filter((tool) =>
restrictToToolset!.includes(tool.name.toLowerCase().replace(/\\/g, "/").trim())
);
}

// JSON output
if (options.json) {
Expand Down
59 changes: 59 additions & 0 deletions packages/cli/src/commands/set/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* enact set command
*
* Set the active toolset by name in ~/.enact/tools.json
*/

import type { Command } from "commander";
import { error, success } from "../../utils";

/**
* Handler for the set command
*/
async function setHandler(toolset: string): Promise<void> {
try {
const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const homeDir = os.homedir();
const toolsJsonPath = path.join(homeDir, ".enact", "tools.json");

if (!fs.existsSync(toolsJsonPath)) {
error(`tools.json not found at ${toolsJsonPath}`);
process.exit(1);
}

const toolsJson = JSON.parse(fs.readFileSync(toolsJsonPath, "utf-8"));

if (toolset === "NONE") {
toolsJson.activeToolset = undefined;
fs.writeFileSync(toolsJsonPath, JSON.stringify(toolsJson, null, 2));
success(`Active toolset unset in ${toolsJsonPath}`);
return;
}

if (!toolsJson.toolsets || !toolsJson.toolsets[toolset]) {
error(`Toolset '${toolset}' not found in tools.json`);
process.exit(1);
}

toolsJson.activeToolset = toolset;
fs.writeFileSync(toolsJsonPath, JSON.stringify(toolsJson, null, 2));
success(`Active toolset set to '${toolset}' in ${toolsJsonPath}`);
} catch (err) {
error(`Failed to set active toolset: ${err}`);
process.exit(1);
}
}

/**
* Configure the set command
*/
export function configureSetCommand(program: Command): void {
program
.command("set <toolset>")
.description(
"Set the active toolset by name (writes to ~/.enact/tools.json). Use 'NONE' to unset."
)
.action(setHandler);
}
5 changes: 5 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
configureReportCommand,
configureRunCommand,
configureSearchCommand,
configureSetCommand,
configureSetupCommand,
configureSignCommand,
configureTrustCommand,
Expand Down Expand Up @@ -54,6 +55,7 @@ async function main() {
.version(version, "-v, --version", "output the version number");

// Configure all commands

configureSetupCommand(program);
configureInitCommand(program);
configureRunCommand(program);
Expand Down Expand Up @@ -86,6 +88,9 @@ async function main() {
// MCP integration commands
configureMcpCommand(program);

// Set active toolset command
configureSetCommand(program);

// Validation command
configureValidateCommand(program);

Expand Down