Added more filtering options to app listing.

This commit is contained in:
2026-02-08 15:26:24 +01:00
parent 400d418763
commit 5888cb4d1a
3 changed files with 31 additions and 6 deletions

View File

@@ -32,10 +32,12 @@ or: sk-az-tools <command> --help`;
}
function usageListApps() {
return `Usage: sk-az-tools list-apps [--display-name|-n <name>] [global options]
return `Usage: sk-az-tools list-apps [--display-name|-n <name>] [--app-id|-i <appId>] [--filter|-f <glob>] [global options]
Options:
-n, --display-name <name> Filter apps by exact display name`;
-n, --display-name <name> Get app by name
-i, --app-id <appId> Get app by id
-f, --filter <glob> Filter by app display name glob`;
}
function usageListAppPermissions() {

View File

@@ -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) {

View File

@@ -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<Array> }
*/
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();