Class: Keel::GCloud::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/keel/gcloud/cli.rb

Overview

A helper class to run system commands and handle interrupts.

Constant Summary collapse

@@wait_sequence =
%w[| / - \\]

Instance Method Summary collapse

Instance Method Details

#execute(command) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/keel/gcloud/cli.rb', line 9

def execute command
  begin
    out = ""
    Open3.popen3(command) do |stdout, stderr, stdin, thread|
      show_wait_spinner{
        while line=stderr.gets do
          out += line
        end
      }
      raise "error while processing. " + out unless thread.value.success?
      return out
    end
  rescue Interrupt
    puts 'Task interrupted.'
  end
end

#show_wait_spinner(fps = 10) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/keel/gcloud/cli.rb', line 35

def show_wait_spinner(fps=10)
  chars = %w[| / - \\]
  delay = 1.0/fps
  iter = 0
  spinner = Thread.new do
    while iter do  # Keep spinning until told otherwise
      print chars[(iter+=1) % chars.length]
      sleep delay
      print "\b"
    end
  end
  yield.tap{       # After yielding to the block, save the return value
    iter = false   # Tell the thread to exit, cleaning up after itself…
    spinner.join   # …and wait for it to do so.
  }                # Use the block's return value as the method's
end

#system_call(command) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/keel/gcloud/cli.rb', line 26

def system_call command
  begin
    system command
  rescue Interrupt
    puts 'Task interrupted.'
  end
end