|
#--
|
|
# CyborgHood, a distributed system management software.
|
|
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#++
|
|
|
|
module CyborgHood
|
|
class Command
|
|
attr_reader :cmdline, :cmdsplit, :parsing_errors
|
|
|
|
def initialize(cmdline, cmdsplit, parsing_errors = [])
|
|
@cmdline = cmdline
|
|
@cmdsplit = cmdsplit
|
|
@parsing_errors = parsing_errors
|
|
end
|
|
|
|
def valid?
|
|
@parsing_errors.empty?
|
|
end
|
|
end
|
|
|
|
class SharedParameter
|
|
attr_reader :content_type, :content
|
|
|
|
def initialize(content, content_type = nil)
|
|
@content = content
|
|
@content_type = content_type
|
|
end
|
|
end
|
|
|
|
class ParameterReference
|
|
attr_reader :reference
|
|
|
|
def initialize(reference)
|
|
@reference = reference
|
|
end
|
|
end
|
|
|
|
class Order
|
|
include I18nTranslation
|
|
attr_reader :error, :user, :commands, :shared_parameters
|
|
|
|
def initialize(params = {})
|
|
@error = params[:error]
|
|
@user = params[:user]
|
|
@commands = params[:commands]
|
|
@shared_parameters = params[:shared_parameters]
|
|
end
|
|
|
|
def self.parse(user, command_lines, shared_parameters)
|
|
logger.debug "Parsing Order"
|
|
used_refs = []
|
|
commands = []
|
|
command_lines.each do |line|
|
|
line.strip!
|
|
# skip empty lines and comments
|
|
next if line == "" or line[0, 1] == "#"
|
|
|
|
errors = []
|
|
begin
|
|
raw_cmd_parts = line.shellsplit
|
|
rescue
|
|
errors << _("Syntax error in command.")
|
|
raw_cmd_parts = []
|
|
end
|
|
cmd_parts = raw_cmd_parts.collect do |word|
|
|
yield word, errors, used_refs
|
|
end
|
|
|
|
commands << Command.new(line, cmd_parts, errors)
|
|
end
|
|
|
|
# remove references to unused parameters
|
|
shared_parameters.delete_if{|ref, param| not used_refs.include?(ref) }
|
|
|
|
logger.debug "Order is OK"
|
|
new(:user => user, :commands => commands, :shared_parameters => shared_parameters)
|
|
end
|
|
|
|
def self.dereference_param(shared_parameters, param, ref = nil)
|
|
if param.is_a? SharedParameter
|
|
[ref, param]
|
|
elsif param.is_a? ParameterReference
|
|
d_ref = param.reference
|
|
d_param = shared_parameters[d_ref]
|
|
return dereference_param(shared_parameters, d_param, d_ref)
|
|
else
|
|
nil
|
|
end
|
|
end
|
|
|
|
def valid?
|
|
@error.nil? and @user and not @commands.empty?
|
|
end
|
|
end
|
|
end
|