Skip to content

Commit

Permalink
refactor: Move pet name parsing to store.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed May 15, 2024
1 parent 61dc434 commit a7ce5f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
17 changes: 10 additions & 7 deletions packages/cli/src/commands/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { makeReaderRef } from '@endo/daemon';
import { E } from '@endo/far';

import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';

/**
* @param {Array<Uint8Array>} arrays
Expand Down Expand Up @@ -72,32 +73,34 @@ export const store = async ({
)})`;
}

const parsedName = parsePetNamePath(name);

await withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
if (storeText !== undefined) {
await E(agent).storeValue(storeText, name);
await E(agent).storeValue(storeText, parsedName);
} else if (storeJson !== undefined) {
await E(agent).storeValue(JSON.parse(storeJson), name);
await E(agent).storeValue(JSON.parse(storeJson), parsedName);
} else if (storeBigInt !== undefined) {
await E(agent).storeValue(BigInt(storeBigInt), name);
await E(agent).storeValue(BigInt(storeBigInt), parsedName);
} else if (storeTextStdin !== undefined) {
const reader = makeNodeReader(process.stdin);
const bytes = await asyncConcat(reader);
const text = new TextDecoder().decode(bytes);
await E(agent).storeValue(text, name);
await E(agent).storeValue(text, parsedName);
} else if (storeJsonStdin !== undefined) {
const reader = makeNodeReader(process.stdin);
const bytes = await asyncConcat(reader);
const text = new TextDecoder().decode(bytes);
await E(agent).storeValue(JSON.parse(text), name);
await E(agent).storeValue(JSON.parse(text), parsedName);
} else if (storeStdin !== undefined) {
const reader = makeNodeReader(process.stdin);
const readerRef = makeReaderRef(reader);
await E(agent).storeBlob(readerRef, name);
await E(agent).storeBlob(readerRef, parsedName);
} else if (storePath !== undefined) {
const nodeReadStream = fs.createReadStream(storePath);
const reader = makeNodeReader(nodeReadStream);
const readerRef = makeReaderRef(reader);
await E(agent).storeBlob(readerRef, name);
await E(agent).storeBlob(readerRef, parsedName);
}
});
};
3 changes: 1 addition & 2 deletions packages/cli/src/endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import url from 'url';

import { Command } from 'commander';
import { prompt } from './prompt.js';
import { parsePetNamePath } from './pet-name.js';

const packageDescriptorPath = url.fileURLToPath(
new URL('../package.json', import.meta.url),
Expand Down Expand Up @@ -371,7 +370,7 @@ export const main = async rawArgs => {
storeJson,
storeJsonStdin,
storeBigInt,
name: parsePetNamePath(name),
name,
agentNames,
});
});
Expand Down

0 comments on commit a7ce5f0

Please sign in to comment.