Class: Dip::Commands::Run

Inherits:
Dip::Command show all
Defined in:
lib/dip/commands/run.rb

Instance Method Summary collapse

Methods inherited from Dip::Command

shell, subshell

Constructor Details

#initialize(cmd, subcmd = nil, argv = []) ⇒ Run

Returns a new instance of Run.



10
11
12
13
14
15
# File 'lib/dip/commands/run.rb', line 10

def initialize(cmd, subcmd = nil, argv = [])
  @cmd = cmd.to_sym
  @subcmd = subcmd.to_sym if subcmd
  @argv = argv
  @config = ::Dip.config.interaction
end

Instance Method Details

#executeObject

TODO: Refactor rubocop:disable Metrics/AbcSize, Metrics/MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dip/commands/run.rb', line 19

def execute
  command = @config.fetch(@cmd)
  command[:subcommands] ||= {}

  if (subcommand = command[:subcommands].fetch(@subcmd, {})).any?
    subcommand[:command] ||= nil
    command.merge!(subcommand)
  elsif @subcmd
    @argv.unshift(@subcmd.to_s)
  end

  Dip.env.merge(command[:environment]) if command[:environment]

  compose_method = command.fetch(:compose_method, "run").to_s

  compose_argv = []
  compose_argv.concat(prepare_compose_run_options(command[:compose_run_options]))
  compose_argv.concat(run_vars)
  compose_argv << "--rm" if compose_method == "run"
  compose_argv << command.fetch(:service).to_s
  compose_argv += command[:command].to_s.shellsplit
  compose_argv.concat(@argv)

  Dip::Commands::Compose.new(compose_method, compose_argv).execute
end

#prepare_compose_run_options(value) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



46
47
48
49
50
51
52
53
# File 'lib/dip/commands/run.rb', line 46

def prepare_compose_run_options(value)
  return [] unless value

  value.map do |o|
    o = o.start_with?("-") ? o : "--#{o}"
    o.shellsplit
  end.flatten
end

#run_varsObject



55
56
57
58
59
60
# File 'lib/dip/commands/run.rb', line 55

def run_vars
  run_vars = Dip::RunVars.env
  return [] unless run_vars

  run_vars.map { |k, v| ["-e", "#{k}=#{v}"] }.flatten
end