Skip to content

Commit

Permalink
Add project tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
michelelizzit committed Nov 22, 2023
1 parent 177b377 commit a98672d
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 38 deletions.
1 change: 1 addition & 0 deletions media/refresh-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions media/refresh-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions media/remove-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions media/remove-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 31 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@
{
"command": "poiex.dispose",
"title": "Remove All Notes (DANGER)"
},
{
"command": "iacAudit.refreshProjectTree",
"title": "Refresh",
"icon": {
"light": "media/refresh-dark.svg",
"dark": "media/refresh-light.svg"
}
},
{
"command": "iacAudit.deleteTreeProject",
"title": "Delete project",
"icon": {
"light": "media/remove-dark.svg",
"dark": "media/remove-light.svg"
}
}
],
"configuration": {
Expand Down Expand Up @@ -161,6 +177,20 @@
"group": "inline@2",
"when": "commentController == poiex"
}
],
"view/title": [
{
"command": "iacAudit.refreshProjectTree",
"when": "view == iacAudit && workspaceFolderCount > 0 && iacAudit.isProjectOpen == false",
"group": "navigation"
}
],
"view/item/context": [
{
"command": "iacAudit.deleteTreeProject",
"when": "view == iacAudit && workspaceFolderCount > 0 && iacAudit.isProjectOpen == false",
"group": "inline"
}
]
},
"viewsContainers": {
Expand Down Expand Up @@ -288,4 +318,4 @@
"type": "git",
"url": "https://github.com/doyensec/poiex.git"
}
}
}
8 changes: 4 additions & 4 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,11 @@ export class IaCComments {
}
}

dispose() {
this.disposables.forEach((disposable) => {
async dispose() {
for (const disposable of this.disposables) {
this.context.subscriptions.splice(this.context.subscriptions.indexOf(disposable), 1);
disposable.dispose();
});
await disposable.dispose();
}
this.disposed = true;
}
}
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const DIAGNOSTICS_CODENAME = 'poiex';
export const IAC_POI_MESSAGE = "IaC Point Of Intersection:";
export const INFRAMAP_TIMEOUT_MS = 10 * 1000;
export const INFRAMAP_DOWNLOADED_STATENAME = "inframapDownloading";
export const PROJECT_TREE_VIEW_TITLE = "PoiEx: Project List";
export const PROJECT_TREE_VIEW_NO_PROJECTS_MESSAGE = "No projects found.";
export const PROJECT_TREE_VIEW_DB_ERROR_MESSAGE = "Invalid DB configuration.";

export const FLAG_UNFLAGGED = 0;
export const FLAG_FALSE = 1;
Expand Down
101 changes: 68 additions & 33 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import { LocalDB } from './db';
import { RemoteDB } from './remote';
import * as comments from './comments';
import { IaCEncryption } from './encryption';
import * as tree from './tree';

let db: LocalDB;
let rdb: RemoteDB;
let mComments: comments.IaCComments;
let pdb: IaCProjectDir;
let projectDisposables: vscode.Disposable[] = [];
let projectClosing: boolean = false;
let projectTreeView: tree.ProjectTreeViewManager;

export async function initLocalDb(dbDir: string, projectUuid: string) {
// Create sqlite3 database in storage directory
Expand Down Expand Up @@ -212,18 +214,57 @@ async function init1(context: vscode.ExtensionContext, iacPath: string) {
}));

// Register command to open an existing project
context.subscriptions.push(vscode.commands.registerCommand(`${constants.EXT_NAME}.openProject`, async () => {
context.subscriptions.push(vscode.commands.registerCommand(`${constants.EXT_NAME}.openProject`, async (projectUuid: string | undefined = undefined) => {
console.log('[IaC Main] Open project button pressed');
if (!ensureDbOk()) {
console.log('[IaC Main] Remote database not configured, cannot open project');
return;
}

let cb2 = async (choice: string | undefined) => {
if (choice === undefined) {
return;
}
let projectUuid = choice;
if (choice.includes(' $ ')) {
projectUuid = choice.split(' $ ')[1];
}
let project = await pdb.getProject(projectUuid);
assert(project !== null, "Project not found in local database");
if (project === null) { return; };
if (project[3] === null || project[2] !== null) {
openProject(context, iacUri, projectUuid);
return;
}
// Ask for project secret
vscode.window.showInputBox({
placeHolder: 'Please enter project secret',
prompt: 'Please enter project secret',
password: true,
validateInput: (value: string) => {
if (value === undefined || value === '') {
return 'Project secret cannot be empty';
}
return undefined;
}
}).then((projectSecret) => {
if (projectSecret === undefined) {
return;
}
openProject(context, iacUri, projectUuid, projectSecret);
});
};

let cb = async () => {
console.log('[IaC Main] Open project ready, executing callback');
if (projectUuid !== undefined) {
cb2(projectUuid);
return;
}

// Show list of projects as quickpick
let projectList = (await pdb.listProjects()) as {}[];
projectTreeView.update(projectList);
let projectNames = projectList.map((project: any) => project.name + " $ " + project.uuid);
console.log('[IaC Main] Open project got list of projects');
if (projectNames.length === 0) {
Expand All @@ -233,36 +274,7 @@ async function init1(context: vscode.ExtensionContext, iacPath: string) {

vscode.window.showQuickPick(projectNames, {
placeHolder: 'Please select a project to open'
}).then(async (choice) => {
if (choice === undefined) {
return;
}
let projectUuid = choice.split(' $ ')[1];
let project = await pdb.getProject(projectUuid);
assert(project !== null, "Project not found in local database");
if (project === null) { return; };
if (project[3] === null || project[2] !== null) {
openProject(context, iacUri, projectUuid);
return;
}
// Ask for project secret
vscode.window.showInputBox({
placeHolder: 'Please enter project secret',
prompt: 'Please enter project secret',
password: true,
validateInput: (value: string) => {
if (value === undefined || value === '') {
return 'Project secret cannot be empty';
}
return undefined;
}
}).then((projectSecret) => {
if (projectSecret === undefined) {
return;
}
openProject(context, iacUri, projectUuid, projectSecret);
});
});
}).then(cb2);
};

if (rdb.settingsEnabled()) {
Expand All @@ -284,9 +296,24 @@ async function init1(context: vscode.ExtensionContext, iacPath: string) {
let projectUuid = project.uuid;
await pdb.removeProject(projectUuid, rdb);
}
projectTreeView.update();
}
});
}));
}));

// Experimental tree view
if (ensureDbOk()) {
projectTreeView = new tree.ProjectTreeViewManager(context, pdb, rdb);
context.subscriptions.push(projectTreeView);
projectTreeView.show();
projectTreeView.update();
}
else {
projectTreeView = new tree.ProjectTreeViewManager(context, pdb, undefined);
context.subscriptions.push(projectTreeView);
projectTreeView.showDbError();
console.log('[IaC Main] Remote database not configured, cannot show project list');
}
}

async function openProject(context: vscode.ExtensionContext, storageUri: vscode.Uri, projectUuid: string, projectSecret: string | null = null) {
Expand Down Expand Up @@ -325,6 +352,9 @@ async function openProject(context: vscode.ExtensionContext, storageUri: vscode.
vscode.commands.executeCommand('setContext', 'iacAudit.isProjectCreator', true);
vscode.commands.executeCommand('setContext', 'iacAudit.isProjectEncrypted', projectSecret !== null);

// Hide project tree view
projectTreeView.hide();

rdb.setProjectUuid(projectUuid, projectSecret);

await initLocalDb(storageUri.fsPath, projectUuid);
Expand Down Expand Up @@ -415,8 +445,8 @@ async function closeProject(context: vscode.ExtensionContext, projectUuid: strin
}
projectClosing = true;

await mComments.dispose();
mIacWebviewManager.dispose();
mComments.dispose();
mIaCDiagnostics.dispose();

// Dispose all project disposables
Expand All @@ -439,6 +469,11 @@ async function closeProject(context: vscode.ExtensionContext, projectUuid: strin
vscode.commands.executeCommand('setContext', 'iacAudit.isProjectCreator', false);
vscode.commands.executeCommand('setContext', 'iacAudit.isProjectEncrypted', false);

// Show project tree view
projectTreeView.show().then(() => {
projectTreeView.update();
});

// Race condition prevention
projectClosing = false;
}
Expand Down
Loading

0 comments on commit a98672d

Please sign in to comment.