Class: Meathook::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/meathook/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, hn, opts) ⇒ Command

Returns a new instance of Command.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/meathook/command.rb', line 7

def initialize(n, hn, opts)
  self.name = n
  self.hookname = hn

  self.command   = opts.fetch('command')
  self.condition = opts.fetch('condition')
  self.halt      = opts['halt'] || false
  self.message   = opts["message"]

  self.match     = false
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



4
5
6
# File 'lib/meathook/command.rb', line 4

def command
  @command
end

#conditionObject

Returns the value of attribute condition.



4
5
6
# File 'lib/meathook/command.rb', line 4

def condition
  @condition
end

#haltObject

Returns the value of attribute halt.



4
5
6
# File 'lib/meathook/command.rb', line 4

def halt
  @halt
end

#hooknameObject

Returns the value of attribute hookname.



3
4
5
# File 'lib/meathook/command.rb', line 3

def hookname
  @hookname
end

#matchObject

Returns the value of attribute match.



5
6
7
# File 'lib/meathook/command.rb', line 5

def match
  @match
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/meathook/command.rb', line 4

def message
  @message
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/meathook/command.rb', line 3

def name
  @name
end

Instance Method Details

#executeObject



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
# File 'lib/meathook/command.rb', line 19

def execute
  output = `#{command}`
  exit_code = ($?)

  case condition
  when "empty"        then self.match = (output =~ /\S/ ? false : true)
  when "nonempty"     then self.match = (output =~ /\S/ ? true : false)
  when "failed"       then self.match = (exit_code != 0)
  when "successful"   then self.match = (exit_code == 0)
  else                raise("The condition '#{condition}' was not recognized")
  end

  if self.match
    if self.message and self.message =~ /\S/
      warn ""
      warn "============================================================"
      warn "    Git Hook '#{hookname}' :: '#{name}' triggered"
      warn ""
      warn "  #{message}"
      warn ""
    end

    if self.halt
      exit(1)
    else
      exit(0)
    end
  end
end