Skip to content

Commit

Permalink
Merge pull request #28 from srl-labs/execCmd
Browse files Browse the repository at this point in the history
Add exec command setting & mapping
  • Loading branch information
FloSch62 authored Jan 26, 2025
2 parents b1fe6bb + 44b4043 commit b8bc5f5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
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

0 comments on commit b8bc5f5

Please sign in to comment.