lib/morph-cli.rb in morph-cli-0.1.1 vs lib/morph-cli.rb in morph-cli-0.2

- old
+ new

@@ -3,29 +3,46 @@ module MorphCLI def self.execute(directory, development, env_config) puts "Uploading and running..." file = MorphCLI.create_tar(directory, MorphCLI.all_paths(directory)) + buffer = "" block = Proc.new do |http_response| - http_response.read_body do |line| - unless line.empty? - a = JSON.parse(line) - if a["stream"] == "stdout" - s = $stdout - elsif a["stream"] == "stderr" - s = $stderr - else - raise "Unknown stream" + if http_response.code == "200" + http_response.read_body do |line| + before, match, after = line.rpartition("\n") + buffer += before + match + buffer.split("\n").each do |l| + log(l) end - s.puts a["text"] + buffer = after end + elsif http_response.code == "401" + raise RestClient::Unauthorized + else + puts http_response.body + exit(1) end end result = RestClient::Request.execute(:method => :post, :url => "#{env_config[:base_url]}/run", :payload => {:api_key => env_config[:api_key], :code => file}, :block_response => block) end - + + def self.log(line) + unless line.empty? + a = JSON.parse(line) + if a["stream"] == "stdout" + s = $stdout + elsif a["stream"] == "stderr" + s = $stderr + else + raise "Unknown stream" + end + s.puts a["text"] + end + end + def self.config_path File.join(Dir.home, ".morph") end def self.save_config(config) @@ -83,10 +100,10 @@ if File.basename(path)[0] == ?. Find.prune end else result << Pathname.new(path).relative_path_from(Pathname.new(directory)).to_s - end + end end result end # Relative path of database file (if it exists)