Skip to content

Commit

Permalink
Merge pull request #62 from srl-labs/save
Browse files Browse the repository at this point in the history
add save commands for lab and node configurations
  • Loading branch information
FloSch62 authored Feb 9, 2025
2 parents 71ebaaf + d2b01b7 commit 4c1bcdf
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
26 changes: 23 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"icon": "resources/containerlab.png",
"description": "Manages containerlab topologies in VS Code",
"version": "0.6.4",
"homepage": "https://containerlab.dev/manual/vsc-extension/",
"engines": {
"vscode": "^1.70.0"
},
Expand Down Expand Up @@ -109,6 +110,11 @@
"icon": "$(trash)",
"category": "Containerlab"
},
{
"command": "containerlab.lab.save",
"title": "Save Lab",
"category": "Containerlab"
},
{
"command": "containerlab.inspectAll",
"title": "Inspect (All Labs)",
Expand Down Expand Up @@ -161,6 +167,10 @@
"command": "containerlab.node.stop",
"title": "Stop node"
},
{
"command": "containerlab.node.save",
"title": "Save node"
},
{
"command": "containerlab.node.attachShell",
"title": "Attach shell",
Expand Down Expand Up @@ -386,10 +396,15 @@
"when": "viewItem == containerlabLabDeployed",
"group": "navigation@6"
},
{
"command": "containerlab.lab.save",
"when": "viewItem =~ /containerlabLab/",
"group": "navigation@7"
},
{
"command": "containerlab.inspectOneLab",
"when": "viewItem == containerlabLabDeployed",
"group": "navigation@7"
"group": "navigation@8"
},
{
"command": "containerlab.lab.graph",
Expand Down Expand Up @@ -421,6 +436,11 @@
"when": "viewItem == containerlabContainer",
"group": "nodeNavigation@3"
},
{
"command": "containerlab.node.save",
"when": "viewItem == containerlabContainer",
"group": "nodeNavigation@4"
},
{
"command": "containerlab.node.attachShell",
"when": "viewItem == containerlabContainer",
Expand Down Expand Up @@ -449,12 +469,12 @@
{
"command": "containerlab.node.showLogs",
"when": "viewItem == containerlabContainer",
"group": "nodeNavigation@4"
"group": "nodeNavigation@5"
},
{
"command": "containerlab.node.manageImpairments",
"when": "viewItem == false",
"group": "nodeNavigation@5"
"group": "nodeNavigation@6"
},
{
"submenu": "containerlab.submenu.node.copy",
Expand Down
2 changes: 2 additions & 0 deletions src/clabTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class ClabContainerTreeNode extends vscode.TreeItem {
public readonly kind: string,
public readonly image: string,
public readonly interfaces: ClabInterfaceTreeNode[],
public readonly labPath: LabPath,
public readonly v4Address?: string,
public readonly v6Address?: string,
contextValue?: string,
Expand Down Expand Up @@ -412,6 +413,7 @@ export class ClabTreeDataProvider implements vscode.TreeDataProvider<ClabLabTree
container.kind,
container.image,
interfaces,
{ absolute: absLabPath, relative: utils.getRelLabFolderPath(labPath) },
container.ipv4_address,
container.ipv6_address,
"containerlabContainer"
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./command";
export * from "./deploy";
export * from "./destroy";
export * from "./redeploy";
export * from "./save";
export * from "./openLabFile";
export * from "./startNode";
export * from "./stopNode";
Expand Down
72 changes: 72 additions & 0 deletions src/commands/save.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// src/commands/save.ts
import * as vscode from "vscode";
import { SpinnerMsg } from "./command";
import { ClabCommand } from "./clabCommand";
import { ClabLabTreeNode, ClabContainerTreeNode } from "../clabTreeDataProvider";
import * as path from "path";

/**
* Save the entire lab configuration.
* Executes: containerlab -t <labPath> save
*/
export async function saveLab(node: ClabLabTreeNode) {
if (!node) {
vscode.window.showErrorMessage("No lab node selected.");
return;
}
const labPath = node.labPath && node.labPath.absolute;
if (!labPath) {
vscode.window.showErrorMessage("No labPath found for the lab.");
return;
}

const spinnerMessages: SpinnerMsg = {
progressMsg: `Saving lab configuration for ${node.label}...`,
successMsg: `Lab configuration for ${node.label} saved successfully!`,
failMsg: `Could not save lab configuration for ${node.label}`
};

// Create a ClabCommand for "save" using the lab node.
const saveCmd = new ClabCommand("save", node, spinnerMessages);
// ClabCommand automatically appends "-t <labPath>".
saveCmd.run();
}

/**
* Save the configuration for a specific container node.
* Executes: containerlab -t <labPath> save --node-filter <shortNodeName>
*/
export async function saveNode(node: ClabContainerTreeNode) {
if (!node) {
vscode.window.showErrorMessage("No container node selected.");
return;
}

if (!node.labPath || !node.labPath.absolute) {
vscode.window.showErrorMessage("Error: Could not determine lab path for this node.");
return;
}

// Extract the short node name by removing the "clab-{labname}-" prefix
const shortNodeName = node.name.replace(/^clab-[^-]+-/, '');

const spinnerMessages: SpinnerMsg = {
progressMsg: `Saving configuration for node ${shortNodeName}...`,
successMsg: `Configuration for node ${shortNodeName} saved successfully!`,
failMsg: `Could not save configuration for node ${shortNodeName}`
};

const tempLabNode = new ClabLabTreeNode(
path.basename(node.labPath.absolute),
vscode.TreeItemCollapsibleState.None,
node.labPath,
undefined,
undefined,
undefined,
"containerlabLabDeployed"
);

const saveCmd = new ClabCommand("save", tempLabNode, spinnerMessages);
// Use --node-filter instead of -n and use the short name
saveCmd.run(["--node-filter", shortNodeName]);
}
10 changes: 9 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('containerlab.lab.destroy.cleanup', cmd.destroyCleanup)
);

// Lab save command
context.subscriptions.push(
vscode.commands.registerCommand('containerlab.lab.save', cmd.saveLab)
);

// Lab inspection commands
context.subscriptions.push(
vscode.commands.registerCommand('containerlab.inspectAll', () =>
Expand Down Expand Up @@ -148,6 +153,9 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('containerlab.node.stop', cmd.stopNode)
);
context.subscriptions.push(
vscode.commands.registerCommand('containerlab.node.save', cmd.saveNode)
);
context.subscriptions.push(
vscode.commands.registerCommand('containerlab.node.attachShell', cmd.attachShell)
);
Expand Down Expand Up @@ -196,7 +204,7 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand(
'containerlab.interface.captureWithEdgeshark',
(clickedNode, allSelectedNodes) => {
cmd.captureInterfaceWithPacketflix(clickedNode, allSelectedNodes); // [CHANGED]
cmd.captureInterfaceWithPacketflix(clickedNode, allSelectedNodes);
}
)
);
Expand Down
Binary file modified vscode-containerlab-0.6.4.vsix
Binary file not shown.

0 comments on commit 4c1bcdf

Please sign in to comment.