#!/usr/bin/env ruby # Commandline client for controlling morph and running scrapers and things require "thor" require "rest_client" # TODO Do compression on the tar file #require 'zlib' require 'archive/tar/minitar' require 'pathname' require 'json' require 'morph-cli' class MorphThor < Thor class_option :dev, default: false, type: :boolean, desc: "Run against development Morph (for morph developers)" desc "[execute]", "execute morph scraper" option :directory, :default => Dir.getwd def execute config = MorphCLI.load_config if options[:dev] env_config = config[:development] else env_config = config[:production] end config = ask_and_save_api_key(env_config, config) if env_config[:api_key].nil? api_key_is_valid = false until api_key_is_valid begin MorphCLI.execute(options[:directory], options[:dev], env_config) api_key_is_valid = true rescue RestClient::Unauthorized puts "Your key isn't working. Let's try again." config = ask_and_save_api_key(env_config, config) rescue Errno::ECONNREFUSED => e $stderr.puts "Morph doesn't look to be running at #{env_config[:base_url]} (#{e})" exit(1) rescue RestClient::InternalServerError => e $stderr.puts "Uh oh. Something has gone wrong on the Morph server at #{env_config[:base_url]} (#{e})" exit(1) end end end desc "version", "Show Morph version number and quit" def version puts "Morph CLI #{MorphCLI::VERSION}" exit end no_commands { def ask_and_save_api_key(env_config, config) env_config[:api_key] = ask("What is your key? (Go to #{env_config[:base_url]}/settings)") MorphCLI.save_config(config) config end } end # If morph is run without any parameters it's the same as "morph execute" MorphThor.start(ARGV.empty? ? ["execute"] : ARGV)