Class: Wip::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/command_builder.rb

Overview

Builds the argument arrays for wslc build/exec/run/custom invocations.

Instance Method Summary collapse

Constructor Details

#initialize(wslc:, config:, environment: Environment.new) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.



8
9
10
11
12
# File 'lib/wip/command_builder.rb', line 8

def initialize(wslc:, config:, environment: Environment.new)
  @wslc = wslc
  @config = config
  @environment = environment
end

Instance Method Details

#build(settings:, extra: []) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
# File 'lib/wip/command_builder.rb', line 58

def build(settings:, extra: [])
  values = @config.defaults.merge(settings)
  context = values['context'] || '.'
  tag = values['tag'] || values['image']
  raise ConfigError, 'Build image/tag must not be empty' if tag.to_s.empty?

  [@wslc, 'build', '-t', tag, *extra, context]
end

#custom(name, arguments) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/wip/command_builder.rb', line 67

def custom(name, arguments)
  values = @config.command(name) || raise(ConfigError, "Unknown command: #{name}")
  type = values['type'] || (name.to_s == 'build' ? 'build' : 'exec')
  base = Shellwords.split(values['command'].to_s)
  return build(settings: values, extra: arguments) if type == 'build'

  public_send(type, base + arguments, settings: values, interactive: values.fetch('interactive', false))
end

#downObject



50
51
52
# File 'lib/wip/command_builder.rb', line 50

def down
  [@wslc, 'stop', required(@config.defaults, 'container')]
end

#exec(arguments, settings: {}, interactive: true) ⇒ Object



14
15
16
17
18
19
# File 'lib/wip/command_builder.rb', line 14

def exec(arguments, settings: {}, interactive: true)
  values = @config.defaults.merge(settings)
  command = [@wslc, 'exec']
  command << '-it' if tty?(interactive)
  command.concat(options(values, include_container: true, include_publish: false)).concat(arguments)
end

#findObject



45
46
47
48
# File 'lib/wip/command_builder.rb', line 45

def find
  container = required(@config.defaults, 'container')
  [@wslc, 'list', '--all', '--filter', "name=#{container}", '--format', 'json']
end

#removeObject



54
55
56
# File 'lib/wip/command_builder.rb', line 54

def remove
  [@wslc, 'remove', '-f', required(@config.defaults, 'container')]
end

#run(arguments, settings: {}, interactive: true) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/wip/command_builder.rb', line 21

def run(arguments, settings: {}, interactive: true)
  values = @config.defaults.merge(settings)
  command = [@wslc, 'run']
  command << '--rm' if values['remove']
  command << '-it' if tty?(interactive)
  command.concat(options(values)).push(required(values, 'image')).concat(arguments)
end

#start(detach: false) ⇒ Object



39
40
41
42
43
# File 'lib/wip/command_builder.rb', line 39

def start(detach: false)
  command = [@wslc, 'start', required(@config.defaults, 'container')]
  command.push('-a', '-i') unless detach
  command
end

#tty?(requested) ⇒ Boolean

Returns:

  • (Boolean)


76
# File 'lib/wip/command_builder.rb', line 76

def tty?(requested) = requested && @environment.interactive?

#up(detach: false) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/wip/command_builder.rb', line 29

def up(detach: false)
  values = @config.defaults
  command = [@wslc, 'run', '--name', required(values, 'container')]
  command << '-d' if detach
  command << '-it' if !detach && tty?(true)
  command.concat(options(values)).push(required(values, 'image'))
  command.concat(Shellwords.split(@config.up_command.to_s)) if @config.up_command
  command
end