Class: Wip::CommandBuilder
- Inherits:
-
Object
- Object
- Wip::CommandBuilder
- Defined in:
- lib/wip/command_builder.rb
Overview
Builds the argument arrays for wslc build/exec/run/custom invocations.
Instance Method Summary collapse
- #build(settings:, extra: []) ⇒ Object
- #custom(name, arguments) ⇒ Object
- #dependency_down(name) ⇒ Object
- #dependency_find(name) ⇒ Object
- #dependency_remove(name) ⇒ Object
- #dependency_start(name) ⇒ Object
- #dependency_up(name, detach: true) ⇒ Object
- #down ⇒ Object
- #exec(arguments, settings: {}, interactive: true) ⇒ Object
- #find ⇒ Object
-
#initialize(wslc:, config:, environment: Environment.new, dotenv: {}) ⇒ CommandBuilder
constructor
A new instance of CommandBuilder.
- #network_create ⇒ Object
- #network_list ⇒ Object
- #remove ⇒ Object
- #run(arguments, settings: {}, interactive: true) ⇒ Object
- #start(detach: false) ⇒ Object
- #tty?(requested) ⇒ Boolean
- #up(detach: false) ⇒ Object
Constructor Details
#initialize(wslc:, config:, environment: Environment.new, dotenv: {}) ⇒ CommandBuilder
Returns a new instance of CommandBuilder.
8 9 10 11 12 13 |
# File 'lib/wip/command_builder.rb', line 8 def initialize(wslc:, config:, environment: Environment.new, dotenv: {}) @wslc = wslc @config = config @environment = environment @dotenv = dotenv end |
Instance Method Details
#build(settings:, extra: []) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/wip/command_builder.rb', line 94 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
103 104 105 106 107 108 109 110 |
# File 'lib/wip/command_builder.rb', line 103 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 |
#dependency_down(name) ⇒ Object
86 87 88 |
# File 'lib/wip/command_builder.rb', line 86 def dependency_down(name) [@wslc, 'stop', name.to_s] end |
#dependency_find(name) ⇒ Object
82 83 84 |
# File 'lib/wip/command_builder.rb', line 82 def dependency_find(name) [@wslc, 'list', '--all', '--filter', "name=#{name}", '--format', 'json'] end |
#dependency_remove(name) ⇒ Object
90 91 92 |
# File 'lib/wip/command_builder.rb', line 90 def dependency_remove(name) [@wslc, 'remove', '-f', name.to_s] end |
#dependency_start(name) ⇒ Object
78 79 80 |
# File 'lib/wip/command_builder.rb', line 78 def dependency_start(name) [@wslc, 'start', name.to_s] end |
#dependency_up(name, detach: true) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/wip/command_builder.rb', line 68 def dependency_up(name, detach: true) values = dependency_values(name) command = [@wslc, 'run', '--name', name.to_s] command.push('--network', @config.network) if @config.network command << '-d' if detach command.concat((values)).push(required(values, 'image')) command.concat(Shellwords.split(values['command'].to_s)) unless values['command'].to_s.empty? command end |
#down ⇒ Object
52 53 54 |
# File 'lib/wip/command_builder.rb', line 52 def down [@wslc, 'stop', required(@config.defaults, 'container')] end |
#exec(arguments, settings: {}, interactive: true) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/wip/command_builder.rb', line 15 def exec(arguments, settings: {}, interactive: true) values = @config.defaults.merge(settings) command = [@wslc, 'exec'] command << '-it' if tty?(interactive) command.concat((values, include_container: true, include_publish: false)).concat(arguments) end |
#find ⇒ Object
47 48 49 50 |
# File 'lib/wip/command_builder.rb', line 47 def find container = required(@config.defaults, 'container') [@wslc, 'list', '--all', '--filter', "name=#{container}", '--format', 'json'] end |
#network_create ⇒ Object
60 61 62 |
# File 'lib/wip/command_builder.rb', line 60 def network_create [@wslc, 'network', 'create', required_network] end |
#network_list ⇒ Object
64 65 66 |
# File 'lib/wip/command_builder.rb', line 64 def network_list [@wslc, 'network', 'list', '--format', 'json'] end |
#remove ⇒ Object
56 57 58 |
# File 'lib/wip/command_builder.rb', line 56 def remove [@wslc, 'remove', '-f', required(@config.defaults, 'container')] end |
#run(arguments, settings: {}, interactive: true) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/wip/command_builder.rb', line 22 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((values)).push(required(values, 'image')).concat(arguments) end |
#start(detach: false) ⇒ Object
41 42 43 44 45 |
# File 'lib/wip/command_builder.rb', line 41 def start(detach: false) command = [@wslc, 'start', required(@config.defaults, 'container')] command.push('-a', '-i') unless detach command end |
#tty?(requested) ⇒ Boolean
112 |
# File 'lib/wip/command_builder.rb', line 112 def tty?(requested) = requested && @environment.interactive? |
#up(detach: false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/wip/command_builder.rb', line 30 def up(detach: false) values = @config.defaults command = [@wslc, 'run', '--name', required(values, 'container')] command.push('--network', @config.network) if @config.network command << '-d' if detach command << '-it' if !detach && tty?(true) command.concat((values)).push(required(values, 'image')) command.concat(Shellwords.split(@config.up_command.to_s)) if @config.up_command command end |