Skip to content

Commit

Permalink
refactor(daemon,cli): Consolidate pet name list commands (merge #2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored Feb 28, 2024
2 parents 0b67947 + 90e756a commit c5d012f
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 37 deletions.
12 changes: 4 additions & 8 deletions packages/cli/src/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import os from 'os';
import { E } from '@endo/far';
import { withEndoHost } from '../context.js';

export const list = async ({ directoryName, special, all }) =>
export const list = async ({ directoryPath }) =>
withEndoHost({ os, process }, async ({ host: party }) => {
if (directoryName !== undefined) {
party = E(party).lookup(directoryName);
if (directoryPath !== undefined) {
party = E(party).lookup(...directoryPath.split('.'));
}
const petNames = await (() => {
if (all) return E(party).listAll();
if (special) return E(party).listSpecial();
return E(party).list();
})();
const petNames = await E(party).list();
for await (const petName of petNames) {
console.log(petName);
}
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,9 @@ export const main = async rawArgs => {
program
.command('list [directory]')
.description('show names known to the current or specified directory')
.option('-s,--special', 'show special names')
.option('--all', 'show all names')
.action(async (directoryName, cmd) => {
const { special, all } = cmd.opts();
.action(async (directoryPath, cmd) => {
const { list } = await import('./commands/list.js');
return list({ directoryName, special, all });
return list({ directoryPath });
});

program
Expand Down
4 changes: 0 additions & 4 deletions packages/daemon/src/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export const makeGuestMaker = ({
rename,
remove,
list,
listSpecial,
listAll,
} = makeMailbox({
petStore,
selfFormulaIdentifier: guestFormulaIdentifier,
Expand All @@ -78,8 +76,6 @@ export const makeGuestMaker = ({
request,
send,
list,
listSpecial,
listAll,
followNames,
listMessages,
followMessages,
Expand Down
4 changes: 0 additions & 4 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export const makeHostMaker = ({
dismiss,
adopt,
list,
listAll,
listSpecial,
rename,
remove,
cancel,
Expand Down Expand Up @@ -511,8 +509,6 @@ export const makeHostMaker = ({
request,
send,
list,
listSpecial,
listAll,
followNames,
listEntries,
followEntries,
Expand Down
8 changes: 1 addition & 7 deletions packages/daemon/src/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ export const makeMailboxMaker = ({
};

/** @type {import('./types.js').Mail['list']} */
const list = () => harden(petStore.list());
/** @type {import('./types.js').Mail['listSpecial']} */
const listSpecial = () => harden(Object.keys(specialNames).sort());
/** @type {import('./types.js').Mail['listAll']} */
const listAll = () =>
const list = () =>
harden([...Object.keys(specialNames).sort(), ...petStore.list()]);

/** @type {import('./types.js').Mail['reverseLookupFormulaIdentifier']} */
Expand Down Expand Up @@ -538,8 +534,6 @@ export const makeMailboxMaker = ({
dismiss,
adopt,
list,
listSpecial,
listAll,
rename,
remove,
cancel,
Expand Down
2 changes: 0 additions & 2 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,6 @@ export interface Mail {
reverseLookup(value: unknown): Array<string>;
// Extended methods:
lookup(...petNamePath: string[]): Promise<unknown>;
listSpecial(): Array<string>;
listAll(): Array<string>;
reverseLookupFormulaIdentifier(formulaIdentifier: string): Array<string>;
cancel(petName: string, reason: Error): Promise<void>;
// Mail operations:
Expand Down
16 changes: 9 additions & 7 deletions packages/daemon/test/test-endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,15 +1178,17 @@ test('list special names', async t => {
const readerRef = makeReaderRef([new TextEncoder().encode('hello\n')]);
await E(host).store(readerRef, 'hello-text');

const names = await E(host).list();
/** @type {string[]} */
const specialNames = await E(host).listSpecial();
const allNames = await E(host).listAll();
const names = await E(host).list();

t.deepEqual(['hello-text'], names);
t.assert(specialNames.length > 0);
t.assert(specialNames.every(name => name.toUpperCase() === name));
t.deepEqual([...specialNames, ...names], allNames);
// There should be special names, but they are in flux at time of writing and
// we don't need to update this test for every change, so just verify that
// there's at least one for now.
t.assert(names.length > 1);
t.deepEqual(
names.filter(name => name.toUpperCase() !== name),
['hello-text'],
);
});

test('guest cannot access host methods', async t => {
Expand Down

0 comments on commit c5d012f

Please sign in to comment.