diff --git a/src/cli.js b/src/cli.js index 63d9508..0cade4d 100755 --- a/src/cli.js +++ b/src/cli.js @@ -32,10 +32,12 @@ or: sk-az-tools --help`; } function usageListApps() { - return `Usage: sk-az-tools list-apps [--display-name|-n ] [global options] + return `Usage: sk-az-tools list-apps [--display-name|-n ] [--app-id|-i ] [--filter|-f ] [global options] Options: - -n, --display-name Filter apps by exact display name`; + -n, --display-name Get app by name + -i, --app-id Get app by id + -f, --filter Filter by app display name glob`; } function usageListAppPermissions() { diff --git a/src/cli/commands.js b/src/cli/commands.js index 7c221f8..11a7ea0 100644 --- a/src/cli/commands.js +++ b/src/cli/commands.js @@ -20,6 +20,12 @@ function filterByPermissionName(rows, pattern) { ); } +function filterByDisplayName(rows, pattern) { + return rows.filter((item) => + minimatch(item.displayName ?? "", pattern, { nocase: true }) + ); +} + async function getGraphClientFromPublicConfig() { const config = await loadPublicConfig(); return getGraphClient({ @@ -34,9 +40,17 @@ async function runTableCommand() { async function runListAppsCommand(values) { const { client } = await getGraphClientFromPublicConfig(); - return listApps(client, { + let result = await listApps(client, { displayName: values["display-name"], + appId: values["app-id"], }); + if (values["app-id"] && result.length > 1) { + throw new Error(`Expected a single app for --app-id ${values["app-id"]}, but got ${result.length}`); + } + if (values.filter) { + result = filterByDisplayName(result, values.filter); + } + return result; } async function runListAppPermissionsCommand(values) { diff --git a/src/graph/app.js b/src/graph/app.js index 868c959..9cbe8a6 100644 --- a/src/graph/app.js +++ b/src/graph/app.js @@ -34,19 +34,28 @@ export async function deleteApp(client, appObjectId) { } /** - * List Azure applications, optionally filtered by display name. + * List Azure applications, optionally filtered by display name and/or app ID. * * @param { Object } client * @param { Object } [options] * @param { string } [options.displayName] + * @param { string } [options.appId] * @returns { Promise } */ export async function listApps(client, options = {}) { - const { displayName } = options; + const { displayName, appId } = options; let request = client.api("/applications"); + const filters = []; if (displayName) { - request = request.filter(`displayName eq '${displayName}'`); + filters.push(`displayName eq '${displayName}'`); + } + if (appId) { + filters.push(`appId eq '${appId}'`); + } + + if (filters.length > 0) { + request = request.filter(filters.join(" and ")); } const result = await request.get();