Class: Mayu::Configuration

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mayu/configuration.rb

Defined Under Namespace

Classes: Instance, Metrics, Paths, Server

Constant Summary collapse

CONFIG_FILE =
"mayu.toml"

Class Method Summary collapse

Class Method Details

.colorize_value(value) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/mayu/configuration.rb', line 167

def self.colorize_value(value)
  if color = value_color(value).nonzero?
    "\e[#{color}m#{value.inspect}\e[0m"
  else
    value.inspect
  end
end

.load_config(mode, pwd: Dir.pwd, overrides: {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mayu/configuration.rb', line 95

def self.load_config(mode, pwd: Dir.pwd, overrides: {})
  file = resolve_config_file(pwd)
  root = File.dirname(file)

  config =
    T.cast(
      TomlRB.load_file(file),
      T::Hash[String, T::Hash[String, T.untyped]]
    )

  base_config = config.dig("base") || {}
  env_config = config.dig(mode.to_s) || {}

  merged_config = base_config.merge(env_config).merge(overrides)

  secret_key =
    merged_config.fetch("secret_key") do
      ENV.fetch("MAYU_SECRET_KEY") do
        raise "secret_key is not configured (can be set with env var MAYU_SECRET_KEY)"
      end
    end

  from_hash!(
    {
      **merged_config,
      "root" => root,
      "secret_key" => secret_key,
      "mode" => mode
    }
  )
end

.log_config(configuration) ⇒ Object



128
129
130
# File 'lib/mayu/configuration.rb', line 128

def self.log_config(configuration)
  Console.logger.info(self) { make_table(configuration) }
end

.make_table(configuration, style: { all_separators: true, border: :unicode }) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mayu/configuration.rb', line 138

def self.make_table(
  configuration,
  style: { all_separators: true, border: :unicode }
)
  Terminal::Table
    .new do |t|
      t.headings = %w[key value type].map { "\e[1m#{_1}\e[0m" }
      t.style = style

      configuration.class.props.each do |prop, opts|
        value =
          if prop.to_s.start_with?("secret_")
            "\e[2m***hidden***\e[0m"
          else
            case configuration.send(prop)
            in T::Struct => struct
              make_table(struct, style: { border: :unicode_round })
            in other
              colorize_value(other)
            end
          end

        t.add_row([prop, value, opts[:type].to_s.delete_prefix("Mayu::")])
      end
    end
    .to_s
end

.resolve_config_file(dir = Dir.pwd) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mayu/configuration.rb', line 74

def self.resolve_config_file(dir = Dir.pwd)
  path = File.join(dir, CONFIG_FILE)

  return path if File.file?(path)

  parent = File.expand_path("..", dir)

  if dir == parent
    raise "Could not find #{CONFIG_FILE} in any parent directory."
  end

  resolve_config_file(parent)
end

.value_color(value) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/mayu/configuration.rb', line 176

def self.value_color(value)
  case value
  when FalseClass
    31
  when TrueClass
    32
  when String
    33
  when Numeric
    34
  when Symbol
    36
  when nil
    2
  else
    0
  end
end