1- // Copyright 2018 Datalust Pty Ltd and Contributors
2- //
3- // Licensed under the Apache License, Version 2.0 (the "License");
4- // you may not use this file except in compliance with the License.
5- // You may obtain a copy of the License at
6- //
7- // http://www.apache.org/licenses/LICENSE-2.0
8- //
9- // Unless required by applicable law or agreed to in writing, software
10- // distributed under the License is distributed on an "AS IS" BASIS,
11- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12- // See the License for the specific language governing permissions and
13- // limitations under the License.
14-
15- using System ;
16- using System . Linq ;
17- using System . Threading . Tasks ;
18- using SeqCli . Cli . Features ;
19- using SeqCli . Connection ;
20- using Serilog ;
21-
22- namespace SeqCli . Cli . Commands . ApiKey ;
23-
24- [ Command ( "apikey" , "remove" , "Remove an API key from the server" ,
25- Example = "seqcli apikey remove -t 'Test API Key'" ) ]
26- class RemoveCommand : Command
27- {
28- readonly SeqConnectionFactory _connectionFactory ;
29-
30- readonly EntityIdentityFeature _entityIdentity ;
31- readonly ConnectionFeature _connection ;
32-
33- public RemoveCommand ( SeqConnectionFactory connectionFactory )
34- {
35- _connectionFactory = connectionFactory ?? throw new ArgumentNullException ( nameof ( connectionFactory ) ) ;
36-
37- _entityIdentity = Enable ( new EntityIdentityFeature ( "API key" , "remove" ) ) ;
38- _connection = Enable < ConnectionFeature > ( ) ;
39- }
40-
41- protected override async Task < int > Run ( )
42- {
43- if ( _entityIdentity . Title == null && _entityIdentity . Id == null )
44- {
45- Log . Error ( "A `title` or `id` must be specified" ) ;
46- return 1 ;
47- }
48-
49- var connection = _connectionFactory . Connect ( _connection ) ;
50-
51- var toRemove = _entityIdentity . Id != null ?
52- new [ ] { await connection . ApiKeys . FindAsync ( _entityIdentity . Id ) } :
53- ( await connection . ApiKeys . ListAsync ( ) )
54- . Where ( ak => _entityIdentity . Title == ak . Title )
55- . ToArray ( ) ;
56-
57- if ( ! toRemove . Any ( ) )
58- {
59- Log . Error ( "No matching API key was found" ) ;
60- return 1 ;
61- }
62-
63- foreach ( var apiKeyEntity in toRemove )
64- await connection . ApiKeys . RemoveAsync ( apiKeyEntity ) ;
65-
66- return 0 ;
67- }
1+ // Copyright 2018 Datalust Pty Ltd and Contributors
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ using System ;
16+ using System . Linq ;
17+ using System . Threading . Tasks ;
18+ using SeqCli . Cli . Features ;
19+ using SeqCli . Connection ;
20+ using Serilog ;
21+
22+ namespace SeqCli . Cli . Commands . ApiKey ;
23+
24+ [ Command ( "apikey" , "remove" , "Remove an API key from the server" ,
25+ Example = "seqcli apikey remove -t 'Test API Key'" ) ]
26+ class RemoveCommand : Command
27+ {
28+ readonly SeqConnectionFactory _connectionFactory ;
29+
30+ readonly EntityIdentityFeature _entityIdentity ;
31+ readonly ConnectionFeature _connection ;
32+
33+ public RemoveCommand ( SeqConnectionFactory connectionFactory )
34+ {
35+ _connectionFactory = connectionFactory ?? throw new ArgumentNullException ( nameof ( connectionFactory ) ) ;
36+
37+ _entityIdentity = Enable ( new EntityIdentityFeature ( "API key" , "remove" ) ) ;
38+ _connection = Enable < ConnectionFeature > ( ) ;
39+ }
40+
41+ protected override async Task < int > Run ( )
42+ {
43+ if ( _entityIdentity . Title == null && _entityIdentity . Id == null )
44+ {
45+ Log . Error ( "A `title` or `id` must be specified" ) ;
46+ return 1 ;
47+ }
48+
49+ var connection = _connectionFactory . Connect ( _connection ) ;
50+
51+ var toRemove = _entityIdentity . Id != null ?
52+ new [ ] { await connection . ApiKeys . FindAsync ( _entityIdentity . Id ) } :
53+ ( await connection . ApiKeys . ListAsync ( ) )
54+ . Where ( ak => _entityIdentity . Title == ak . Title )
55+ . ToArray ( ) ;
56+
57+ if ( ! toRemove . Any ( ) )
58+ {
59+ Log . Error ( "No matching API key was found" ) ;
60+ return 1 ;
61+ }
62+
63+ foreach ( var apiKeyEntity in toRemove )
64+ await connection . ApiKeys . RemoveAsync ( apiKeyEntity ) ;
65+
66+ return 0 ;
67+ }
6868}
0 commit comments