1- import { Command } from "../command" ;
21const Table = require ( "cli-table" ) ;
2+
3+ import { Command } from "../command" ;
34import * as clc from "colorette" ;
45import * as ora from "ora" ;
56
67import { logger } from "../logger" ;
78import { requirePermissions } from "../requirePermissions" ;
8- import { needProjectNumber } from "../projectUtils" ;
9- import * as firedata from "../gcp/firedata" ;
109import { Emulators } from "../emulator/types" ;
1110import { warnEmulatorNotSupported } from "../emulator/commandUtils" ;
1211import * as experiments from "../experiments" ;
@@ -18,71 +17,48 @@ import {
1817 parseDatabaseLocation ,
1918} from "../management/database" ;
2019
21- function logInstances ( instances : DatabaseInstance [ ] ) : void {
22- if ( instances . length === 0 ) {
23- logger . info ( clc . bold ( "No database instances found." ) ) ;
24- return ;
25- }
26- const tableHead = [ "Database Instance Name" , "Location" , "Type" , "State" ] ;
27- const table = new Table ( { head : tableHead , style : { head : [ "green" ] } } ) ;
28- instances . forEach ( ( db ) => {
29- table . push ( [ db . name , db . location , db . type , db . state ] ) ;
30- } ) ;
31-
32- logger . info ( table . toString ( ) ) ;
33- }
34-
35- function logInstancesCount ( count = 0 ) : void {
36- if ( count === 0 ) {
37- return ;
38- }
39- logger . info ( "" ) ;
40- logger . info ( `${ count } database instance(s) total.` ) ;
41- }
42-
43- export let command = new Command ( "database:instances:list" )
20+ export const command = new Command ( "database:instances:list" )
4421 . description ( "list realtime database instances, optionally filtered by a specified location" )
4522 . before ( requirePermissions , [ "firebasedatabase.instances.list" ] )
23+ . option (
24+ "-l, --location <location>" ,
25+ "(optional) location for the database instance, defaults to all regions"
26+ )
4627 . before ( warnEmulatorNotSupported , Emulators . DATABASE )
4728 . action ( async ( options : any ) => {
4829 const location = parseDatabaseLocation ( options . location , DatabaseLocation . ANY ) ;
4930 const spinner = ora (
5031 "Preparing the list of your Firebase Realtime Database instances" +
5132 `${ location === DatabaseLocation . ANY ? "" : ` for location: ${ location } ` } `
5233 ) . start ( ) ;
53- let instances ;
5434
55- if ( experiments . isEnabled ( "rtdbmanagement" ) ) {
56- const projectId = needProjectId ( options ) ;
57- try {
58- instances = await listDatabaseInstances ( projectId , location ) ;
59- } catch ( err : any ) {
60- spinner . fail ( ) ;
61- throw err ;
62- }
63- spinner . succeed ( ) ;
64- logInstances ( instances ) ;
65- logInstancesCount ( instances . length ) ;
66- return instances ;
67- }
68- const projectNumber = await needProjectNumber ( options ) ;
35+ const projectId = needProjectId ( options ) ;
36+ let instances : DatabaseInstance [ ] = [ ] ;
6937 try {
70- instances = await firedata . listDatabaseInstances ( projectNumber ) ;
38+ instances = await listDatabaseInstances ( projectId , location ) ;
7139 } catch ( err : any ) {
7240 spinner . fail ( ) ;
7341 throw err ;
7442 }
7543 spinner . succeed ( ) ;
76- for ( const instance of instances ) {
77- logger . info ( instance . instance ) ;
44+ if ( instances . length === 0 ) {
45+ logger . info ( clc . bold ( "No database instances found." ) ) ;
46+ return ;
47+ }
48+ // TODO: remove rtdbmanagement experiment in the next major release.
49+ if ( ! experiments . isEnabled ( "rtdbmanagement" ) ) {
50+ for ( const instance of instances ) {
51+ logger . info ( instance . name ) ;
52+ }
53+ logger . info ( `Project ${ options . project } has ${ instances . length } database instances` ) ;
54+ return instances ;
55+ }
56+ const tableHead = [ "Database Instance Name" , "Location" , "Type" , "State" ] ;
57+ const table = new Table ( { head : tableHead , style : { head : [ "green" ] } } ) ;
58+ for ( const db of instances ) {
59+ table . push ( [ db . name , db . location , db . type , db . state ] ) ;
7860 }
79- logger . info ( `Project ${ options . project } has ${ instances . length } database instances` ) ;
61+ logger . info ( table . toString ( ) ) ;
62+ logger . info ( `${ instances . length } database instance(s) total.` ) ;
8063 return instances ;
8164 } ) ;
82-
83- if ( experiments . isEnabled ( "rtdbmanagement" ) ) {
84- command = command . option (
85- "-l, --location <location>" ,
86- "(optional) location for the database instance, defaults to us-central1"
87- ) ;
88- }
0 commit comments