Class: Foruiman::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/foruiman/configuration.rb

Constant Summary collapse

OPTIONS =
%w[procfile root env dotenv port log_lines timeout exit_on tui].freeze
BOOLEAN_OPTIONS =
%w[dotenv tui].freeze
PATH_OPTIONS =
%w[procfile root env].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options, directory: Dir.pwd) ⇒ Configuration

Returns a new instance of Configuration.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/foruiman/configuration.rb', line 10

def initialize(options, directory: Dir.pwd)
  @directory = directory
  @options = defaults.merge(options.to_h.transform_keys { |key| key.to_s.tr("-", "_") })
  BOOLEAN_OPTIONS.each do |key|
    next unless @options.key?(key)
    next if [true, false].include?(@options[key])

    raise Foruiman::Error, "#{key}: expected true or false"
  end
  PATH_OPTIONS.each do |key|
    next unless @options.key?(key)
    next if @options[key].is_a?(String) && !@options[key].empty?

    raise Foruiman::Error, "#{key}: expected a nonempty path"
  end
end

Instance Method Details

#environment ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/foruiman/configuration.rb', line 36

def environment
  # Foreman loads the default environment before inferring a root from -f.
  env_root = File.expand_path(@options.fetch("root", "."), @directory)
  files = @options["env"]&.split(",", -1)&.map do |file|
    raise Foruiman::Error, "env: expected comma-separated file paths" if file.empty?

    File.expand_path(file, @directory)
  end
  Foruiman::Env.load(root: env_root, file: files, dotenv: @options.fetch("dotenv", true))
end

#exit_on ⇒ Object

Raises:



62
63
64
65
66
67
# File 'lib/foruiman/configuration.rb', line 62

def exit_on
  value = @options.fetch("exit_on", "all")
  return value.to_sym if %w[all any failure].include?(value)

  raise Foruiman::Error, "exit-on must be all, any, or failure"
end

#log_lines ⇒ Object



51
52
53
# File 'lib/foruiman/configuration.rb', line 51

def log_lines
  integer("log_lines", @options.fetch("log_lines", "10000"))
end

#port(env) ⇒ Object



47
48
49
# File 'lib/foruiman/configuration.rb', line 47

def port(env)
  integer("port", @options.fetch("port") { env.fetch("PORT", "5000") })
end

#procfile ⇒ Object



27
28
29
30
# File 'lib/foruiman/configuration.rb', line 27

def procfile
  # Foreman's explicit -f is relative to invocation, independently of -d.
  File.expand_path(@options.fetch("procfile", File.join(@options.fetch("root", "."), "Procfile")), @directory)
end

#root ⇒ Object



32
33
34
# File 'lib/foruiman/configuration.rb', line 32

def root
  File.expand_path(@options.fetch("root", File.dirname(procfile)), @directory)
end

#timeout ⇒ Object

Raises:



55
56
57
58
59
60
# File 'lib/foruiman/configuration.rb', line 55

def timeout
  value = Float(@options.fetch("timeout", 5), exception: false)
  return value if value&.finite? && value >= 0

  raise Foruiman::Error, "timeout must be a finite nonnegative number"
end

#tui? ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/foruiman/configuration.rb', line 69

def tui?
  @options.fetch("tui", true)
end