Module: Harnex::Config

Defined in:
lib/harnex/config.rb

Defined Under Namespace

Classes: RepoConfig

Constant Summary collapse

CONFIG_RELATIVE_PATH =
File.join(".harnex", "config.json").freeze
PHASE_POLICIES =
%w[warn reject].freeze
RETENTION_DIRS =
%w[events output].freeze
RETENTION_FIELDS =
%w[max_age_days max_bytes].freeze
RETENTION_ENV =
{
  "events" => {
    "max_age_days" => "HARNEX_EVENTS_MAX_AGE_DAYS",
    "max_bytes" => "HARNEX_EVENTS_MAX_BYTES"
  },
  "output" => {
    "max_age_days" => "HARNEX_OUTPUT_MAX_AGE_DAYS",
    "max_bytes" => "HARNEX_OUTPUT_MAX_BYTES"
  }
}.freeze
DEFAULT_RETENTION_LIMITS =
RETENTION_DIRS.to_h do |dir|
  [dir, { "max_age_days" => 45, "max_bytes" => 1_073_741_824 }]
end.freeze
ConfigError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.deep_dup_retention_defaultsObject



162
163
164
# File 'lib/harnex/config.rb', line 162

def deep_dup_retention_defaults
  DEFAULT_RETENTION_LIMITS.transform_values(&:dup)
end

.load_repo(start_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/harnex/config.rb', line 41

def load_repo(start_path)
  root = DispatchHistory.find_git_root(start_path)
  return RepoConfig.new(root: nil, path: nil, data: {}) unless root

  path = File.join(root, CONFIG_RELATIVE_PATH)
  return RepoConfig.new(root: root, path: path, data: {}) unless File.file?(path)

  parsed = JSON.parse(File.read(path))
  validate_document!(parsed, path)
  RepoConfig.new(root: root, path: path, data: parsed)
rescue JSON::ParserError => e
  raise ConfigError, "#{path}: malformed JSON: #{e.message}"
end

.parse_positive_integer!(value, label) ⇒ Object

Raises:



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/harnex/config.rb', line 150

def parse_positive_integer!(value, label)
  text = value.to_s.strip
  unless text.match?(/\A[0-9]+\z/)
    raise ConfigError, "#{label} must be a positive integer"
  end

  integer = Integer(text)
  raise ConfigError, "#{label} must be a positive integer" if integer <= 0

  integer
end

.retention_limits(start_path = Dir.pwd, env: ENV, config: nil) ⇒ Object



88
89
90
91
92
93
94
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
# File 'lib/harnex/config.rb', line 88

def retention_limits(start_path = Dir.pwd, env: ENV, config: nil)
  config ||= load_repo(start_path)
  limits = deep_dup_retention_defaults
  retention = config.retention

  if retention
    RETENTION_DIRS.each do |dir|
      next unless retention.key?(dir)

      RETENTION_FIELDS.each do |field|
        next unless retention.fetch(dir).key?(field)

        limits.fetch(dir)[field] = parse_positive_integer!(
          retention.fetch(dir).fetch(field),
          "#{config.path}: $.retention.#{dir}.#{field}"
        )
      end
    end
  end

  RETENTION_ENV.each do |dir, fields|
    fields.each do |field, name|
      next unless env.key?(name)

      limits.fetch(dir)[field] = parse_positive_integer!(
        env.fetch(name),
        name
      )
    end
  end

  limits
end

.validate_document!(document, path) ⇒ Object



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

def validate_document!(document, path)
  unless document.is_a?(Hash)
    raise ConfigError, "#{path}: config must be a JSON object"
  end

  validate_phase!(document["phase"], path) if document.key?("phase")
  validate_retention!(document["retention"], path) if document.key?("retention")
end

.validate_phase!(phase, path) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/harnex/config.rb', line 64

def validate_phase!(phase, path)
  unless phase.is_a?(Hash)
    raise ConfigError, "#{path}: $.phase must be an object"
  end

  allowlist = phase["allowlist"]
  unless allowlist.is_a?(Array)
    raise ConfigError, "#{path}: $.phase.allowlist must be an array"
  end

  allowlist.each_with_index do |entry, index|
    next if entry.is_a?(String) && !entry.strip.empty?

    raise ConfigError,
      "#{path}: $.phase.allowlist[#{index}] must be a non-empty string"
  end

  policy = phase["policy"]
  unless PHASE_POLICIES.include?(policy)
    raise ConfigError,
      "#{path}: $.phase.policy must be one of #{PHASE_POLICIES.join(', ')}"
  end
end

.validate_retention!(retention, path) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/harnex/config.rb', line 122

def validate_retention!(retention, path)
  unless retention.is_a?(Hash)
    raise ConfigError, "#{path}: $.retention must be an object"
  end

  retention.each do |dir, value|
    unless RETENTION_DIRS.include?(dir)
      raise ConfigError,
        "#{path}: $.retention.#{dir} is not supported (expected events or output)"
    end
    unless value.is_a?(Hash)
      raise ConfigError, "#{path}: $.retention.#{dir} must be an object"
    end

    value.each do |field, limit|
      unless RETENTION_FIELDS.include?(field)
        raise ConfigError,
          "#{path}: $.retention.#{dir}.#{field} is not supported"
      end

      parse_positive_integer!(
        limit,
        "#{path}: $.retention.#{dir}.#{field}"
      )
    end
  end
end