Class: Balmora::Command

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

Defined Under Namespace

Classes: Commands, Error, Exec, File, Files, Link, Links, Pacman, ReloadConfig, Restart, SetVariable, Stop, Yaourt

Instance Method Summary collapse

Constructor Details

#initialize(state, command) ⇒ Command

Returns a new instance of Command.



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

def initialize(state, command)
  @state = state

  @logger = state.logger
  @shell = state.shell
  @config = state.config
  @contexts = state.contexts
  @variables = state.variables
  @balmora = state.balmora

  @command = command
end

Instance Method Details

#_executeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/balmora/command.rb', line 54

def _execute()
  if @command.instance_of?(::Hash) && !@contexts.check(@command[:context])
    @logger.debug("Skip: #{@command.inspect()}")
    return
  end

  init()

  if !@require.nil?()
    option(:require).each() { |file|
      require file
    }
  end

  @logger.debug("Run: #{@command.inspect()}")

  execute = self.method(:run)

  if !@sudo.nil?()
    execute_sudo = execute
    execute = Proc.new() {
      @shell.sudo!(option(:sudo)) {
        execute_sudo.call()
      }
    }
  end

  execute.call()
rescue => error
  @logger.error("#{error.inspect()}; failed to run " +
    "command: #{@command.inspect()}")

  if @fallback
    @logger.debug("Executing fallback for command #{@command.inspect()}")
    @balmora.execute(@fallback)
  end

  if option(:ignore_error) == true
    return
  end

  raise error
end

#executeObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/balmora/command.rb', line 41

def execute()
  if @command.has_key?(:chdir)
    dir = @command[:chdir]
    dir = @variables.inject(dir)
    dir = @shell.expand(dir)
    Dir.chdir(dir) {
      _execute()
    }
  else
    _execute()
  end
end

#initObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/balmora/command.rb', line 18

def init()
  if (@command.keys() - [:command] - options()).length != 0
    raise Error.new("Unknown options #{(@command.keys() - [:command] -
      options()).inspect()}")
  end

  options().each() { |key|
    if self.instance_variable_defined?(:"@#{key}")
      raise Error.new("Can not use #{key} as option")
    end

    option = @command.fetch(key, nil)
    self.instance_variable_set(:"@#{key}", option)
  }

  verify()
end

#option(option) ⇒ Object



102
103
104
# File 'lib/balmora/command.rb', line 102

def option(option)
  return @variables.inject(self.instance_variable_get(:"@#{option}"))
end

#optionsObject



36
37
38
39
# File 'lib/balmora/command.rb', line 36

def options()
  return [:ignore_error, :fallback, :extensions, :sudo, :context, :chdir,
    :require]
end

#runObject

Raises:



98
99
100
# File 'lib/balmora/command.rb', line 98

def run()
  raise Error.new("Run should be implemented in subclass")
end