Skip to content

Commit 091d793

Browse files
authored
Stop user from adding a db list with the same name (#1873)
1 parent b1bf82d commit 091d793

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

extensions/ql-vscode/src/databases/config/db-config-store.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,29 @@ export class DbConfigStore extends DisposableObject {
9999
throw Error("Cannot add remote list if config is not loaded");
100100
}
101101

102+
if (this.doesRemoteListExist(listName)) {
103+
throw Error(`A remote list with the name '${listName}' already exists`);
104+
}
105+
102106
const config: DbConfig = cloneDbConfig(this.config);
103107
config.databases.remote.repositoryLists.push({
104108
name: listName,
105109
repositories: [],
106110
});
107111

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

115+
public doesRemoteListExist(listName: string): boolean {
116+
if (!this.config) {
117+
throw Error("Cannot check remote list existence if config is not loaded");
118+
}
119+
120+
return this.config.databases.remote.repositoryLists.some(
121+
(l) => l.name === listName,
122+
);
123+
}
124+
112125
private async writeConfig(config: DbConfig): Promise<void> {
113126
await writeJSON(this.configPath, config, {
114127
spaces: 2,

extensions/ql-vscode/src/databases/db-manager.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ export class DbManager {
7676
}
7777

7878
public async addNewRemoteList(listName: string): Promise<void> {
79+
if (this.dbConfigStore.doesRemoteListExist(listName)) {
80+
throw Error(`A list with the name '${listName}' already exists`);
81+
}
82+
7983
await this.dbConfigStore.addRemoteList(listName);
8084
}
85+
86+
public doesRemoteListExist(listName: string): boolean {
87+
return this.dbConfigStore.doesRemoteListExist(listName);
88+
}
8189
}

extensions/ql-vscode/src/databases/ui/db-panel.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TreeViewExpansionEvent, window, workspace } from "vscode";
22
import { commandRunner } from "../../commandRunner";
3+
import { showAndLogErrorMessage } from "../../helpers";
34
import { DisposableObject } from "../../pure/disposable-object";
45
import { DbManager } from "../db-manager";
56
import { DbTreeDataProvider } from "./db-tree-data-provider";
@@ -58,15 +59,21 @@ export class DbPanel extends DisposableObject {
5859
}
5960

6061
private async addNewRemoteList(): Promise<void> {
61-
// TODO: check that config exists *before* showing the input box
6262
const listName = await window.showInputBox({
6363
prompt: "Enter a name for the new list",
6464
placeHolder: "example-list",
6565
});
6666
if (listName === undefined) {
6767
return;
6868
}
69-
await this.dbManager.addNewRemoteList(listName);
69+
70+
if (this.dbManager.doesRemoteListExist(listName)) {
71+
void showAndLogErrorMessage(
72+
`A list with the name '${listName}' already exists`,
73+
);
74+
} else {
75+
await this.dbManager.addNewRemoteList(listName);
76+
}
7077
}
7178

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

0 commit comments

Comments
 (0)