require 'tty/prompt' require_relative 'cli/run' require_relative 'cli/delete_container' require_relative 'cli/delete_image' module Dockerun class CliEngine include TR::ArgUtils arg_spec do opt "i", "Initialize sample dockerspec at given directory as next argument" do |v| init(v) end opt_alias "i", "init" opt "r", "Run the dockerspec at given directory as next argument" do |v| run(v) end opt_alias "r","run" opt "dc", "Delete container at given directory as next argument" do |v| delete_container(v) end opt "di", "Delete image and its associated container(s) at given directory as next argument" do |v| delete_image(v) end opt 'clean', "Clean generated Dockerfile and temporary files at given directory as next argument" do |v| clean_env(v) end opt 'help', "Command help" do print_help end end def print_help pmt = self.class.pmt pmt.puts "" pmt.puts " Dockerun version #{Dockerun::VERSION}".magenta pmt.puts "" pmt.puts " Supported options : " self.class.arg_options.each do |key, val| pmt.puts " #{key}\t\t#{val[:desc]}" end pmt.puts "" end def self.select_spec(root) raise InsufficientParameter, "Given path cannot be empty or nil" if is_empty?(root) #sel = Dir.entries(Dir.getwd).sort sel = Dir.entries(root).sort default = sel.select { |e| (e =~ /^dockerspec/) != nil } selSpec = pmt.select(" Please select the dockerspec to proceed : ", filter: true, default: default.first) do |m| sel.each do |s| next if sel == "." or sel == ".." or File.directory?(s) m.choice s, File.expand_path(s) end end selSpec end def init(root) raise InsufficientParameter, "Given path cannot be empty or nil" if is_empty?(root) root = File.expand_path(root) template = File.join(File.dirname(__FILE__),"..","..","dockerspec.sample") if File.exist?(template) #FileUtils.cp template, File.join(Dir.getwd, "dockerspec.sample") FileUtils.cp template, File.join(root, "dockerspec.sample") end end def run(root) raise InsufficientParameter, "Given path cannot be empty or nil" if is_empty?(root) root = File.expand_path(root) r = Dockerun::Cli::Run.new r.start(root) end def delete_container(root) raise InsufficientParameter, "Given path cannot be empty or nil" if is_empty?(root) root = File.expand_path(root) d = Dockerun::Cli::DeleteContainer.new d.start(root) end def delete_image(root) raise InsufficientParameter, "Given path cannot be empty or nil" if is_empty?(root) root = File.expand_path(root) d = Dockerun::Cli::DeleteImage.new d.start(root) end def clean_env(root) raise InsufficientParameter, "Given path cannot be empty or nil" if is_empty?(root) root = File.expand_path(root) Dir.glob(File.join(root,"Dockerfile-*")).each do |f| FileUtils.rm(f) end pa = File.join(root,"script_for_gem.sh") FileUtils.rm(pa) if File.exist?(pa) self.class.pmt.puts " Generated Dockerfile-* and script_for_gem.sh is deleted at '#{root}'".green end def self.pmt if @_pmt.nil? @_pmt = TTY::Prompt.new end @_pmt end end end