Method: Morpheus::Cli::CliRegistry.exec_expression

Defined in:
lib/morpheus/cli/cli_registry.rb

.exec_expression(input) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/morpheus/cli/cli_registry.rb', line 103

def exec_expression(input)
  # puts "exec_expression(#{input})"
  flow = input
  if input.is_a?(String)
    begin
      flow = Morpheus::Cli::ExpressionParser.parse(input)
    rescue Morpheus::Cli::ExpressionParser::InvalidExpression => e
      raise e
    end
  end
  # puts "executing flow: #{flow.inspect}"
  final_command_result = nil
  if flow.size == 0
    # no input eh?
  else
    last_command_result = nil
    if ['&&','||', '|'].include?(flow.first)
      raise Morpheus::Cli::ExpressionParser::InvalidExpression.new "#{Morpheus::Terminal.angry_prompt}invalid command format, begins with an operator: #{input}"
    elsif ['&&','||', '|'].include?(flow.last)
      raise Morpheus::Cli::ExpressionParser::InvalidExpression.new "#{Morpheus::Terminal.angry_prompt}invalid command format, ends with an operator: #{input}"
    # elsif ['&&','||', '|'].include?(flow.last)
    #   raise Morpheus::Cli::ExpressionParser::InvalidExpression.new "invalid command format, consecutive operators: #{cmd}"
    else
      #Morpheus::Logging::DarkPrinter.puts "Executing command flow: #{flow.inspect}" if Morpheus::Logging.debug?
      previous_command = nil
      previous_command_result = nil
      current_operator = nil
      still_executing = true
      flow.each do |flow_cmd|
        if still_executing
          if flow_cmd == '&&'
            # AND operator
            current_operator = flow_cmd
            exit_code, cmd_err = parse_command_result(previous_command_result)
            if exit_code != 0
              still_executing = false
            end
          elsif flow_cmd == '||' # or with previous command
            current_operator = flow_cmd
            exit_code, err = parse_command_result(previous_command_result)
            if exit_code == 0
              still_executing = false
            end
          elsif flow_cmd == '|' # or with previous command
            raise Morpheus::Cli::ExpressionParser::InvalidExpression.new "The PIPE (|) operator is not yet supported =["
            previous_command_result = nil
            still_executing = false
            # or just continue?
          elsif flow_cmd.is_a?(Array)
            # this is a subexpression, execute it as such
            current_operator = nil
            previous_command_result = exec_expression(flow_cmd)
          else # it's a command, not an operator
            current_operator = nil
            flow_argv = Shellwords.shellsplit(flow_cmd)
            previous_command_result = exec_command(flow_argv[0], flow_argv[1..-1])
          end
          previous_command = flow_cmd
        else
          #Morpheus::Logging::DarkPrinter.puts "operator skipped command: #{flow_cmd}" if Morpheus::Logging.debug?
        end
        # previous_command = flow_cmd
      end
      final_command_result = previous_command_result
    end
  end
  return final_command_result
end