require_relative 'command_factory' module Dockerun module Cli class DSLProxy include DSL end class DeleteImage include TR::CondUtils def initialize @proxy = DSLProxy.new end def start(root) @proxy.set_exec_root(root) begin pmt.puts " Dockerun version #{Dockerun::VERSION}".yellow pmt.puts " Operational : Delete Image" delete_image rescue TTY::Reader::InputInterrupt end end def delete_image selSpec = CliEngine.select_spec(@proxy.exec_root) cont = File.read(selSpec) @proxy.set_dry_run_mode @proxy.instance_eval(cont) res = cf.find_image(@proxy.image_name).run if res.success? if not_empty?(res.output_lines) skip = pmt.no?(" Delete image named '#{@proxy.image_name}'?") if not skip res = cf.find_container_names_by_image_name(@proxy.image_name, all_containers: true).run raise Error, "Failed to extract container name by image name '#{@proxy.image_name}'" if not res.success? if not_empty?(res.output_lines) cont = [] cnt = 0 res.output_lines.each do |l| cont << " #{cnt += 1}. #{l}" end skip = pmt.no?(" All the containers shall be deleted prior to image '#{@proxy.image_name}' deletion. Proceed?\n#{cont.join("\n")}") if not skip res.output_lines.each do |ci| cf.stop_container(ci).run cf.delete_container(ci).run end res = cf.delete_image(@proxy.image_name).run if res.success? pmt.puts " Image '#{@proxy.image_name}' deleted".green else raise Error, " Image '#{@proxy.image_name}' deletion failed. Error was : #{res.err_lines.join("\n")}" end else pmt.puts " Delete of image '#{@proxy.image_name}' aborted".yellow end else res = cf.delete_image(@proxy.image_name).run if res.success? pmt.puts " Image '#{@proxy.image_name}' deleted".green else raise Error, " Image '#{@proxy.image_name}' deletion failed. Error was : #{res.err_lines.join("\n")}" end end else pmt.puts " Delete of image '#{@proxy.image_name}' aborted".yellow end else pmt.puts " Image '#{@proxy.image_name}' does not exist".green end else pmt.puts " Failed to find image '#{@proxy.image_name}'. Error was : #{res.err_lines.join("\n")}" end end def pmt CliEngine.pmt end def cf if @_cf.nil? @_cf = CommandFactory.new end @_cf end end end end