diff --git a/src/cli/commands.js b/src/cli/commands.js index 1d942f1..7c221f8 100644 --- a/src/cli/commands.js +++ b/src/cli/commands.js @@ -28,54 +28,72 @@ async function getGraphClientFromPublicConfig() { }); } +async function runTableCommand() { + return readJsonFromStdin(); +} + +async function runListAppsCommand(values) { + const { client } = await getGraphClientFromPublicConfig(); + return listApps(client, { + displayName: values["display-name"], + }); +} + +async function runListAppPermissionsCommand(values) { + if (!values["app-id"]) { + throw new Error("--app-id is required for list-app-permissions"); + } + + const { client } = await getGraphClientFromPublicConfig(); + let result = values.resolve || values.filter + ? await listAppPermissionsResolved(client, values["app-id"]) + : await listAppPermissions(client, values["app-id"]); + if (values.filter) { + result = filterByPermissionName(result, values.filter); + } + return result; +} + +async function runListAppGrantsCommand(values) { + if (!values["app-id"]) { + throw new Error("--app-id is required for list-app-grants"); + } + + const { client } = await getGraphClientFromPublicConfig(); + return listAppGrants(client, values["app-id"]); +} + +async function runListResourcePermissionsCommand(values) { + if (!values["app-id"] && !values["display-name"]) { + throw new Error("--app-id or --display-name is required for list-resource-permissions"); + } + if (values["app-id"] && values["display-name"]) { + throw new Error("Use either --app-id or --display-name for list-resource-permissions, not both"); + } + + const { client } = await getGraphClientFromPublicConfig(); + let result = await listResourcePermissions(client, { + appId: values["app-id"], + displayName: values["display-name"], + }); + if (values.filter) { + result = filterByPermissionName(result, values.filter); + } + return result; +} + export async function runCommand(command, values) { switch (command) { case "table": - return readJsonFromStdin(); - case "list-apps": { - const { client } = await getGraphClientFromPublicConfig(); - return listApps(client, { - displayName: values["display-name"], - }); - } - case "list-app-permissions": { - if (!values["app-id"]) { - throw new Error("--app-id is required for list-app-permissions"); - } - const { client } = await getGraphClientFromPublicConfig(); - let result = values.resolve || values.filter - ? await listAppPermissionsResolved(client, values["app-id"]) - : await listAppPermissions(client, values["app-id"]); - if (values.filter) { - result = filterByPermissionName(result, values.filter); - } - return result; - } - case "list-app-grants": { - if (!values["app-id"]) { - throw new Error("--app-id is required for list-app-grants"); - } - const { client } = await getGraphClientFromPublicConfig(); - return listAppGrants(client, values["app-id"]); - } - case "list-resource-permissions": { - if (!values["app-id"] && !values["display-name"]) { - throw new Error("--app-id or --display-name is required for list-resource-permissions"); - } - if (values["app-id"] && values["display-name"]) { - throw new Error("Use either --app-id or --display-name for list-resource-permissions, not both"); - } - - const { client } = await getGraphClientFromPublicConfig(); - let result = await listResourcePermissions(client, { - appId: values["app-id"], - displayName: values["display-name"], - }); - if (values.filter) { - result = filterByPermissionName(result, values.filter); - } - return result; - } + return runTableCommand(); + case "list-apps": + return runListAppsCommand(values); + case "list-app-permissions": + return runListAppPermissionsCommand(values); + case "list-app-grants": + return runListAppGrantsCommand(values); + case "list-resource-permissions": + return runListResourcePermissionsCommand(values); default: throw new Error(`Unknown command: ${command}`); }