@@ -2,15 +2,70 @@ import { Command } from "commander";
22import { loadConfig } from "../helpers/config" ;
33import { logLoginMessageAndQuit } from "../helpers/errors" ;
44import { getApiUrl } from "../helpers/urls" ;
5+ import Table from "cli-table3" ;
6+
7+ interface Contract {
8+ object : string ;
9+ status : string ;
10+ id : string ;
11+ created_at : string ;
12+ instance_type : string ;
13+ shape : {
14+ // These are date strings
15+ intervals : string [ ] ;
16+ quantities : number [ ] ;
17+ } ;
18+ colocate_with : string [ ] ;
19+ cluster_id : string ;
20+ }
21+
22+ function printTable ( data : Contract [ ] ) {
23+ const table = new Table ( {
24+ head : [
25+ "ID" ,
26+ "Status" ,
27+ "Instance Type" ,
28+ // Found by looking at the first interval
29+ "Starts At" ,
30+ // Found by looking at the last interval
31+ "Ends At" ,
32+ ] ,
33+ } ) ;
34+
35+ for ( const contract of data ) {
36+ const startsAt = contract . shape . intervals [ 0 ] ;
37+ const endsAt =
38+ contract . shape . intervals [ contract . shape . intervals . length - 1 ] ;
39+ table . push ( [
40+ contract . id ,
41+ contract . status ,
42+ contract . instance_type ,
43+ new Date ( startsAt ) . toLocaleString ( ) ,
44+ new Date ( endsAt ) . toLocaleString ( ) ,
45+ ] ) ;
46+ }
47+
48+ console . log ( table . toString ( ) ) ;
49+ }
550
651export function registerContracts ( program : Command ) {
752 program
853 . command ( "contracts" )
954 . description ( "Manage contracts" )
1055 . addCommand (
11- new Command ( "list" ) . description ( "List all contracts" ) . action ( async ( ) => {
12- await listContracts ( ) ;
13- } ) ,
56+ new Command ( "list" )
57+ . alias ( "ls" )
58+ . option ( "--json" , "Output in JSON format" )
59+ . description ( "List all contracts" )
60+ . action ( async ( options ) => {
61+ if ( options . json ) {
62+ console . log ( await listContracts ( ) ) ;
63+ } else {
64+ const data = await listContracts ( ) ;
65+ printTable ( data . data ) ;
66+ }
67+ process . exit ( 0 ) ;
68+ } ) ,
1469 ) ;
1570}
1671
@@ -29,5 +84,5 @@ async function listContracts() {
2984 } ) ;
3085
3186 const data = await response . json ( ) ;
32- console . log ( data ) ;
87+ return data ;
3388}
0 commit comments