Module: MorphCLI
- Defined in:
- lib/morph-cli.rb,
lib/morph-cli/version.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ development: { base_url: "http://127.0.0.1:3000" }, production: { base_url: "https://morph.io" } }
- VERSION =
"0.2.5"
Class Method Summary collapse
-
.all_paths(directory) ⇒ Object
Relative paths to all the files in the given directory (recursive) (except for anything below a directory starting with “.”).
- .config_path ⇒ Object
- .create_tar(directory, paths) ⇒ Object
-
.database_path(directory) ⇒ Object
Relative path of database file (if it exists).
- .execute(directory, development, env_config) ⇒ Object
- .get_dir_size(directory, paths) ⇒ Object
- .in_directory(directory) ⇒ Object
- .load_config ⇒ Object
- .log(line) ⇒ Object
- .save_config(config) ⇒ Object
Class Method Details
.all_paths(directory) ⇒ Object
Relative paths to all the files in the given directory (recursive) (except for anything below a directory starting with “.”)
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/morph-cli.rb', line 125 def self.all_paths(directory) result = [] Find.find(directory) do |path| if FileTest.directory?(path) if File.basename(path)[0] == ?. Find.prune end else result << Pathname.new(path).relative_path_from(Pathname.new(directory)).to_s end end result end |
.config_path ⇒ Object
65 66 67 |
# File 'lib/morph-cli.rb', line 65 def self.config_path File.join(Dir.home, ".morph") end |
.create_tar(directory, paths) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/morph-cli.rb', line 99 def self.create_tar(directory, paths) tempfile = File.new('/tmp/out', 'wb') in_directory(directory) do begin tar = Archive::Tar::Minitar::Output.new("/tmp/out") paths.each do |entry| Archive::Tar::Minitar.pack_file(entry, tar) end ensure tar.close end end File.new('/tmp/out', 'r') end |
.database_path(directory) ⇒ Object
Relative path of database file (if it exists)
140 141 142 143 |
# File 'lib/morph-cli.rb', line 140 def self.database_path(directory) path = "data.sqlite" path if File.exists?(File.join(directory, path)) end |
.execute(directory, development, env_config) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/morph-cli.rb', line 7 def self.execute(directory, development, env_config) all_paths = MorphCLI.all_paths(directory) if !all_paths.find{ |file| /scraper\.[\S]+$/ =~ file } $stderr.puts "Can't find scraper to upload. Expected to find a file called scraper.rb, scraper.php, scraper.py, scraper.pl, scraper.js, etc to upload" exit(1) end size = MorphCLI.get_dir_size(directory, all_paths) puts "Uploading #{size}..." file = MorphCLI.create_tar(directory, all_paths) buffer = "" block = Proc.new do |http_response| 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 buffer = after end elsif http_response.code == "401" raise RestClient::Unauthorized else puts http_response.body exit(1) end end if env_config.key?(:timeout) timeout = env_config[:timeout] else timeout = 600 # 10 minutes should be "enough for everyone", right? # Setting to nil will disable the timeout entirely. # Default is 60 seconds. 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, :timeout => timeout) end |
.get_dir_size(directory, paths) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/morph-cli.rb', line 115 def self.get_dir_size(directory, paths) size = 0 in_directory(directory) do paths.each { |entry| size += File.size(entry) } end Filesize.from("#{size} B").pretty end |
.in_directory(directory) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/morph-cli.rb', line 91 def self.in_directory(directory) cwd = FileUtils.pwd FileUtils.cd(directory) yield ensure FileUtils.cd(cwd) end |
.load_config ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/morph-cli.rb', line 83 def self.load_config if File.exists?(config_path) YAML.load(File.read(config_path)) else DEFAULT_CONFIG end end |
.log(line) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/morph-cli.rb', line 49 def self.log(line) unless line.empty? a = JSON.parse(line) s = case a["stream"] when "stdout", "internalout" $stdout when "stderr" $stderr else raise "Unknown stream" end s.puts a["text"] end end |
.save_config(config) ⇒ Object
69 70 71 72 |
# File 'lib/morph-cli.rb', line 69 def self.save_config(config) File.open(config_path, "w") {|f| f.write config.to_yaml} File.chmod(0600, config_path) end |