Skip to content

Commit

Permalink
feat: Add terminal access
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorniaky committed Dec 25, 2023
1 parent ed46962 commit 39bcb76
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@
"title": "%command.apps.stop%",
"icon": "$(debug-stop)"
},
{
"command": "discloud.apps.terminal",
"title": "%command.apps.terminal%"
},
{
"command": "discloud.domain.refresh",
"title": "%command.domain.refresh%",
Expand Down Expand Up @@ -673,6 +677,10 @@
"command": "discloud.apps.stop",
"when": "never"
},
{
"command": "discloud.apps.terminal",
"when": "never"
},
{
"command": "discloud.domain.refresh",
"when": "config.discloud.token"
Expand Down Expand Up @@ -853,6 +861,11 @@
"group": "0",
"when": "view == discloud-apps && discloudAppLength"
},
{
"command": "discloud.apps.terminal",
"group": "1",
"when": "view == discloud-apps && viewItem =~ /online\":true/"
},
{
"command": "discloud.upload",
"group": "inline",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"command.apps.start": "Start app",
"command.apps.status": "Refresh app status",
"command.apps.stop": "Stop app",
"command.apps.terminal": "Use the terminal",
"command.domain.refresh": "Refresh domain list",
"command.subdomain.refresh": "Refresh subdomain list",
"command.team.backup": "Backup team app",
Expand Down
1 change: 1 addition & 0 deletions package.nls.pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"command.apps.start": "Iniciar app",
"command.apps.status": "Atualizar estado do app",
"command.apps.stop": "Parar app",
"command.apps.terminal": "Usar o terminal",
"command.domain.refresh": "Atualizar lista de domínios",
"command.subdomain.refresh": "Atualizar lista de subdomínios",
"command.team.backup": "Backup app de equipe",
Expand Down
28 changes: 28 additions & 0 deletions src/commands/apps/terminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Uri, window } from "vscode";
import { TaskData } from "../../@types";
import extension from "../../extension";
import AppTreeItem from "../../structures/AppTreeItem";
import Command from "../../structures/Command";

export default class extends Command {
constructor() {
super();
}

async run(task: TaskData, item?: AppTreeItem) {
if (!item) {
const picked = await this.pickAppOrTeamApp(task, { showOther: false });
item = picked.app;
}

const terminal = window.createTerminal({
env: { DISCLOUD_TOKEN: extension.token },
iconPath: item.iconPath as Uri,
name: typeof item.label === "string" ? item.label : item.appId,
});

terminal.show();

terminal.sendText(`npx discloud-cli terminal ${item.appId}\nexit`);
}
}
9 changes: 7 additions & 2 deletions src/structures/AppChildTreeItem.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TreeItem } from "vscode";
import { AppTreeItemData } from "../@types";
import { getIconPath } from "../util";
import { JSONparse, getIconPath } from "../util";

export default class AppChildTreeItem extends TreeItem {
iconName?: string;
readonly appId: string;
readonly children?: Map<string, TreeItem>;

constructor(options: AppTreeItemData & { appId: string }) {
constructor(options: AppTreeItemData & { appId: string, online: boolean }) {
super(options.label, options.collapsibleState);
this.description = options.description;
this.iconName = options.iconName;
Expand All @@ -22,6 +22,11 @@ export default class AppChildTreeItem extends TreeItem {
this.children = new Map(options.children.map(child => [`${child.label}`, child]));
}
}

const values = this.contextValue.match(/([^\W]+)(?:\W(.*))?/) ?? [];
const json = values[2] ? JSONparse(values[2]) : null;

this.contextValue = `${values[1]}.${JSON.stringify(Object.assign({}, json, { online: options.online }))}`;
}

contextValue = "ChildTreeItem";
Expand Down
13 changes: 12 additions & 1 deletion src/structures/AppTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { t } from "@vscode/l10n";
import { LogOutputChannel, TreeItemCollapsibleState, Uri, window } from "vscode";
import { ApiVscodeApp, AppTreeItemData } from "../@types";
import extension from "../extension";
import { calculatePercentage, getIconName, getIconPath } from "../util";
import { JSONparse, calculatePercentage, getIconName, getIconPath } from "../util";
import AppChildTreeItem from "./AppChildTreeItem";
import BaseTreeItem from "./BaseTreeItem";

Expand Down Expand Up @@ -49,6 +49,11 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
this.iconPath = getIconPath(this.iconName);
this.isOnline = this.iconName === "on";

const values = this.contextValue.match(/([^\W]+)(?:\W(.*))?/) ?? [];
const json = values[2] ? JSONparse(values[2]) : null;

this.contextValue = `${values[1]}.${JSON.stringify(Object.assign({}, json, { online: this.isOnline }))}`;

const showAvatar = extension.config.get<string>("app.show.avatar.instead.status");

switch (showAvatar) {
Expand Down Expand Up @@ -90,6 +95,7 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
description: t("container"),
iconName: "container",
appId: this.appId,
online: this.isOnline,
}));

if ("memory" in data)
Expand All @@ -98,6 +104,7 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
description: t("label.ram"),
iconName: "ram",
appId: this.appId,
online: this.isOnline,
}));

if ("cpu" in data)
Expand All @@ -106,6 +113,7 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
description: t("label.cpu"),
iconName: "cpu",
appId: this.appId,
online: this.isOnline,
}));

if ("ssd" in data)
Expand All @@ -114,6 +122,7 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
description: t("label.ssd"),
iconName: "ssd",
appId: this.appId,
online: this.isOnline,
}));

if ("netIO" in data)
Expand All @@ -122,6 +131,7 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
description: t("network"),
iconName: "network",
appId: this.appId,
online: this.isOnline,
}));

if ("last_restart" in data)
Expand All @@ -130,6 +140,7 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
description: t("last.restart"),
iconName: "uptime",
appId: this.appId,
online: this.isOnline,
}));

this.collapsibleState =
Expand Down

0 comments on commit 39bcb76

Please sign in to comment.