Skip to content

Commit

Permalink
List duplicate resource names when import error
Browse files Browse the repository at this point in the history
  • Loading branch information
emrul committed Jan 5, 2023
1 parent b955233 commit 86526d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cliCmd/importCmd.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {genFileNameFromNetworkName, loadNetworkAndApiKey, AFFIRMATIVES, tryProcessPortRestrictionString, loadClientForCLI} from "../utils/smallUtilFuncs.mjs";
import {genFileNameFromNetworkName, loadNetworkAndApiKey, AFFIRMATIVES, tryProcessPortRestrictionString, loadClientForCLI, findDuplicates} from "../utils/smallUtilFuncs.mjs";
import {TwingateApiClient} from "../TwingateApiClient.mjs";
import {Log} from "../utils/log.js";
import XLSX from "https://cdn.esm.sh/v58/xlsx@0.17.4/deno/xlsx.js";
Expand Down Expand Up @@ -201,8 +201,9 @@ export const importCmd = new Command()
if ( options.resources ) {
node.resourceNames = node.resources.map(resourceId => nodeIdMap[resourceId].name);
node.resources = node.resources.map(resourceId => nodeIdMap[resourceId]);
if (node.resourceNames.length !== (new Set(node.resourceNames)).size) {
throw new Error(`Remote network '${node.name}' contains resources with duplicate names`);
let duplicateResourceNames = findDuplicates(node.resourceNames);
if ( duplicateResourceNames.length > 0 ) {
throw new Error(`Remote network '${node.name}' contains resources with duplicate names:\n${duplicateResourceNames.join('\n')}`);
}
}
nodeLabelIdMap.RemoteNetwork[node.name] = node.id;
Expand Down
7 changes: 7 additions & 0 deletions utils/smallUtilFuncs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,11 @@ export async function downloadFile(url, filename){
}
}

export function findDuplicates(array) {
let seen = new Set(),
duplicates = [];
array.forEach( item => seen.has(item) ? duplicates.push(item) : seen.add(item) );
return duplicates;
}

export const delay = async (ms) => new Promise(resolve => setTimeout(resolve, ms));

0 comments on commit 86526d8

Please sign in to comment.