Skip to content

Commit

Permalink
ls: Show accessible resources (#62)
Browse files Browse the repository at this point in the history
Addresses ENG-1608. Marks accessible resources with * in ls results and
shows them at the top.

Depends on PR p0-security/app#1537

Example output:
<img width="573" alt="image"
src="https://github.com/p0-security/p0cli/assets/117865035/ec77196c-5946-403a-b125-bf57928851c8">

---------

Co-authored-by: Nathan Brahms <nbrahms@users.noreply.github.com>
  • Loading branch information
komal-dhull and nbrahms authored Mar 29, 2024
1 parent caa1a7e commit d7132a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/commands/__tests__/__snapshots__/ls.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ exports[`ls when valid ls command should print friendly message if no items: std
exports[`ls when valid ls command should print list response: stderr 1`] = `
[
[
"Showing destinations:",
"Showing destinations. Resources labeled with * are already accessible to you:",
],
]
`;
exports[`ls when valid ls command should print list response: stdout 1`] = `
[
[
"instance-1[02m - Group / Resource 1[00m",
" instance-1[02m - Group / Resource 1[00m",
],
[
"instance-2[02m - Resource 2[00m",
" instance-2[02m - Resource 2[00m",
],
]
`;
33 changes: 22 additions & 11 deletions src/commands/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import { fetchCommand } from "../drivers/api";
import { authenticate } from "../drivers/auth";
import { guard } from "../drivers/firestore";
import { print2, print1, Ansi } from "../drivers/stdio";
import { max } from "lodash";
import { max, orderBy } from "lodash";
import pluralize from "pluralize";
import yargs from "yargs";

type LsResponse = {
ok: true;
items: { key: string; value: string; group?: string }[];
items: {
key: string;
value: string;
group?: string;
isPreexisting?: boolean;
}[];
isTruncated: boolean;
term: string;
arg: string;
Expand Down Expand Up @@ -69,17 +74,23 @@ const ls = async (
${allArguments.join(" ")} <like>\` to narrow results)`
: "";

print2(`Showing${truncationPart} ${label}${postfixPart}:`);
const isSameValue = data.items.every((i) => !i.group && i.key === i.value);
const maxLength = max(data.items.map((i) => i.key.length)) || 0;
for (const item of data.items) {
print2(
`Showing${truncationPart} ${label}${postfixPart}. Resources labeled with * are already accessible to you:`
);
const sortedItems = orderBy(data.items, "isPreexisting", "desc");
const isSameValue = sortedItems.every((i) => !i.group && i.key === i.value);
const maxLength = max(sortedItems.map((i) => i.key.length)) || 0;
for (const item of sortedItems) {
const tagPart = `${item.group ? `${item.group} / ` : ""}${item.value}`;
const prefix = item.isPreexisting ? "* " : " ";
print1(
isSameValue
? item.key
: maxLength > 30
? `${item.key}\n ${Ansi.Dim}${tagPart}${Ansi.Reset}`
: `${item.key.padEnd(maxLength)}${Ansi.Dim} - ${tagPart}${Ansi.Reset}`
`${prefix}${
isSameValue
? item.key
: maxLength > 30
? `${item.key}\n ${Ansi.Dim}${tagPart}${Ansi.Reset}`
: `${item.key.padEnd(maxLength)}${Ansi.Dim} - ${tagPart}${Ansi.Reset}`
}`
);
}
} else {
Expand Down

0 comments on commit d7132a6

Please sign in to comment.