@@ -2,7 +2,11 @@ import colors from 'yoctocolors-cjs'
22
33import { Spinner } from '@socketsecurity/registry/lib/spinner'
44
5- import { handleApiCall , handleUnsuccessfulApiResponse } from '../../utils/api'
5+ import {
6+ getLastFiveOfApiToken ,
7+ handleApiCall ,
8+ handleUnsuccessfulApiResponse
9+ } from '../../utils/api'
610import { AuthError } from '../../utils/errors'
711import { getDefaultToken , setupSdk } from '../../utils/sdk'
812
@@ -15,7 +19,6 @@ export async function getOrganization(
1519 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
1620 )
1721 }
18-
1922 await printOrganizationsFromToken ( apiToken , format )
2023}
2124
@@ -30,69 +33,74 @@ async function printOrganizationsFromToken(
3033 'looking up organizations'
3134 )
3235
33- if ( result . success === false ) {
36+ if ( ! result . success ) {
3437 handleUnsuccessfulApiResponse ( 'getOrganizations' , result , spinner )
3538 return
3639 }
3740
38- spinner . stop ( '' )
41+ spinner . stop ( )
3942
4043 const organizations = Object . values ( result . data . organizations )
44+ const lastFiveOfApiToken = getLastFiveOfApiToken ( apiToken )
4145
42- if ( format === 'json' ) {
43- const obj = Array . from ( organizations ) . map ( o => ( {
44- name : o . name ,
45- id : o . id ,
46- plan : o . plan
47- } ) )
48- console . log ( JSON . stringify ( obj , null , 2 ) )
49- return
50- }
51-
52- if ( format === 'markdown' ) {
53- // | Syntax | Description |
54- // | ----------- | ----------- |
55- // | Header | Title |
56- // | Paragraph | Text |
57-
58- let mw1 = 4
59- let mw2 = 2
60- let mw3 = 4
61- for ( const o of organizations ) {
62- mw1 = Math . max ( mw1 , ( o ?. name || '' ) . length )
63- mw2 = Math . max ( mw2 , ( o ?. id || '' ) . length )
64- mw3 = Math . max ( mw3 , ( o ?. plan || '' ) . length )
46+ switch ( format ) {
47+ case 'json' : {
48+ console . log (
49+ JSON . stringify (
50+ organizations . map ( o => ( {
51+ name : o . name ,
52+ id : o . id ,
53+ plan : o . plan
54+ } ) ) ,
55+ null ,
56+ 2
57+ )
58+ )
59+ return
6560 }
66-
67- console . log ( '# Organizations\n' )
68- console . log (
69- `List of organizations associated with your API key, ending with: ${ colors . italic ( apiToken . slice ( - 10 , - 4 ) ) } \n`
70- )
71- console . log (
72- `| Name${ ' ' . repeat ( mw1 - 4 ) } | ID${ ' ' . repeat ( mw2 - 2 ) } | Plan${ ' ' . repeat ( mw3 - 4 ) } |`
73- )
74- console . log (
75- `| ${ '-' . repeat ( mw1 ) } | ${ '-' . repeat ( mw2 ) } | ${ '-' . repeat ( mw3 ) } |`
76- )
77- for ( const o of organizations ) {
61+ case 'markdown' : {
62+ // | Syntax | Description |
63+ // | ----------- | ----------- |
64+ // | Header | Title |
65+ // | Paragraph | Text |
66+ let mw1 = 4
67+ let mw2 = 2
68+ let mw3 = 4
69+ for ( const o of organizations ) {
70+ mw1 = Math . max ( mw1 , o . name . length )
71+ mw2 = Math . max ( mw2 , o . id . length )
72+ mw3 = Math . max ( mw3 , o . plan . length )
73+ }
74+ console . log ( '# Organizations\n' )
7875 console . log (
79- `| ${ ( o ?. name || '' ) . padEnd ( mw1 , ' ' ) } | ${ ( o ?. id || '' ) . padEnd ( mw2 , ' ' ) } | ${ ( o ?. plan || '' ) . padEnd ( mw3 , ' ' ) } | `
76+ `List of organizations associated with your API key, ending with: ${ colors . italic ( lastFiveOfApiToken ) } \n `
8077 )
78+ console . log (
79+ `| Name${ ' ' . repeat ( mw1 - 4 ) } | ID${ ' ' . repeat ( mw2 - 2 ) } | Plan${ ' ' . repeat ( mw3 - 4 ) } |`
80+ )
81+ console . log (
82+ `| ${ '-' . repeat ( mw1 ) } | ${ '-' . repeat ( mw2 ) } | ${ '-' . repeat ( mw3 ) } |`
83+ )
84+ for ( const o of organizations ) {
85+ console . log (
86+ `| ${ ( o . name || '' ) . padEnd ( mw1 , ' ' ) } | ${ ( o . id || '' ) . padEnd ( mw2 , ' ' ) } | ${ ( o . plan || '' ) . padEnd ( mw3 , ' ' ) } |`
87+ )
88+ }
89+ console . log (
90+ `| ${ '-' . repeat ( mw1 ) } | ${ '-' . repeat ( mw2 ) } | ${ '-' . repeat ( mw3 ) } |`
91+ )
92+ return
93+ }
94+ default : {
95+ console . log (
96+ `List of organizations associated with your API key, ending with: ${ colors . italic ( lastFiveOfApiToken ) } \n`
97+ )
98+ // Just dump
99+ for ( const o of organizations ) {
100+ console . log (
101+ `- Name: ${ colors . bold ( o . name ) } , ID: ${ colors . bold ( o . id ) } , Plan: ${ colors . bold ( o . plan ) } `
102+ )
103+ }
81104 }
82- console . log (
83- `| ${ '-' . repeat ( mw1 ) } | ${ '-' . repeat ( mw2 ) } | ${ '-' . repeat ( mw3 ) } |`
84- )
85- return
86- }
87-
88- console . log (
89- `List of organizations associated with your API key, ending with: ${ colors . italic ( apiToken . slice ( - 10 , - 4 ) ) } \n`
90- )
91-
92- // Just dump
93- for ( const o of organizations ) {
94- console . log (
95- `- Name: ${ colors . bold ( o ?. name ) } , ID: ${ colors . bold ( o ?. id ) } , Plan: ${ colors . bold ( o ?. plan ) } `
96- )
97105 }
98106}
0 commit comments