Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ls: Show keys and values #42

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = {
prettierPath: null,
preset: "ts-jest",
testEnvironment: "node",
modulePathIgnorePatterns: ["/build"],
modulePathIgnorePatterns: ["/dist"],
};
4 changes: 2 additions & 2 deletions src/commands/__tests__/__snapshots__/ls.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ exports[`ls when valid ls command should print list response: stderr 1`] = `
exports[`ls when valid ls command should print list response: stdout 1`] = `
[
[
"instance-1",
"instance-1 - Group / Resource 1",
],
[
"instance-2",
"instance-2 - Resource 2",
],
]
`;
7 changes: 5 additions & 2 deletions src/commands/__tests__/ls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("ls", () => {
describe("when valid ls command", () => {
const command = "ls ssh destination";

const mockItems = (items: string[]) =>
const mockItems = (items: object[]) =>
mockFetchCommand.mockResolvedValue({
ok: true,
term: "",
Expand All @@ -37,7 +37,10 @@ describe("ls", () => {
});

it("should print list response", async () => {
mockItems(["instance-1", "instance-2"]);
mockItems([
{ key: "instance-1", group: "Group", value: "Resource 1" },
{ key: "instance-2", value: "Resource 2" },
]);
await lsCommand(yargs()).parse(command);
expect(mockPrint1.mock.calls).toMatchSnapshot("stdout");
expect(mockPrint2.mock.calls).toMatchSnapshot("stderr");
Expand Down
33 changes: 25 additions & 8 deletions src/commands/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ You should have received a copy of the GNU General Public License along with @p0
import { fetchCommand } from "../drivers/api";
import { authenticate } from "../drivers/auth";
import { guard } from "../drivers/firestore";
import { print2, print1 } from "../drivers/stdio";
import { print2, print1, Ansi } from "../drivers/stdio";
import { max } from "lodash";
import pluralize from "pluralize";
import yargs from "yargs";

type LsResponse = {
ok: true;
items: string[];
items: { key: string; value: string; group?: string }[];
isTruncated: boolean;
term: string;
arg: string;
Expand Down Expand Up @@ -50,20 +51,36 @@ const ls = async (
"ls",
...args.arguments,
]);
const allArguments = [...args._, ...args.arguments];

if (data && "ok" in data && data.ok) {
const label = pluralize(data.arg);
if (data.items.length === 0) {
print2(`No ${label}`);
return;
}
print2(
`Showing${
data.isTruncated ? ` the first ${data.items.length}` : ""
} ${label}${data.term ? ` matching '${data.term}'` : ""}:`
);
const truncationPart = data.isTruncated
? ` the first ${data.items.length}`
: "";
const postfixPart = data.term
? ` matching '${data.term}'`
: data.isTruncated
? ` (use \`p0
${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) {
print1(item);
const tagPart = `${item.group ? `${item.group} / ` : ""}${item.value}`;
print1(
isSameValue
? item.key
: maxLength > 30
? `${item.key}\n ${Ansi.Dim}${tagPart}${Ansi.Reset}`
: `${item.key.padEnd(maxLength)}${Ansi.Dim} - ${tagPart}${Ansi.Reset}`
);
}
} else {
throw data;
Expand Down
9 changes: 9 additions & 0 deletions src/drivers/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ This file is part of @p0security/p0cli

You should have received a copy of the GNU General Public License along with @p0security/p0cli. If not, see <https://www.gnu.org/licenses/>.
**/

/** Functions to handle stdio
*
* These are essentially wrappers around console.foo, but allow for
* - Better testing
* - Later redirection / duplication
*/
import { mapValues } from "lodash";

/** Used to output machine-readable text to stdout
*
Expand All @@ -33,3 +35,10 @@ export function print2(message: any) {
// eslint-disable-next-line no-console
console.error(message);
}

const AnsiCodes = {
Reset: "00",
Dim: "02",
} as const;

export const Ansi = mapValues(AnsiCodes, (v) => `\u001b[${v}m`);
Loading