Class: Triglav::Agent::Base::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/triglav/agent/base/setting.rb

Overview

A base class represents settings coming from config.yml and cli options

You usually do not need to customize this class, but if you want to implement your original, configure

Triglav::Agent::Configuration.setting_class =

Constant Summary

DEFAULT_LOG =
'STDOUT'.freeze
DEFAULT_LOG_LEVEL =
'info'.freeze
DEFAULT_APP_ENV =
'development'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_options = {}) ⇒ Setting

Returns a new instance of Setting



21
22
23
24
25
26
27
# File 'lib/triglav/agent/base/setting.rb', line 21

def initialize(cli_options = {})
  @cli_options = cli_options
  if dotenv?
    require 'dotenv'
    Dotenv.load
  end
end

Instance Attribute Details

#cli_optionsObject (readonly)

Returns the value of attribute cli_options



15
16
17
# File 'lib/triglav/agent/base/setting.rb', line 15

def cli_options
  @cli_options
end

Instance Method Details

#[](key) ⇒ Object



86
87
88
# File 'lib/triglav/agent/base/setting.rb', line 86

def [](key)
  config[key]
end

#app_envObject



36
37
38
# File 'lib/triglav/agent/base/setting.rb', line 36

def app_env
  @app_env ||= ENV['APP_ENV'] || DEFAULT_APP_ENV
end

#config_fileObject



40
41
42
# File 'lib/triglav/agent/base/setting.rb', line 40

def config_file
  @cli_options[:config]
end

#debug?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/triglav/agent/base/setting.rb', line 56

def debug?
  @cli_options[:debug]
end

#dig(*args) ⇒ Object



90
91
92
# File 'lib/triglav/agent/base/setting.rb', line 90

def dig(*args)
  config.dig(*args)
end

#dotenv?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/triglav/agent/base/setting.rb', line 52

def dotenv?
  @cli_options[:dotenv]
end

#logObject



68
69
70
# File 'lib/triglav/agent/base/setting.rb', line 68

def log
  @cli_options[:log] || config.dig(:serverengine, :log) || DEFAULT_LOG
end

#log_levelObject



64
65
66
# File 'lib/triglav/agent/base/setting.rb', line 64

def log_level
  @cli_options[:log_level] || config.dig(:serverengine, :log_level) || DEFAULT_LOG_LEVEL
end

#loggerObject



60
61
62
# File 'lib/triglav/agent/base/setting.rb', line 60

def logger
  @logger ||= Logger.new(log, serverengine_logger_options)
end

#reloadObject



29
30
31
32
33
34
# File 'lib/triglav/agent/base/setting.rb', line 29

def reload
  Dotenv.overload if dotenv?
  @config = nil
  @logger.close rescue nil
  @logger = nil
end

#serverengine_optionsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/triglav/agent/base/setting.rb', line 72

def serverengine_options
  serverengine_options = config[:serverengine].dup || {}
  # serverengine_options[:workers] = 1 # default
  # serverengine_options[:worker_type] = 'embedded' # default
  serverengine_options.keys.each do |k|
    serverengine_options.delete(k) if k.to_s.start_with?('log')
  end
  serverengine_options.merge!({
    logger: logger,
    log_level: log_level,
    setting: self,
  })
end

#status_fileObject



44
45
46
# File 'lib/triglav/agent/base/setting.rb', line 44

def status_file
  @status_file ||= @cli_options[:status] || config.dig(:triglav, :status_file) || 'status.yml'
end

#token_fileObject



48
49
50
# File 'lib/triglav/agent/base/setting.rb', line 48

def token_file
  @token_file ||= @cli_options[:token] || config.dig(:triglav, :token_file) || 'token.yml'
end