40 lines
893 B
Ruby
Executable file
40 lines
893 B
Ruby
Executable file
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
lib = File.expand_path('../lib', __dir__).freeze
|
|
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
|
|
|
|
require 'json'
|
|
require 'referator'
|
|
|
|
$stdin.sync = true
|
|
$stdout.sync = true
|
|
|
|
def serialize_err(err)
|
|
{
|
|
class: err.class.name.freeze,
|
|
message: err.message.freeze,
|
|
stacktrace: err.backtrace.map(&:freeze).freeze,
|
|
cause: err.cause.nil? ? nil : serialize_err(err.cause),
|
|
}.freeze
|
|
end
|
|
|
|
workdir = ARGV.first
|
|
workdir = Dir.pwd if String(workdir).strip.empty?
|
|
workdir = File.expand_path(workdir).freeze
|
|
|
|
machine = Referator::Machine.new workdir
|
|
|
|
while machine.running?
|
|
begin
|
|
line = $stdin.gets&.strip || 'exit'
|
|
line = 'null' if line.empty?
|
|
|
|
command = Referator::Command.parse line
|
|
result = machine.call command
|
|
|
|
puts "OK #{result.to_json}"
|
|
rescue => e
|
|
puts "ERR #{serialize_err(e).to_json}"
|
|
end
|
|
end
|