refactor(cli): split command dispatcher into per-command handlers
This commit is contained in:
@@ -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) {
|
export async function runCommand(command, values) {
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "table":
|
case "table":
|
||||||
return readJsonFromStdin();
|
return runTableCommand();
|
||||||
case "list-apps": {
|
case "list-apps":
|
||||||
const { client } = await getGraphClientFromPublicConfig();
|
return runListAppsCommand(values);
|
||||||
return listApps(client, {
|
case "list-app-permissions":
|
||||||
displayName: values["display-name"],
|
return runListAppPermissionsCommand(values);
|
||||||
});
|
case "list-app-grants":
|
||||||
}
|
return runListAppGrantsCommand(values);
|
||||||
case "list-app-permissions": {
|
case "list-resource-permissions":
|
||||||
if (!values["app-id"]) {
|
return runListResourcePermissionsCommand(values);
|
||||||
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;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unknown command: ${command}`);
|
throw new Error(`Unknown command: ${command}`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user