Class: Autowow::Command

Inherits:
Object
  • Object
show all
Includes:
EasyLogging
Defined in:
lib/autowow/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Command

Returns a new instance of Command.



31
32
33
# File 'lib/autowow/command.rb', line 31

def initialize(*args)
  @cmd = args
end

Instance Attribute Details

#wait_thrObject (readonly)

Returns the value of attribute wait_thr.



29
30
31
# File 'lib/autowow/command.rb', line 29

def wait_thr
  @wait_thr
end

Class Method Details

.popen3_reader(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/autowow/command.rb', line 16

def self.popen3_reader(*args)
  args.each do |arg|
    reader = "      def \#{arg}\n        @\#{arg} = @\#{arg}.read.rstrip unless @\#{arg}.is_a?(String)\n        return @\#{arg}\n      end\n    EOF\n    class_eval(reader)\n  end\nend\n"

.run(*args) ⇒ Object



8
9
10
# File 'lib/autowow/command.rb', line 8

def self.run(*args)
  Command.new(*args).check.explain.chronic_execute
end

.run_dry(*args) ⇒ Object



12
13
14
# File 'lib/autowow/command.rb', line 12

def self.run_dry(*args)
  Command.new(*args).silent_check.execute
end

Instance Method Details

#checkObject



60
61
62
63
64
# File 'lib/autowow/command.rb', line 60

def check
  silent_check do
    logger.info("Skipping '#{@cmd.join(' ')}' because command '#{@cmd[0]}' is not found.")
  end
end

#chronic_executeObject



45
46
47
48
49
# File 'lib/autowow/command.rb', line 45

def chronic_execute
  @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(*@cmd) unless @cmd.empty?
  logger.error(stderr) unless stderr.empty?
  self
end

#executeObject



40
41
42
43
# File 'lib/autowow/command.rb', line 40

def execute
  @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(*@cmd) unless @cmd.empty?
  self
end

#explainObject



35
36
37
38
# File 'lib/autowow/command.rb', line 35

def explain
  logger.debug(@cmd.join(' ')) unless @cmd.empty?
  self
end

#not_empty_matcherObject



74
75
76
# File 'lib/autowow/command.rb', line 74

def not_empty_matcher
  %r{.*\S.*}
end

#output_does_not_match?(matcher) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/autowow/command.rb', line 70

def output_does_not_match?(matcher)
  !output_matches?(matcher)
end

#output_matches?(matcher) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/autowow/command.rb', line 66

def output_matches?(matcher)
  stdout.match(matcher)
end

#silent_checkObject



51
52
53
54
55
56
57
58
# File 'lib/autowow/command.rb', line 51

def silent_check
  @stdin, @stdout, @stderr, @wait_thr = Open3.popen3('which', @cmd[0])
  unless output_matches?(not_empty_matcher)
    yield if block_given?
    @cmd = []
  end
  self
end