@@ -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 ) ;
0 commit comments