Class: Mildred::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mildred/runner.rb

Constant Summary collapse

DEFAULT_RULES_PATH =
File.join(Dir.home, ".mildred", "rules.yml")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: DEFAULT_RULES_PATH, noop: false) ⇒ Runner

Returns a new instance of Runner.



11
12
13
14
# File 'lib/mildred/runner.rb', line 11

def initialize(path: DEFAULT_RULES_PATH, noop: false)
  @jobs = Job.from_yaml(path)
  @noop = noop
end

Instance Attribute Details

#jobs ⇒ Object (readonly)

Returns the value of attribute jobs.



9
10
11
# File 'lib/mildred/runner.rb', line 9

def jobs
  @jobs
end

#noop ⇒ Object (readonly)

Returns the value of attribute noop.



9
10
11
# File 'lib/mildred/runner.rb', line 9

def noop
  @noop
end

Instance Method Details

#run ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mildred/runner.rb', line 16

def run
  if @noop
    puts Gum.style("▸ Dry run — no changes will be made", foreground: "220", bold: true)
    puts
  else
    Gum.spin("Cleaning up stale containers...", spinner: :dot) { Container.cleanup_stale }
    Gum.spin("Ensuring sandbox image...", spinner: :dot) { Container.ensure_image }
    puts
  end

  results = @jobs.map do |job|
    puts Gum.style("▸ #{job.name}", foreground: "212", bold: true)

    result = Gum.spin("Running #{job.name}...", spinner: :dot) do
      execute_job(job)
    end

    if result == true
      puts Gum.style("  ✓ Done", foreground: "46")
    else
      puts Gum.style("  ✗ Failed", foreground: "196")
      puts Gum.style("    #{result.message}", foreground: "196", faint: true) if result.is_a?(StandardError)
    end

    result == true
  end

  passed = results.count { |r| r }
  total = results.length
  puts
  if passed == total
    puts Gum.style("✓ #{passed}/#{total} jobs completed", foreground: "46", bold: true)
  else
    puts Gum.style("⚠ #{passed}/#{total} jobs completed", foreground: "220", bold: true)
  end
end