@@ -87,6 +87,7 @@ def run # rubocop:disable Metrics/AbcSize
8787 c . option '--verbose' , 'Enables verbose output'
8888 c . option '--service STRING' , String , 'Configured pooler service name'
8989 c . option '--active' , 'Prints information about active vms for a given token'
90+ c . option '--json' , 'Prints information as JSON'
9091 c . option '--token STRING' , String , 'Token for pooler service'
9192 c . option '--url STRING' , String , 'URL of pooler service'
9293 c . action do |args , options |
@@ -100,10 +101,18 @@ def run # rubocop:disable Metrics/AbcSize
100101 running_vms = service . list_active ( verbose )
101102 host = URI . parse ( service . url ) . host
102103 if running_vms . empty?
103- puts "You have no running VMs on #{ host } "
104+ if options . json
105+ puts { } . to_json
106+ else
107+ FloatyLogger . info "You have no running VMs on #{ host } "
108+ end
104109 else
105- puts "Your VMs on #{ host } :"
106- Utils . pretty_print_hosts ( verbose , service , running_vms )
110+ if options . json
111+ puts Utils . get_host_data ( verbose , service , running_vms ) . to_json
112+ else
113+ puts "Your VMs on #{ host } :"
114+ Utils . pretty_print_hosts ( verbose , service , running_vms )
115+ end
107116 end
108117 else
109118 # list available vms from pooler
@@ -200,6 +209,7 @@ def run # rubocop:disable Metrics/AbcSize
200209 c . option '--service STRING' , String , 'Configured pooler service name'
201210 c . option '--all' , 'Deletes all vms acquired by a token'
202211 c . option '-f' , 'Does not prompt user when deleting all vms'
212+ c . option '--json' , 'Outputs hosts scheduled for deletion as JSON'
203213 c . option '--token STRING' , String , 'Token for pooler service'
204214 c . option '--url STRING' , String , 'URL of pooler service'
205215 c . action do |args , options |
@@ -215,12 +225,18 @@ def run # rubocop:disable Metrics/AbcSize
215225 if delete_all
216226 running_vms = service . list_active ( verbose )
217227 if running_vms . empty?
218- puts 'You have no running VMs.'
228+ if options . json
229+ puts { } . to_json
230+ else
231+ FloatyLogger . info "You have no running VMs."
232+ end
219233 else
220- Utils . pretty_print_hosts ( verbose , service , running_vms , true )
221- # Confirm deletion
222234 confirmed = true
223- confirmed = agree ( 'Delete all these VMs? [y/N]' ) unless force
235+ unless force
236+ Utils . pretty_print_hosts ( verbose , service , running_vms , true )
237+ # Confirm deletion
238+ confirmed = agree ( 'Delete all these VMs? [y/N]' )
239+ end
224240 if confirmed
225241 response = service . delete ( verbose , running_vms )
226242 response . each do |hostname , result |
@@ -257,9 +273,15 @@ def run # rubocop:disable Metrics/AbcSize
257273
258274 unless successes . empty?
259275 FloatyLogger . info unless failures . empty?
260- puts 'Scheduled the following VMs for deletion:'
261- successes . each do |hostname |
262- puts "- #{ hostname } "
276+ if options . json
277+ puts successes . to_json
278+ else
279+ puts 'Scheduled the following VMs for deletion:'
280+ output = ''
281+ successes . each do |hostname |
282+ output += "- #{ hostname } \n "
283+ end
284+ puts output
263285 end
264286 end
265287
0 commit comments