Class: Onair::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/onair/config.rb

Overview

Resolution order: CLI flags → env vars → .onair.yml (searched upward from cwd to the git root) → error with a hint to run onair init.

Constant Summary collapse

FILENAME =
".onair.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform:, app:, repo:, branch:, fetch:, task: nil) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
18
19
20
# File 'lib/onair/config.rb', line 13

def initialize(platform:, app:, repo:, branch:, fetch:, task: nil)
  @platform = platform
  @app = app
  @repo = repo
  @branch = branch
  @fetch = fetch
  @task = task
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



11
12
13
# File 'lib/onair/config.rb', line 11

def app
  @app
end

#branchObject (readonly)

Returns the value of attribute branch.



11
12
13
# File 'lib/onair/config.rb', line 11

def branch
  @branch
end

#fetchObject (readonly)

Returns the value of attribute fetch.



11
12
13
# File 'lib/onair/config.rb', line 11

def fetch
  @fetch
end

#platformObject (readonly)

Returns the value of attribute platform.



11
12
13
# File 'lib/onair/config.rb', line 11

def platform
  @platform
end

#repoObject (readonly)

Returns the value of attribute repo.



11
12
13
# File 'lib/onair/config.rb', line 11

def repo
  @repo
end

#taskObject (readonly)

Returns the value of attribute task.



11
12
13
# File 'lib/onair/config.rb', line 11

def task
  @task
end

Class Method Details

.find_file(start) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/onair/config.rb', line 37

def self.find_file(start)
  dir = File.expand_path(start)
  loop do
    path = File.join(dir, FILENAME)
    return YAML.safe_load_file(path) || {} if File.file?(path)
    return nil if File.exist?(File.join(dir, ".git"))

    parent = File.dirname(dir)
    return nil if parent == dir

    dir = parent
  end
end

.resolve(flags = {}, env: ENV, dir: Dir.pwd) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/onair/config.rb', line 22

def self.resolve(flags = {}, env: ENV, dir: Dir.pwd)
  file = find_file(dir) || {}
  app = flags[:app] || env["HEROKU_APP"] || file["app"]
  raise Error, "no app configured — pass --app NAME, set HEROKU_APP, or run `onair init`" if app.nil?

  new(
    platform: file["platform"] || "heroku",
    app: app,
    repo: env["GITHUB_REPO"] || file["repo"],
    branch: file["branch"] || "main",
    fetch: !flags[:no_fetch],
    task: TaskLink.from_config(file["task"])
  )
end