Module: Kettle::Dev::OpenCollectiveConfig

Defined in:
lib/kettle/dev/open_collective_config.rb

Overview

Shared utility for resolving Open Collective configuration for this repository.
Centralizes the logic for locating and reading .opencollective.yml and
for deriving the handle from environment or the YAML file.

Class Method Summary collapse

Class Method Details

.handle(required: false, root: nil, strict: false) ⇒ String?

Determine the Open Collective handle.
Precedence:
1) ENV[“OPENCOLLECTIVE_HANDLE”] when set and non-empty
2) .opencollective.yml key “collective” (or :collective)

Parameters:

  • required (Boolean) (defaults to: false)

    when true, aborts the process if not found; when false, returns nil

  • root (String, nil) (defaults to: nil)

    optional project root to look for .opencollective.yml

Returns:

  • (String, nil)

    the handle, or nil when not required and not discoverable



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kettle/dev/open_collective_config.rb', line 29

def handle(required: false, root: nil, strict: false)
  env = ENV["OPENCOLLECTIVE_HANDLE"]
  return env unless env.nil? || env.to_s.strip.empty?

  ypath = yaml_path(root)
  if strict
    yml = YAML.safe_load(File.read(ypath))
    if yml.is_a?(Hash)
      handle = yml["collective"] || yml[:collective] || yml["org"] || yml[:org]
      return handle.to_s unless handle.nil? || handle.to_s.strip.empty?
    end
  elsif File.file?(ypath)
    begin
      yml = YAML.safe_load(File.read(ypath))
      if yml.is_a?(Hash)
        handle = yml["collective"] || yml[:collective] || yml["org"] || yml[:org]
        return handle.to_s unless handle.nil? || handle.to_s.strip.empty?
      end
    rescue StandardError => e
      Kettle::Dev.debug_error(e, __method__) if Kettle::Dev.respond_to?(:debug_error)
      # fall through to required check
    end
  end

  if required
    Kettle::Dev::ExitAdapter.abort("ERROR: Open Collective handle not provided. Set OPENCOLLECTIVE_HANDLE or add 'collective: <handle>' to .opencollective.yml.")
  end
  nil
end

.yaml_path(root = nil) ⇒ String

Absolute path to a .opencollective.yml

Parameters:

  • root (String, nil) (defaults to: nil)

    optional project root to resolve against; when nil, uses this repo root

Returns:

  • (String)


16
17
18
19
# File 'lib/kettle/dev/open_collective_config.rb', line 16

def yaml_path(root = nil)
  return File.expand_path(".opencollective.yml", root) if root
  File.expand_path("../../../.opencollective.yml", __dir__)
end