11import type PolykeyClient from '../../PolykeyClient' ;
2+ import type { IdentityId , ProviderId } from '../../identities/types' ;
23import CommandPolykey from '../CommandPolykey' ;
34import * as binOptions from '../utils/options' ;
45import * as binUtils from '../utils' ;
6+ import * as parsers from '../utils/parsers' ;
57import * as binProcessors from '../utils/processors' ;
68
79class CommandSearch extends CommandPolykey {
@@ -10,13 +12,32 @@ class CommandSearch extends CommandPolykey {
1012 this . name ( 'search' ) ;
1113 this . description ( 'Searches a Provider for any Connected Identities' ) ;
1214 this . argument (
13- '<providerId>' ,
14- 'Name of the digital identity provider to search on' ,
15+ '[searchTerms...]' ,
16+ 'Search parameters to apply to connected identities' ,
17+ ) ;
18+ this . option (
19+ '-pi, --provider-id [providerId...]' ,
20+ 'Digital identity provider(s) to search on' ,
21+ parsers . parseProviderIdList ,
22+ ) ;
23+ this . option (
24+ '-ii, --identity-id [identityId]' ,
25+ 'Name of the digital identity to search for' ,
26+ parsers . parseIdentityId ,
27+ ) ;
28+ this . option (
29+ '-d, --disconnected' ,
30+ 'Include disconnected identities in search' ,
31+ ) ;
32+ this . option (
33+ '-l, --limit [number]' ,
34+ 'Limit the number of search results to display to a specific number' ,
35+ parsers . parseInteger ,
1536 ) ;
1637 this . addOption ( binOptions . nodeId ) ;
1738 this . addOption ( binOptions . clientHost ) ;
1839 this . addOption ( binOptions . clientPort ) ;
19- this . action ( async ( providerId , options ) => {
40+ this . action ( async ( searchTerms , options ) => {
2041 const { default : PolykeyClient } = await import ( '../../PolykeyClient' ) ;
2142 const identitiesPB = await import (
2243 '../../proto/js/polykey/v1/identities/identities_pb'
@@ -34,7 +55,11 @@ class CommandSearch extends CommandPolykey {
3455 this . fs ,
3556 ) ;
3657 let pkClient : PolykeyClient ;
58+ let genReadable : ReturnType <
59+ typeof pkClient . grpcClient . identitiesInfoConnectedGet
60+ > ;
3761 this . exitHandlers . handlers . push ( async ( ) => {
62+ if ( genReadable != null ) genReadable . stream . cancel ( ) ;
3863 if ( pkClient != null ) await pkClient . stop ( ) ;
3964 } ) ;
4065 try {
@@ -45,25 +70,48 @@ class CommandSearch extends CommandPolykey {
4570 port : clientOptions . clientPort ,
4671 logger : this . logger . getChild ( PolykeyClient . name ) ,
4772 } ) ;
48- const providerMessage = new identitiesPB . Provider ( ) ;
49- providerMessage . setProviderId ( providerId ) ;
50- const res = await binUtils . retryAuthentication (
51- ( auth ) =>
52- pkClient . grpcClient . identitiesInfoGet ( providerMessage , auth ) ,
53- meta ,
54- ) ;
55- let output = '' ;
56- if ( res . getIdentityId ( ) && res . getProviderId ( ) ) {
57- output = `${ res . getProviderId ( ) } :${ res . getIdentityId ( ) } ` ;
73+ const providerSearchMessage = new identitiesPB . ProviderSearch ( ) ;
74+ providerSearchMessage . setSearchTermList ( searchTerms ) ;
75+ if ( options . providerId ) {
76+ providerSearchMessage . setProviderIdList ( options . providerId ) ;
77+ }
78+ if ( options . disconnected ) {
79+ providerSearchMessage . setDisconnected ( true ) ;
5880 } else {
59- this . logger . info ( 'No Connected Identities found for Provider' ) ;
81+ providerSearchMessage . setDisconnected ( false ) ;
82+ }
83+ if ( options . limit ) {
84+ providerSearchMessage . setLimit ( options . limit ) ;
6085 }
61- process . stdout . write (
62- binUtils . outputFormatter ( {
63- type : options . format === 'json' ? 'json' : 'list' ,
64- data : [ output ] ,
65- } ) ,
66- ) ;
86+ await binUtils . retryAuthentication ( async ( auth ) => {
87+ if ( options . identity ) {
88+ providerSearchMessage . setIdentityId ( options . identity ) ;
89+ genReadable = pkClient . grpcClient . identitiesInfoGet (
90+ providerSearchMessage ,
91+ auth ,
92+ ) ;
93+ } else {
94+ genReadable = pkClient . grpcClient . identitiesInfoConnectedGet (
95+ providerSearchMessage ,
96+ auth ,
97+ ) ;
98+ }
99+ for await ( const val of genReadable ) {
100+ const output = {
101+ providerId : val . getProvider ( ) ! . getProviderId ( ) as ProviderId ,
102+ identityId : val . getProvider ( ) ! . getIdentityId ( ) as IdentityId ,
103+ name : val . getName ( ) ,
104+ email : val . getEmail ( ) ,
105+ url : val . getUrl ( ) ,
106+ } ;
107+ process . stdout . write (
108+ binUtils . outputFormatter ( {
109+ type : options . format === 'json' ? 'json' : 'dict' ,
110+ data : output ,
111+ } ) ,
112+ ) ;
113+ }
114+ } , meta ) ;
67115 } finally {
68116 if ( pkClient ! != null ) await pkClient . stop ( ) ;
69117 }
0 commit comments