Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion extensions/ql-vscode/src/databases/config/db-config-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,29 @@ export class DbConfigStore extends DisposableObject {
throw Error("Cannot add remote list if config is not loaded");
}

if (this.doesRemoteListExist(listName)) {
throw Error(`A remote list with the name '${listName}' already exists`);
}

const config: DbConfig = cloneDbConfig(this.config);
config.databases.remote.repositoryLists.push({
name: listName,
repositories: [],
});

// TODO: validate that the name doesn't already exist
await this.writeConfig(config);
}

public doesRemoteListExist(listName: string): boolean {
if (!this.config) {
throw Error("Cannot check remote list existence if config is not loaded");
}

return this.config.databases.remote.repositoryLists.some(
(l) => l.name === listName,
);
}

private async writeConfig(config: DbConfig): Promise<void> {
await writeJSON(this.configPath, config, {
spaces: 2,
Expand Down
8 changes: 8 additions & 0 deletions extensions/ql-vscode/src/databases/db-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ export class DbManager {
}

public async addNewRemoteList(listName: string): Promise<void> {
if (this.dbConfigStore.doesRemoteListExist(listName)) {
throw Error(`A list with the name '${listName}' already exists`);
}

await this.dbConfigStore.addRemoteList(listName);
}

public doesRemoteListExist(listName: string): boolean {
return this.dbConfigStore.doesRemoteListExist(listName);
}
}
11 changes: 9 additions & 2 deletions extensions/ql-vscode/src/databases/ui/db-panel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TreeViewExpansionEvent, window, workspace } from "vscode";
import { commandRunner } from "../../commandRunner";
import { showAndLogErrorMessage } from "../../helpers";
import { DisposableObject } from "../../pure/disposable-object";
import { DbManager } from "../db-manager";
import { DbTreeDataProvider } from "./db-tree-data-provider";
Expand Down Expand Up @@ -58,15 +59,21 @@ export class DbPanel extends DisposableObject {
}

private async addNewRemoteList(): Promise<void> {
// TODO: check that config exists *before* showing the input box
const listName = await window.showInputBox({
prompt: "Enter a name for the new list",
placeHolder: "example-list",
});
if (listName === undefined) {
return;
}
await this.dbManager.addNewRemoteList(listName);

if (this.dbManager.doesRemoteListExist(listName)) {
void showAndLogErrorMessage(
`A list with the name '${listName}' already exists`,
);
} else {
await this.dbManager.addNewRemoteList(listName);
}
}

private async setSelectedItem(treeViewItem: DbTreeViewItem): Promise<void> {
Expand Down