morph 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env ruby
  2. # Commandline client for controlling morph and running scrapers and things
  3. require "thor"
  4. require "rest_client"
  5. # TODO Do compression on the tar file
  6. #require 'zlib'
  7. require 'archive/tar/minitar'
  8. require 'pathname'
  9. require 'json'
  10. require 'morph-cli'
  11. class MorphThor < Thor
  12. class_option :dev, default: false, type: :boolean, desc: "Run against development Morph (for morph developers)"
  13. desc "[execute]", "execute morph scraper"
  14. option :directory, :default => Dir.getwd
  15. def execute
  16. config = MorphCLI.load_config
  17. if options[:dev]
  18. env_config = config[:development]
  19. else
  20. env_config = config[:production]
  21. end
  22. config = ask_and_save_api_key(env_config, config) if env_config[:api_key].nil?
  23. api_key_is_valid = false
  24. until api_key_is_valid
  25. begin
  26. MorphCLI.execute(options[:directory], options[:dev], env_config)
  27. api_key_is_valid = true
  28. rescue RestClient::Unauthorized
  29. puts "Your key isn't working. Let's try again."
  30. config = ask_and_save_api_key(env_config, config)
  31. rescue Errno::ECONNREFUSED => e
  32. $stderr.puts "Morph doesn't look to be running at #{env_config[:base_url]} (#{e})"
  33. exit(1)
  34. rescue RestClient::InternalServerError => e
  35. $stderr.puts "Uh oh. Something has gone wrong on the Morph server at #{env_config[:base_url]} (#{e})"
  36. exit(1)
  37. end
  38. end
  39. end
  40. desc "version", "Show Morph version number and quit"
  41. def version
  42. puts "Morph CLI #{MorphCLI::VERSION}"
  43. exit
  44. end
  45. no_commands {
  46. def ask_and_save_api_key(env_config, config)
  47. env_config[:api_key] = ask("What is your key? (Go to #{env_config[:base_url]}/settings)")
  48. MorphCLI.save_config(config)
  49. config
  50. end
  51. }
  52. end
  53. # If morph is run without any parameters it's the same as "morph execute"
  54. MorphThor.start(ARGV.empty? ? ["execute"] : ARGV)