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 1 commit
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 @@ -19,10 +19,10 @@ Unknown argument: foo",
exports[`ls when valid ls command should print list response 1`] = `
[
[
"instance-1",
"instance-1 - Instance 1",
],
[
"instance-2",
"instance-2 - Instance 2",
],
]
`;
Expand Down
5 changes: 4 additions & 1 deletion src/commands/__tests__/ls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ describe("ls", () => {
ok: true,
term: "",
arg: "destination",
items: ["instance-1", "instance-2"],
items: [
{ key: "instance-1", value: "Instance 1" },
{ key: "instance-2", value: "Instance 2" },
],
});
});

Expand Down
27 changes: 20 additions & 7 deletions src/commands/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import { fetchCommand } from "../drivers/api";
import { authenticate } from "../drivers/auth";
import { guard } from "../drivers/firestore";
import { print2, print1 } 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,15 +51,27 @@ const ls = async (
"ls",
...args.arguments,
]);
const allArguments = [...args._, ...args.arguments];

if (data && "ok" in data && data.ok) {
print2(
`Showing${
data.isTruncated ? ` the first ${data.items.length}` : ""
} ${pluralize(data.arg)}${data.term ? ` matching '${data.term}'` : ""}:`
);
const truncationPart = data.isTruncated
? ` the first ${data.items.length}`
: "";
const argPart = pluralize(data.arg);
const postfixPart = data.term
? ` matching '${data.term}'`
: data.isTruncated
? ` (use \`p0
${allArguments.join(" ")} <like>\` to narrow results)`
: "";

print2(`Showing${truncationPart} ${argPart}${postfixPart}:`);
const isSameValue = data.items.every((i) => !i.group && i.key === i.value);
const longest = max(data.items.map((i) => i.key.length)) || 0;
for (const item of data.items) {
print1(item);
print1(
isSameValue ? item.key : `${item.key.padEnd(longest)} - ${item.value}`
);
}
} else {
throw data;
Expand Down