Skip to content

Commit b61c9bf

Browse files
committed
chore: updated compositional types to tagged types
1 parent 97aad31 commit b61c9bf

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

src/secrets/CommandCat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class CommandGet extends CommandPolykey {
9595
// Print out incoming data to standard out
9696
let hasErrored = false;
9797
for await (const result of response.readable) {
98-
if (result.type === 'error') {
98+
if (result.type === 'ErrorMessage') {
9999
hasErrored = true;
100100
switch (result.code) {
101101
case 'ENOENT':

src/secrets/CommandEdit.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class CommandEdit extends CommandPolykey {
2727
const secretPath = fullSecretPath[1] ?? '/';
2828
const os = await import('os');
2929
const { spawn } = await import('child_process');
30-
const vaultsErrors = await import('polykey/dist/vaults/errors');
3130
const { default: PolykeyClient } = await import(
3231
'polykey/dist/PolykeyClient'
3332
);
@@ -66,41 +65,48 @@ class CommandEdit extends CommandPolykey {
6665
const secretExists = await binUtils.retryAuthentication(
6766
async (auth) => {
6867
let exists = true;
69-
const response = await pkClient.rpcClient.methods.vaultsSecretsGet({
68+
const response =
69+
await pkClient.rpcClient.methods.vaultsSecretsCat();
70+
const writer = response.writable.getWriter();
71+
await writer.write({
7072
nameOrId: vaultName,
7173
secretName: secretPath,
7274
metadata: auth,
7375
});
74-
try {
75-
let rawSecretContent: string = '';
76-
for await (const chunk of response) {
76+
await writer.close();
77+
let rawSecretContent: string = '';
78+
for await (const chunk of response.readable) {
79+
if (chunk.type === 'SuccessMessage') {
7780
rawSecretContent += chunk.secretContent;
81+
} else {
82+
if (chunk.code === 'ENOENT') {
83+
exists = false;
84+
break;
85+
} else if (chunk.code === 'EISDIR') {
86+
// First, write the inline error to standard error like other
87+
// secrets commands do.
88+
process.stderr.write(
89+
`edit: ${secretPath}: No such file or directory\n`,
90+
);
91+
// Then, throw an error to get the non-zero exit code. As this
92+
// command is Polykey-specific, the code doesn't really matter
93+
// that much.
94+
throw new errors.ErrorPolykeyCLIEditSecret(
95+
'The specified secret cannot be edited',
96+
);
97+
} else {
98+
throw new errors.ErrorPolykeyCLIEditSecret(
99+
`Unexpected error value returned: ${chunk.code} (${chunk.data})`,
100+
);
101+
}
78102
}
103+
}
104+
// Only make the temp file is the secret actually exists
105+
if (exists) {
79106
const secretContent = Buffer.from(rawSecretContent, 'binary');
80107
await this.fs.promises.writeFile(tmpFile, secretContent);
81-
} catch (e) {
82-
const [cause, _] = binUtils.remoteErrorCause(e);
83-
if (cause instanceof vaultsErrors.ErrorSecretsSecretUndefined) {
84-
exists = false;
85-
} else if (
86-
cause instanceof vaultsErrors.ErrorSecretsIsDirectory
87-
) {
88-
// First, write the inline error to standard error like other
89-
// secrets commands do.
90-
process.stderr.write(
91-
`edit: ${secretPath}: No such file or directory\n`,
92-
);
93-
// Then, throw an error to get the non-zero exit code. As this
94-
// command is Polykey-specific, the code doesn't really matter
95-
// that much.
96-
throw new errors.ErrorPolykeyCLIEditSecret(
97-
'Failed to edit secret',
98-
);
99-
} else {
100-
throw e;
101-
}
108+
return exists;
102109
}
103-
return exists;
104110
},
105111
meta,
106112
);
@@ -141,6 +147,7 @@ class CommandEdit extends CommandPolykey {
141147
editorProc.on('error', onError);
142148
editorProc.on('close', onClose);
143149
});
150+
// TODO: don't write secret if it matches what we already have
144151
let content: string;
145152
try {
146153
const buffer = await this.fs.promises.readFile(tmpFile);

src/secrets/CommandMkdir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class CommandMkdir extends CommandPolykey {
7878
// TypeScript cannot properly perform type narrowing on this type, so
7979
// the `as` keyword is used to help it out.
8080
for await (const result of response.readable) {
81-
if (result.type === 'error') {
81+
if (result.type === 'ErrorMessage') {
8282
hasErrored = true;
8383
switch (result.code) {
8484
case 'ENOENT':

src/secrets/CommandRemove.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class CommandRemove extends CommandPolykey {
7979
// Check if any errors were raised
8080
let hasErrored = false;
8181
for await (const result of response.readable) {
82-
if (result.type === 'error') {
82+
if (result.type === 'ErrorMessage') {
8383
hasErrored = true;
8484
switch (result.code) {
8585
case 'ENOTEMPTY':

0 commit comments

Comments
 (0)