Skip to content

Commit

Permalink
Merge pull request #10 from yacoman89/compose-fixes
Browse files Browse the repository at this point in the history
compose groups show and compose commands run
  • Loading branch information
dreamcatcher45 authored Nov 8, 2024
2 parents 82bd06a + ccb32ab commit 934b20e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
"title": "Compose Up",
"icon": "$(arrow-up)"
},
{
"command": "podmanager.composeDown",
"title": "Compose Down",
"icon": "$(trash)"
},
{
"command": "podmanager.composeStart",
"title": "Compose Start",
Expand All @@ -128,11 +133,6 @@
"title": "Compose Restart",
"icon": "$(refresh)"
},
{
"command": "podmanager.composeDown",
"title": "Compose Down",
"icon": "$(trash)"
},
{
"command": "podmanager.refreshOverview",
"title": "Refresh Podman Overview",
Expand Down Expand Up @@ -289,22 +289,27 @@
"group": "inline"
},
{
"command": "podmanager.composeStart",
"command": "podmanager.composeUp",
"when": "view == podmanView && viewItem == compose-group",
"group": "inline"
},
{
"command": "podmanager.composeStop",
"command": "podmanager.composeDown",
"when": "view == podmanView && viewItem == compose-group",
"group": "inline"
},
{
"command": "podmanager.composeRestart",
"command": "podmanager.composeStart",
"when": "view == podmanView && viewItem == compose-group",
"group": "inline"
},
{
"command": "podmanager.composeDown",
"command": "podmanager.composeStop",
"when": "view == podmanView && viewItem == compose-group",
"group": "inline"
},
{
"command": "podmanager.composeRestart",
"when": "view == podmanView && viewItem == compose-group",
"group": "inline"
},
Expand Down
24 changes: 22 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { PodmanItem } from './podmanItem';
import { createContainer } from './createContainer';
import { createVolume, createNetwork } from './createResource';


const execAsync = promisify(exec);
const podmanTreeDataProvider = new PodmanTreeDataProvider();

function getPodmanPath(): string {
const config = vscode.workspace.getConfiguration('podmanager');
Expand All @@ -25,7 +25,6 @@ async function resetPodmanPath() {
export function activate(context: vscode.ExtensionContext) {
console.log('Podmanager extension is now active!');

const podmanTreeDataProvider = new PodmanTreeDataProvider();
const treeView = vscode.window.createTreeView('podmanView', { treeDataProvider: podmanTreeDataProvider });
context.subscriptions.push(treeView);

Expand Down Expand Up @@ -55,6 +54,10 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('podmanager.deleteVolume', deleteVolume),
vscode.commands.registerCommand('podmanager.deleteNetwork', deleteNetwork),
vscode.commands.registerCommand('podmanager.composeUp', composeUp),
vscode.commands.registerCommand('podmanager.composeDown', composeDown),
vscode.commands.registerCommand('podmanager.composeStart', composeStart),
vscode.commands.registerCommand('podmanager.composeStop', composeStop),
vscode.commands.registerCommand('podmanager.composeRestart', composeRestart),
vscode.commands.registerCommand('podmanager.startPod', startPod),
vscode.commands.registerCommand('podmanager.stopPod', stopPod),
vscode.commands.registerCommand('podmanager.restartPod', restartPod),
Expand Down Expand Up @@ -273,6 +276,22 @@ async function composeUp(uri?: vscode.Uri) {
await runComposeCommand('up -d', uri);
}

async function composeDown(uri?: vscode.Uri) {
await runComposeCommand('down', uri);
}

async function composeStart(uri?: vscode.Uri) {
await runComposeCommand('start', uri);
}

async function composeStop(uri?: vscode.Uri) {
await runComposeCommand('stop', uri);
}

async function composeRestart(uri?: vscode.Uri) {
await runComposeCommand('restart', uri);
}

async function startPod(item: PodmanItem) {
await runPodCommand('start', item.id!);
}
Expand Down Expand Up @@ -363,6 +382,7 @@ async function runComposeCommand(command: string, uri?: vscode.Uri) {
} catch (error) {
vscode.window.showErrorMessage(`Failed to execute Podman Compose ${command}: ${error}`);
}
podmanTreeDataProvider.refresh();
}

async function runPodCommand(command: string, podId: string, force: boolean = false) {
Expand Down
3 changes: 2 additions & 1 deletion src/podmanItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export class PodmanItem extends vscode.TreeItem {
public readonly composeProject?: string,
public children?: PodmanItem[],
public readonly isUsed?: boolean,
public readonly resourceName?: string
public readonly resourceName?: string,
public readonly fsPath?: string
) {
super(label, collapsibleState);
this.contextValue = contextValue;
Expand Down
43 changes: 38 additions & 5 deletions src/podmanTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export class PodmanTreeDataProvider implements vscode.TreeDataProvider<PodmanIte
case 'pod':
children = await this.getPodContainers(element.id!);
break;
case 'compose-group':
children = element?.children || [];
break;
}

this.cache.set(cacheKey, children);
Expand Down Expand Up @@ -109,7 +112,8 @@ export class PodmanTreeDataProvider implements vscode.TreeDataProvider<PodmanIte
const isRunning = status.startsWith('Up');
const isCompose = labels.includes('com.docker.compose.project');
const composeProject = isCompose ? this.extractComposeProject(labels) : '';
return { id, name, status, isRunning, isCompose, composeProject };
const composeFile = isCompose ? this.extractComposeFile(labels) : '';
return { id, name, status, isRunning, isCompose, composeProject, composeFile };
});

const nonComposeContainers = containers
Expand Down Expand Up @@ -276,8 +280,25 @@ export class PodmanTreeDataProvider implements vscode.TreeDataProvider<PodmanIte
}

private extractComposeProject(labels: string): string {
const projectLabel = labels.split(',').find(label => label.startsWith('com.docker.compose.project='));
return projectLabel ? projectLabel.split('=')[1] : 'Unknown Project';
const projectPattern = /com\.docker\.compose\.project(=|:)/;
return this.extractLabelValue(labels, projectPattern, 'Unknown Project');
}

private extractComposeFile(labels: string): string {
const directoryPattern = /com\.docker\.compose\.project\.working_dir(=|:)/;
const path = this.extractLabelValue(labels, directoryPattern, '');
if (path.length > 0) {
const filePattern = /com\.docker\.compose\.project\.config_files(=|:)/;
const composeFile = this.extractLabelValue(labels, filePattern, '');
return composeFile.length > 0 ? `${path}/${composeFile}` : '';
}
return '';
}

private extractLabelValue(labels: string, labelKeyPattern: RegExp, defaultValue: string): string {
const splitLabels = labels.split(/,| /);
const projectLabel = splitLabels.find(label => labelKeyPattern.test(label));
return projectLabel ? projectLabel.split(labelKeyPattern)[2] : defaultValue;
}

private getComposeGroups(containers: any[]): PodmanItem[] {
Expand All @@ -293,10 +314,22 @@ export class PodmanTreeDataProvider implements vscode.TreeDataProvider<PodmanIte
}, {});

return Object.entries(composeGroups).map(([project, containers], index) => {
const groupItem = new PodmanItem(`Compose Group ${index + 1}: ${project}`, vscode.TreeItemCollapsibleState.Collapsed, 'compose-group', undefined, undefined, undefined, project);
groupItem.children = containers.map((c: any) =>
const children = containers.map((c: any) =>
new PodmanItem(`${c.name} (${c.id})`, vscode.TreeItemCollapsibleState.None, 'compose-container', c.id, c.status, c.isRunning, project)
);
const groupItem = new PodmanItem(
`Compose Group ${index + 1}: ${project}`,
vscode.TreeItemCollapsibleState.Collapsed,
'compose-group',
undefined,
undefined,
undefined,
project,
children,
undefined,
undefined,
containers[0].composeFile
);
return groupItem;
});
}
Expand Down

0 comments on commit 934b20e

Please sign in to comment.