diff --git a/src/commands/__tests__/__snapshots__/ls.test.ts.snap b/src/commands/__tests__/__snapshots__/ls.test.ts.snap index b9932b8d..7437f60a 100644 --- a/src/commands/__tests__/__snapshots__/ls.test.ts.snap +++ b/src/commands/__tests__/__snapshots__/ls.test.ts.snap @@ -29,7 +29,7 @@ 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:", ], ] `; @@ -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 - Group / Resource 1", + " instance-1 - Group / Resource 1", ], [ - "instance-2 - Resource 2", + " instance-2 - Resource 2", ], ] `; diff --git a/src/commands/ls.ts b/src/commands/ls.ts index 54e4b8c5..120aab17 100644 --- a/src/commands/ls.ts +++ b/src/commands/ls.ts @@ -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; @@ -69,17 +74,23 @@ const ls = async ( ${allArguments.join(" ")} \` 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 {