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,37 @@ 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+ '-aii, --auth-identity-id, [authIdentityId]' ,
25+ 'Name of your own authenticated identity to find connected identities of' ,
26+ parsers . parseIdentityId ,
27+ ) ;
28+ this . option (
29+ '-ii, --identity-id [identityId]' ,
30+ 'Name of the digital identity to search for' ,
31+ parsers . parseIdentityId ,
32+ ) ;
33+ this . option (
34+ '-d, --disconnected' ,
35+ 'Include disconnected identities in search' ,
36+ ) ;
37+ this . option (
38+ '-l, --limit [number]' ,
39+ 'Limit the number of search results to display to a specific number' ,
40+ parsers . parseInteger ,
1541 ) ;
1642 this . addOption ( binOptions . nodeId ) ;
1743 this . addOption ( binOptions . clientHost ) ;
1844 this . addOption ( binOptions . clientPort ) ;
19- this . action ( async ( providerId , options ) => {
45+ this . action ( async ( searchTerms , options ) => {
2046 const { default : PolykeyClient } = await import ( '../../PolykeyClient' ) ;
2147 const identitiesPB = await import (
2248 '../../proto/js/polykey/v1/identities/identities_pb'
@@ -34,7 +60,11 @@ class CommandSearch extends CommandPolykey {
3460 this . fs ,
3561 ) ;
3662 let pkClient : PolykeyClient ;
63+ let genReadable : ReturnType <
64+ typeof pkClient . grpcClient . identitiesInfoConnectedGet
65+ > ;
3766 this . exitHandlers . handlers . push ( async ( ) => {
67+ if ( genReadable != null ) genReadable . stream . cancel ( ) ;
3868 if ( pkClient != null ) await pkClient . stop ( ) ;
3969 } ) ;
4070 try {
@@ -45,25 +75,49 @@ class CommandSearch extends CommandPolykey {
4575 port : clientOptions . clientPort ,
4676 logger : this . logger . getChild ( PolykeyClient . name ) ,
4777 } ) ;
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 ( ) } ` ;
58- } else {
59- this . logger . info ( 'No Connected Identities found for Provider' ) ;
78+ const providerSearchMessage = new identitiesPB . ProviderSearch ( ) ;
79+ providerSearchMessage . setSearchTermList ( searchTerms ) ;
80+ if ( options . providerId ) {
81+ providerSearchMessage . setProviderIdList ( options . providerId ) ;
82+ }
83+ if ( options . authIdentityId ) {
84+ providerSearchMessage . setAuthIdentityId ( options . authIdentityId ) ;
85+ }
86+ if ( options . disconnected ) {
87+ providerSearchMessage . setDisconnected ( true ) ;
88+ }
89+ if ( options . limit ) {
90+ providerSearchMessage . setLimit ( options . limit ) ;
6091 }
61- process . stdout . write (
62- binUtils . outputFormatter ( {
63- type : options . format === 'json' ? 'json' : 'list' ,
64- data : [ output ] ,
65- } ) ,
66- ) ;
92+ await binUtils . retryAuthentication ( async ( auth ) => {
93+ if ( options . identity ) {
94+ providerSearchMessage . setIdentityId ( options . identity ) ;
95+ genReadable = pkClient . grpcClient . identitiesInfoGet (
96+ providerSearchMessage ,
97+ auth ,
98+ ) ;
99+ } else {
100+ genReadable = pkClient . grpcClient . identitiesInfoConnectedGet (
101+ providerSearchMessage ,
102+ auth ,
103+ ) ;
104+ }
105+ for await ( const val of genReadable ) {
106+ const output = {
107+ providerId : val . getProvider ( ) ! . getProviderId ( ) as ProviderId ,
108+ identityId : val . getProvider ( ) ! . getIdentityId ( ) as IdentityId ,
109+ name : val . getName ( ) ,
110+ email : val . getEmail ( ) ,
111+ url : val . getUrl ( ) ,
112+ } ;
113+ process . stdout . write (
114+ binUtils . outputFormatter ( {
115+ type : options . format === 'json' ? 'json' : 'dict' ,
116+ data : output ,
117+ } ) ,
118+ ) ;
119+ }
120+ } , meta ) ;
67121 } finally {
68122 if ( pkClient ! != null ) await pkClient . stop ( ) ;
69123 }
0 commit comments