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



52
53
54
55
56
57
# File 'lib/wip/config.rb', line 52

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



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

def compose = @raw['compose']

#compose?Boolean

Returns:

  • (Boolean)


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

def compose? = mode == 'compose'

#compose_commandObject



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

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

#compose_fileObject



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

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

#compose_projectObject



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

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

#compose_serviceObject



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

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. No default: guessing a name here (the old default was "app") either matches by luck or fails in a way that doesn't point at the real problem (a differently-named entry), so a project with any dependencies: must say which one explicitly.



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

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

#dependenciesObject



25
26
27
28
29
30
31
# 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. No default:
# guessing a name here (the old default was "app") either matches by luck or
# fails in a way that doesn't point at the real problem (a differently-named
# entry), so a project with any dependencies: must say which one explicitly.

#dependency(name) ⇒ Object



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

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

  DEPENDENCY_DEFAULTS.merge(entry)
end

#modeObject



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

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

#networkObject



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

def network = @raw['network']

#primaryObject

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



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

def primary = dependency(container)

#syncObject



43
44
45
46
47
# File 'lib/wip/config.rb', line 43

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

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

#sync?Boolean

Returns:

  • (Boolean)


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

def sync? = !!sync

#to_h(redact: true) ⇒ Object



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

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'