update: adopted new output options. Various optimizations.
Some checks failed
build / build (push) Failing after 15s
Some checks failed
build / build (push) Failing after 15s
This commit is contained in:
35
src/cli.ts
35
src/cli.ts
@@ -13,10 +13,7 @@ import { usageLogin } from "./cli/commands/login.ts";
|
||||
import { usageLogout } from "./cli/commands/logout.ts";
|
||||
import { usageRest } from "./cli/commands/rest.ts";
|
||||
import {
|
||||
normalizeOutputFormat,
|
||||
outputFiltered,
|
||||
parseHeaderSpec,
|
||||
renderOutput,
|
||||
renderCliOutput,
|
||||
} from "@slawek/sk-tools";
|
||||
|
||||
type CliValues = {
|
||||
@@ -36,6 +33,7 @@ type CliValues = {
|
||||
short?: boolean;
|
||||
filter?: string;
|
||||
query?: string;
|
||||
columns?: string;
|
||||
header?: string;
|
||||
output?: string;
|
||||
[key: string]: string | boolean | undefined;
|
||||
@@ -56,6 +54,7 @@ Commands:
|
||||
|
||||
Global options (all commands):
|
||||
-q, --query <jmespath>
|
||||
-C, --columns <definition> Column tokens: col (raw), col: (auto), col:Label (custom), exact via = prefix
|
||||
-o, --output <format> table|t|alignedtable|at|prettytable|pt|tsv
|
||||
-h, --help
|
||||
|
||||
@@ -86,16 +85,6 @@ function usageCommand(command: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
function omitRecords(record: Record<string, unknown>, names: Set<string>): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(record).filter(([key]) => !names.has(key)),
|
||||
);
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const argv = process.argv.slice(2);
|
||||
const command = argv[0];
|
||||
@@ -128,7 +117,8 @@ async function main(): Promise<void> {
|
||||
short: { type: "boolean", short: "s" },
|
||||
filter: { type: "string", short: "f" },
|
||||
query: { type: "string", short: "q" },
|
||||
header: { type: "string", short: "H" },
|
||||
columns: { type: "string", short: "C" },
|
||||
header: { type: "string" },
|
||||
output: { type: "string", short: "o" },
|
||||
},
|
||||
strict: true,
|
||||
@@ -142,19 +132,8 @@ async function main(): Promise<void> {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const outputFormat = normalizeOutputFormat(typedValues.output);
|
||||
const result = await runCommand(command, typedValues);
|
||||
const filtered = outputFiltered(result, typedValues.query);
|
||||
let output: unknown = filtered;
|
||||
if (command === "list-app-permissions" && typedValues.short && Array.isArray(filtered) && filtered.every(isRecord)) {
|
||||
const names = new Set(["resourceAppId", "permissionId"]);
|
||||
output = filtered.map((item) => omitRecords(item, names));
|
||||
}
|
||||
const headerSpec = command === "rest"
|
||||
? parseHeaderSpec(undefined)
|
||||
: parseHeaderSpec(typedValues.header);
|
||||
|
||||
renderOutput(outputFormat, headerSpec, output);
|
||||
const output = await runCommand(command, typedValues);
|
||||
renderCliOutput(output, typedValues.output, typedValues.query, typedValues.columns);
|
||||
}
|
||||
|
||||
main().catch((err: unknown) => {
|
||||
|
||||
@@ -5,6 +5,23 @@ import { listAppPermissions, listAppPermissionsResolved } from "../../graph/app.
|
||||
import { filterByPermissionName, getGraphClientFromPublicConfig } from "./shared.ts";
|
||||
import type { CommandValues } from "./types.ts";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function omitColumns(input: unknown, names: string[]): unknown {
|
||||
if (!Array.isArray(input) || !input.every(isRecord)) {
|
||||
return input;
|
||||
}
|
||||
|
||||
const namesSet = new Set(names);
|
||||
return input.map((record) =>
|
||||
Object.fromEntries(
|
||||
Object.entries(record).filter(([key]) => !namesSet.has(key)),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function usageListAppPermissions(): string {
|
||||
return `Usage: sk-az-tools list-app-permissions --app-id|-i <appId> [--resolve|-r] [--short|-s] [--filter|-f <glob>] [global options]
|
||||
|
||||
@@ -21,11 +38,14 @@ export async function runListAppPermissionsCommand(values: CommandValues): Promi
|
||||
}
|
||||
|
||||
const { client } = await getGraphClientFromPublicConfig();
|
||||
let result = values.resolve || values.filter
|
||||
let result: unknown = values.resolve || values.filter
|
||||
? await listAppPermissionsResolved(client, values["app-id"])
|
||||
: await listAppPermissions(client, values["app-id"]);
|
||||
if (values.short) {
|
||||
result = omitColumns(result, ["resourceAppId", "permissionId"]);
|
||||
}
|
||||
if (values.filter) {
|
||||
result = filterByPermissionName(result, values.filter);
|
||||
result = filterByPermissionName(result as Array<Record<string, unknown>>, values.filter);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user