Skip to content
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

Add exec command setting & mapping #28

Merged
merged 2 commits into from
Jan 26, 2025
Merged
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
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@
"type": "number",
"default": 10000,
"description": "Refresh interval (in milliseconds) for the Containerlab Explorer."
},
"containerlab.node.execCommandMapping": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"default": {},
"markdownDescription": "Change the default exec action for node when using the 'attach' command. Enter in the mapping between the kind and command.\n\nFor example: `{\"nokia_srlinux\": \"sr_cli\"}` means that `docker exec -it <container> sr_cli` will be executed if `<container>` is the `nokia_srlinux` kind."
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions resources/exec_cmd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"nokia_srlinux": "sr_cli",
"cisco_xrd": "/pkg/bin/xr_cli.sh"
}
22 changes: 16 additions & 6 deletions src/commands/attachShell.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import * as vscode from "vscode";
import { execCommandInTerminal } from "./command";
import { ContainerlabNode } from "../containerlabTreeDataProvider";
import { execCmdMapping } from "../extension";

export function attachShell(node: ContainerlabNode) {
if (!node) {
vscode.window.showErrorMessage('No container node selected.');
return;
}

const containerId = node.details?.containerId;
const nodeDetails = node.details;

if(!nodeDetails) { return vscode.window.showErrorMessage("Couldn't fetch node details");}

const containerId = nodeDetails.containerId;
const containerKind = nodeDetails.kind;
const containerLabel = node.label || "Container";

if (!containerId) {
vscode.window.showErrorMessage('No containerId for shell attach.');
return;
}
if (!containerId) { return vscode.window.showErrorMessage('No containerId for shell attach.');}
if (!containerKind) { return vscode.window.showErrorMessage('No container kind for shell attach.');}

let execCmd = execCmdMapping[containerKind] || "sh";

// Use the sudoEnabledByDefault setting
const config = vscode.workspace.getConfiguration("containerlab");
const useSudo = config.get<boolean>("sudoEnabledByDefault", true);

const userExecMapping = config.get("node.execCommandMapping") as { [key: string]: string };

execCmd = userExecMapping[containerKind] || execCmd;

execCommandInTerminal(
`${useSudo ? "sudo " : ""}docker exec -it ${containerId} sh`,
`${useSudo ? "sudo " : ""}docker exec -it ${containerId} ${execCmd}`,
`Shell - ${containerLabel}`
);
}
4 changes: 3 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import {

export let outputChannel: vscode.OutputChannel;
const execAsync = promisify(exec);
export const execCmdMapping = require('../resources/exec_cmd.json');

console.log(execCmdMapping);

export async function activate(context: vscode.ExtensionContext) {
outputChannel = vscode.window.createOutputChannel("Containerlab");
Expand All @@ -55,7 +58,6 @@ export async function activate(context: vscode.ExtensionContext) {
}
versionOutput = '';
}


const provider = new ContainerlabTreeDataProvider(context);
vscode.window.registerTreeDataProvider('containerlabExplorer', provider);
Expand Down