require 'tty/which' require_relative 'cli/command_factory' module Dockerun # Generate docker cli string class DockerCli include TR::CondUtils def self.docker_exe exe = TTY::Which.which("docker") raise Error, "Docker executable cannot be found. Please make sure it is installed and in the PATH" if is_empty?(exe) exe end def initialize(config, dfile) @_config = config @_dfile = dfile end def set_container_name(val) @name = val end def container_name @name.nil? ? "" : @name end def is_container_name_given? not_empty?(container_name) end def generated_container_name if @_gen_cont_name.nil? @_gen_cont_name = "#{File.basename(Dir.getwd)}_cont_name" end @_gen_cont_name end def keep(bool = true) @keep = bool end def keep_container? @keep.nil? ? true : @keep end def interactive(bool = true) @interactive = bool end def is_interactive? @interactive.nil? ? false : true end def mount(*mnt) mnt.each do |mm| mm.each do |on_host, on_docker| case on_host when :current_dir hostPath = Dir.getwd else hostPath = File.expand_path(on_host) end req_mounts[hostPath] = on_docker end end end def mounts req_mounts end def port(*ports) ports.each do |port| raise Error, "Hash of hostPort => dockerPort is required" if not port.is_a?(Hash) port.each do |hostPort, dockerPort| expose_ports[hostPort] = dockerPort end end end def expose_ports if @_ports.nil? @_ports = {} end @_ports end def has_exposed_ports? expose_ports.size > 0 end def listener(&block) @listener = block end def run_in_docker(cmd) @cmdToBeRun = cmd end def command_to_be_run @cmdToBeRun end def trigger_listener(evt, *args, &block) if not @listener.nil? args << { docker_cli: self } @listener.call(evt, *args, &block) end end private def method_missing(mtd, *args, &block) if @_config.respond_to?(mtd) @_config.send(mtd, *args, &block) end end def req_mounts if @_req_mounts.nil? @_req_mounts = {} end @_req_mounts end end end