11import type PolykeyClient from 'polykey/dist/PolykeyClient' ;
2- import * as errors from '../errors' ;
32import CommandPolykey from '../CommandPolykey' ;
3+ import * as binProcessors from '../utils/processors' ;
4+ import * as binParsers from '../utils/parsers' ;
45import * as binUtils from '../utils' ;
56import * as binOptions from '../utils/options' ;
6- import * as binParsers from '../utils/parsers' ;
7- import * as binProcessors from '../utils/processors' ;
87
9- class CommandUpdate extends CommandPolykey {
8+ class CommandWrite extends CommandPolykey {
109 constructor ( ...args : ConstructorParameters < typeof CommandPolykey > ) {
1110 super ( ...args ) ;
12- this . name ( 'update' ) ;
13- this . description ( 'Update a Secret' ) ;
14- this . argument (
15- '<directoryPath>' ,
16- 'On disk path to the secret file with the contents of the updated secret' ,
17- ) ;
11+ this . name ( 'write' ) ;
12+ this . description ( 'Write data into a secret from standard in' ) ;
1813 this . argument (
1914 '<secretPath>' ,
20- 'Path to where the secret to be updated , specified as <vaultName>:<directoryPath>' ,
15+ 'Path to the secret, specified as <vaultName>:<directoryPath>' ,
2116 binParsers . parseSecretPathValue ,
2217 ) ;
2318 this . addOption ( binOptions . nodeId ) ;
2419 this . addOption ( binOptions . clientHost ) ;
2520 this . addOption ( binOptions . clientPort ) ;
26- this . action ( async ( directoryPath , secretPath , options ) => {
21+ this . action ( async ( secretPath , options ) => {
2722 const { default : PolykeyClient } = await import (
2823 'polykey/dist/PolykeyClient'
2924 ) ;
@@ -54,27 +49,36 @@ class CommandUpdate extends CommandPolykey {
5449 } ,
5550 logger : this . logger . getChild ( PolykeyClient . name ) ,
5651 } ) ;
57- let content : Buffer ;
58- try {
59- content = await this . fs . promises . readFile ( directoryPath ) ;
60- } catch ( e ) {
61- throw new errors . ErrorPolykeyCLIFileRead ( e . message , {
62- data : {
63- errno : e . errno ,
64- syscall : e . syscall ,
65- code : e . code ,
66- path : e . path ,
67- } ,
68- cause : e ,
69- } ) ;
70- }
52+
53+ let stdin : string = '' ;
54+ await new Promise < void > ( ( resolve , reject ) => {
55+ const cleanup = ( ) => {
56+ process . stdin . removeListener ( 'data' , dataHandler ) ;
57+ process . stdin . removeListener ( 'error' , errorHandler ) ;
58+ process . stdin . removeListener ( 'end' , endHandler ) ;
59+ } ;
60+ const dataHandler = ( data : Buffer ) => {
61+ stdin += data . toString ( ) ;
62+ } ;
63+ const errorHandler = ( err : Error ) => {
64+ cleanup ( ) ;
65+ reject ( err ) ;
66+ } ;
67+ const endHandler = ( ) => {
68+ cleanup ( ) ;
69+ resolve ( ) ;
70+ } ;
71+ process . stdin . on ( 'data' , dataHandler ) ;
72+ process . stdin . once ( 'error' , errorHandler ) ;
73+ process . stdin . once ( 'end' , endHandler ) ;
74+ } ) ;
7175 await binUtils . retryAuthentication (
72- ( auth ) =>
73- pkClient . rpcClient . methods . vaultsSecretsEdit ( {
76+ async ( auth ) =>
77+ await pkClient . rpcClient . methods . vaultsSecretsWriteFile ( {
7478 metadata : auth ,
7579 nameOrId : secretPath [ 0 ] ,
7680 secretName : secretPath [ 1 ] ,
77- secretContent : content . toString ( 'binary' ) ,
81+ secretContent : stdin ,
7882 } ) ,
7983 meta ,
8084 ) ;
@@ -85,4 +89,4 @@ class CommandUpdate extends CommandPolykey {
8589 }
8690}
8791
88- export default CommandUpdate ;
92+ export default CommandWrite ;
0 commit comments