Class: Wip::Config

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

Overview

Validated, defaulted access to a parsed wip.yml document.

Constant Summary collapse

DEPENDENCY_DEFAULTS =

Applied to every dependencies: entry, primary container included — there is no separate, differently-shaped bucket for "the one you exec into."

{ 'workdir' => nil, 'interactive' => false, 'remove' => true,
'env' => {}, 'ports' => [], 'volumes' => [] }.freeze
SECRET_PATTERN =
/token|password|secret|credential|auth/i
MODES =

Which orchestration path up/down/sync/etc. take. Explicit rather than inferred from a compose: block's presence, so a config reader doesn't have to know that rule to predict which mode wip runs in.

%w[container compose].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, path = nil) ⇒ Config

Returns a new instance of Config.



17
18
19
20
21
# File 'lib/wip/config.rb', line 17

def initialize(raw, path = nil)
  @raw = stringify(raw)
  @path = path
  validate!
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/wip/config.rb', line 15

def path
  @path
end

Instance Method Details

#command(name) ⇒ Object



49
50
51
52
53
54
# File 'lib/wip/config.rb', line 49

def command(name)
  entry = commands[name.to_s]
  return unless entry

  (primary || {}).merge('type' => 'exec').merge(entry)
end

#commandsObject



24
# File 'lib/wip/config.rb', line 24

def commands = @raw['commands'] || {}

#composeObject



32
# File 'lib/wip/config.rb', line 32

def compose = @raw['compose']

#compose?Boolean

Returns:

  • (Boolean)


33
# File 'lib/wip/config.rb', line 33

def compose? = mode == 'compose'

#compose_commandObject



37
# File 'lib/wip/config.rb', line 37

def compose_command = compose && compose['command']

#compose_fileObject



35
# File 'lib/wip/config.rb', line 35

def compose_file = compose && compose['file']

#compose_projectObject



36
# File 'lib/wip/config.rb', line 36

def compose_project = compose && compose['project']

#compose_serviceObject



34
# File 'lib/wip/config.rb', line 34

def compose_service = compose && compose['service']

#containerObject

Which dependencies: entry up/down/exec/run/build/commands: target by default — the one container wip itself considers "the app." Everything else in dependencies: is a sidecar wip only starts and stops.



29
# File 'lib/wip/config.rb', line 29

def container = presence(@raw['container']) || 'app'

#dependenciesObject



25
26
27
28
# File 'lib/wip/config.rb', line 25

def dependencies = @raw['dependencies'] || {}
# Which dependencies: entry `up`/`down`/`exec`/`run`/`build`/`commands:` target
# by default — the one container wip itself considers "the app." Everything
# else in dependencies: is a sidecar wip only starts and stops.

#dependency(name) ⇒ Object



56
57
58
59
60
61
# File 'lib/wip/config.rb', line 56

def dependency(name)
  entry = dependencies[name.to_s]
  return unless entry

  DEPENDENCY_DEFAULTS.merge(entry)
end

#modeObject



31
# File 'lib/wip/config.rb', line 31

def mode = @raw['mode'] || 'container'

#networkObject



30
# File 'lib/wip/config.rb', line 30

def network = @raw['network']

#primaryObject

The dependencies: entry container points at, or nil if it isn't defined.



47
# File 'lib/wip/config.rb', line 47

def primary = dependency(container)

#syncObject



40
41
42
43
44
# File 'lib/wip/config.rb', line 40

def sync
  return @sync if defined?(@sync)

  @sync = @raw.key?('sync') ? build_sync : nil
end

#sync?Boolean

Returns:

  • (Boolean)


38
# File 'lib/wip/config.rb', line 38

def sync? = !!sync

#to_h(redact: true) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/wip/config.rb', line 63

def to_h(redact: true)
  value = { 'version' => 1, 'wslc' => { 'command' => wslc_command }, 'mode' => mode, 'container' => container,
            'network' => network, 'dependencies' => dependencies, 'compose' => compose, 'sync' => sync&.to_h,
            'commands' => commands.transform_values do |entry|
              (primary || {}).merge('type' => 'exec').merge(entry)
            end }
  redact ? redact_secrets(value) : value
end

#wslc_commandObject



23
# File 'lib/wip/config.rb', line 23

def wslc_command = @raw.dig('wslc', 'command') || 'auto'