1- import {
2- cliux ,
3- configHandler ,
4- CLITable ,
5- TableFlags ,
6- FlagInput ,
7- handleAndLogError ,
8- log ,
9- } from '@contentstack/cli-utilities' ;
1+ import { Help } from '@oclif/core' ;
102import { BaseCommand } from '../../../base-command' ;
11- export default class TokensListCommand extends BaseCommand < typeof TokensListCommand > {
12- static aliases = [ 'tokens' ] ;
13- static examples = [ '$ csdx auth:tokens' ] ;
14- static description = 'Lists all existing tokens added to the session' ;
3+ import { CLITable , FlagInput } from '@contentstack/cli-utilities' ;
4+
5+ export default class AuthTokensCommand extends BaseCommand < typeof AuthTokensCommand > {
6+ static description = 'Manage authentication tokens for API access' ;
7+ static strict = false ;
8+
9+ static examples = [
10+ '$ csdx auth:tokens:list' ,
11+ '$ csdx auth:tokens:add --alias mytoken' ,
12+ '$ csdx auth:tokens:remove --alias mytoken' ,
13+ ] ;
14+
1515 static flags : FlagInput = CLITable . getTableFlags ( [
1616 'columns' ,
1717 'sort' ,
@@ -20,65 +20,11 @@ export default class TokensListCommand extends BaseCommand<typeof TokensListComm
2020 'no-truncate' ,
2121 'no-header' ,
2222 'output' ,
23- ] ) ; // use the cli table flags as it displays tokens in table
23+ ] ) ;
2424
2525 async run ( ) : Promise < any > {
26- log . debug ( 'TokensListCommand run method started.' , this . contextDetails ) ;
27- this . contextDetails . module = 'tokens-list' ;
28-
29- try {
30- log . debug ( 'Retrieving tokens from configuration.' , this . contextDetails ) ;
31- const managementTokens = configHandler . get ( 'tokens' ) ;
32- log . debug ( 'Tokens retrieved from configuration.' , { ...this . contextDetails , tokenCount : managementTokens ? Object . keys ( managementTokens ) . length : 0 } ) ;
33-
34- const tokens : Record < string , unknown > [ ] = [ ] ;
35- if ( managementTokens && Object . keys ( managementTokens ) . length > 0 ) {
36- log . debug ( 'Processing tokens for display.' , this . contextDetails ) ;
37- Object . keys ( managementTokens ) . forEach ( function ( item ) {
38- tokens . push ( {
39- alias : item ,
40- token : managementTokens [ item ] . token ,
41- apiKey : managementTokens [ item ] . apiKey ,
42- environment : managementTokens [ item ] . environment ? managementTokens [ item ] . environment : '-' ,
43- type : managementTokens [ item ] . type ,
44- } ) ;
45- log . debug ( `Token processed: ${ item } ` , { tokenType : managementTokens [ item ] . type } ) ;
46- } ) ;
47-
48- const { flags } = await this . parse ( TokensListCommand ) ;
49- log . debug ( 'Token list flags parsed.' , { ...this . contextDetails , flags } ) ;
50-
51- const headers = [
52- {
53- value : 'alias' ,
54- } ,
55- {
56- value : 'token' ,
57- } ,
58- {
59- value : 'apiKey' ,
60- } ,
61- {
62- value : 'environment' ,
63- } ,
64- {
65- value : 'type' ,
66- } ,
67- ] ;
68-
69- log . debug ( 'Displaying token table.' , { ...this . contextDetails , tokenCount : tokens . length } ) ;
70- cliux . table ( headers , tokens , flags as TableFlags ) ;
71- log . debug ( 'Token table displayed successfully.' , this . contextDetails ) ;
72- } else {
73- log . debug ( 'No tokens found in configuration.' , this . contextDetails ) ;
74- cliux . print ( 'CLI_AUTH_TOKENS_LIST_NO_TOKENS' ) ;
75- }
76-
77- log . debug ( 'Token list command completed successfully.' , this . contextDetails ) ;
78- } catch ( error ) {
79- log . debug ( 'Token list command failed.' , { ...this . contextDetails , error } ) ;
80- cliux . print ( 'CLI_AUTH_TOKENS_LIST_FAILED' , { color : 'yellow' } ) ;
81- handleAndLogError ( error , { ...this . contextDetails } ) ;
82- }
26+ await this . parse ( AuthTokensCommand , this . argv . filter ( ( a ) => a !== '-help' && a !== '-h' ) ) ;
27+ const cmd = this . config . findCommand ( 'auth:tokens' ) ;
28+ if ( cmd ) await new Help ( this . config ) . showCommandHelp ( cmd ) ;
8329 }
8430}
0 commit comments