morph-cli 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTg4MTEwYzQ4ZDMyMWVkNjg1ZDk4MDY0YzllZTQ1ODhjOTFlOWQ3MA==
4
+ OWJhOGZjNjA0YmU1ZjliNDA2MjVhZDAxM2U2OWEwZWY1ZmQ2MjM0YQ==
5
5
  data.tar.gz: !binary |-
6
- Y2U0NGJkMDU5MTdhNWNmMjlhYTc3ZTg4ZjI3ZmVkNDllNjdhNjQ5MQ==
6
+ MDNlODNhMzhjYmUzNjFkMTY0MGQ0ZmNjY2E2ZmQxNzFjYTdlOGEzNA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZWE1NDAwYjk4YmIxMmMwMjk0ZDQ0YzliZjExNGZhMjQzZDBhNGMyOWQ0ODQ5
10
- NjY2YTkwNDRlZGIwYWRkYzEwOTk0NWNiYzdiYWMyMjI2ZTE3NDQ5YWY5OWUz
11
- NTU0N2Q3MTg2OGU3OTdlODU4MDdiMzUyNzYzNDg0ZTMzZjlhMTg=
9
+ NTZiZDJiYWVkYTBkOGU2YmZjZTU2ZjY5ZTEyZDY2ZGRlZDhmMzNlMDdlNzAz
10
+ OGIzZjcwNDFlZjkyYjkxZTc5ZGJjMTQ2ZWM0ZWVmOWRiZGQxMzI2MjI2OGU3
11
+ MzZiMGRiMTc4Y2Q5MTViYjc5MTUzYzRhOTc1NWZlNWQyYjM1ZmQ=
12
12
  data.tar.gz: !binary |-
13
- MGM4Y2VkMWFkMmYyMzQ0ZmY0MzBmOTQ4MTBhMzVmYmE5MDdjNzhhYzJmNzQy
14
- YTAwZDAwOTAwYzY4M2Q3YmM3OWY3ODRiYzBjZTJiZjQzNDFmYjFlZjAxODYw
15
- NzU1MTJkZGMyN2ZmNThiZDZiNWJiOTU5ZjkxNDA3MzI1OTI2ZDM=
13
+ ZDVlYWExY2VlN2NmNTg3NzM1MjdkMWM0ZTYzNzY2MzVjNGQ4MTMwNDdhOWE2
14
+ OTlkNDNhZTNmNzNhNDQ2NDAyZWQ0MDRjYTE0YjkwMWRmNTk2YWFlYWFlODZm
15
+ Y2U1OTExZTUwOTc4YTFlOTBjZmY4YTNhNDk2OGEwN2JjNzZjZWM=
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/morph-cli.png)](http://badge.fury.io/rb/morph-cli)
2
+
1
3
  # Morph
2
4
 
3
5
  TODO: Write a gem description
data/bin/morph CHANGED
@@ -8,128 +8,49 @@ require "rest_client"
8
8
  require 'archive/tar/minitar'
9
9
  require 'pathname'
10
10
  require 'json'
11
+ require 'morph-cli'
11
12
 
12
13
  class MorphThor < Thor
13
- class_option :dev, default: false, type: :boolean, desc: "Run against a local dev of Morph running at http://localhost:3000"
14
+ class_option :dev, default: false, type: :boolean, desc: "Run against a local dev of Morph"
14
15
 
15
16
  desc "[execute]", "execute morph scraper"
16
17
  option :directory, :default => Dir.getwd
17
18
 
18
19
  def execute
19
- api_key = retrieve_api_key
20
- if api_key.nil?
21
- api_key = ask("What is your key? (Go to #{base_url(options)}/settings)")
22
- save_api_key(api_key)
20
+ config = MorphCLI.load_config
21
+ if options[:dev]
22
+ env_config = config[:development]
23
+ else
24
+ env_config = config[:production]
23
25
  end
24
26
 
27
+ config = ask_and_save_api_key(env_config, config) if env_config[:api_key].nil?
28
+
25
29
  api_key_is_valid = false
26
30
  until api_key_is_valid
27
31
  begin
28
- puts "Uploading and running..."
29
- file = create_tar(options[:directory], all_paths(options[:directory]))
30
- result = RestClient.post("#{base_url(options)}/run", :api_key => api_key, :code => file)
32
+ MorphCLI.execute(options[:directory], options[:dev], env_config)
31
33
  api_key_is_valid = true
32
34
  rescue RestClient::Unauthorized
33
35
  puts "Your key isn't working. Let's try again."
34
- api_key = ask("What is your key? (Go to #{base_url(options)}/settings)")
35
- save_api_key(api_key)
36
- end
37
- end
38
- # Interpret each line separately as json
39
- result.split("\n").each do |line|
40
- a = JSON.parse(line)
41
- if a["stream"] == "stdout"
42
- s = $stdout
43
- elsif a["stream"] == "stderr"
44
- s = $stderr
45
- else
46
- raise "Unknown stream"
36
+ config = ask_and_save_api_key(env_config, config)
37
+ rescue Errno::ECONNREFUSED => e
38
+ $stderr.puts "Morph doesn't look to be running at #{env_config[:base_url]} (#{e})"
39
+ exit(1)
40
+ rescue RestClient::InternalServerError => e
41
+ $stderr.puts "Uh oh. Something has gone wrong on the Morph server at #{env_config[:base_url]} (#{e})"
42
+ exit(1)
47
43
  end
48
- s.puts a["text"]
49
44
  end
50
45
  end
51
46
 
52
47
  no_commands {
53
- def base_url(options)
54
- if options[:dev]
55
- "http://127.0.0.1:3000"
56
- else
57
- "https://morph.io"
58
- end
59
- end
60
-
61
- def config_path
62
- File.join(Dir.home, ".morph")
63
- end
64
-
65
- def save_api_key(api_key)
66
- configuration = {api_key: api_key}
67
- File.open(config_path, "w") {|f| f.write configuration.to_yaml}
68
- File.chmod(0600, config_path)
69
- end
70
-
71
- def retrieve_api_key
72
- if File.exists?(config_path)
73
- YAML.load(File.read(config_path))[:api_key]
74
- end
75
- end
76
-
77
- # TODO Temporary file should be named differently every time
78
- def create_dir_tar(directory)
79
- in_directory(directory) do
80
- tempfile = File.new('/tmp/out', 'wb')
81
- Archive::Tar::Minitar.pack('.', tempfile)
82
- File.new('/tmp/out', 'r')
83
- end
84
- end
85
-
86
- def in_directory(directory)
87
- cwd = FileUtils.pwd
88
- FileUtils.cd(directory)
89
- yield
90
- ensure
91
- FileUtils.cd(cwd)
92
- end
93
-
94
- def create_tar(directory, paths)
95
- tempfile = File.new('/tmp/out', 'wb')
96
-
97
- in_directory(directory) do
98
- begin
99
- tar = Archive::Tar::Minitar::Output.new("/tmp/out")
100
- paths.each do |entry|
101
- Archive::Tar::Minitar.pack_file(entry, tar)
102
- end
103
- ensure
104
- tar.close
105
- end
106
- end
107
- File.new('/tmp/out', 'r')
108
- end
109
-
110
- # Relative paths to all the files in the given directory (recursive)
111
- # (except for anything below a directory starting with ".")
112
- def all_paths(directory)
113
- result = []
114
- Find.find(directory) do |path|
115
- if FileTest.directory?(path)
116
- if File.basename(path)[0] == ?.
117
- Find.prune
118
- end
119
- else
120
- result << Pathname.new(path).relative_path_from(Pathname.new(directory)).to_s
121
- end
122
- end
123
- result
124
- end
125
-
126
- # Relative path of database file (if it exists)
127
- def database_path(directory)
128
- path = "data.sqlite"
129
- path if File.exists?(File.join(directory, path))
48
+ def ask_and_save_api_key(env_config, config)
49
+ env_config[:api_key] = ask("What is your key? (Go to #{env_config[:base_url]}/settings)")
50
+ MorphCLI.save_config(config)
51
+ config
130
52
  end
131
53
  }
132
-
133
54
  end
134
55
 
135
56
  # If morph is run without any parameters it's the same as "morph execute"
data/lib/morph-cli.rb CHANGED
@@ -1,5 +1,93 @@
1
1
  require "morph-cli/version"
2
2
 
3
3
  module MorphCLI
4
- # Your code goes here...
4
+ def self.execute(directory, development, env_config)
5
+ puts "Uploading and running..."
6
+ file = MorphCLI.create_tar(directory, MorphCLI.all_paths(directory))
7
+ result = RestClient.post("#{env_config[:base_url]}/run", :api_key => env_config[:api_key], :code => file)
8
+ # Interpret each line separately as json
9
+ result.split("\n").each do |line|
10
+ a = JSON.parse(line)
11
+ if a["stream"] == "stdout"
12
+ s = $stdout
13
+ elsif a["stream"] == "stderr"
14
+ s = $stderr
15
+ else
16
+ raise "Unknown stream"
17
+ end
18
+ s.puts a["text"]
19
+ end
20
+ end
21
+
22
+ def self.config_path
23
+ File.join(Dir.home, ".morph")
24
+ end
25
+
26
+ def self.save_config(config)
27
+ File.open(config_path, "w") {|f| f.write config.to_yaml}
28
+ File.chmod(0600, config_path)
29
+ end
30
+
31
+ DEFAULT_CONFIG = {
32
+ development: {
33
+ base_url: "http://127.0.0.1:3000"
34
+ },
35
+ production: {
36
+ base_url: "https://morph.io"
37
+ }
38
+ }
39
+
40
+ def self.load_config
41
+ if File.exists?(config_path)
42
+ YAML.load(File.read(config_path))
43
+ else
44
+ DEFAULT_CONFIG
45
+ end
46
+ end
47
+
48
+ def self.in_directory(directory)
49
+ cwd = FileUtils.pwd
50
+ FileUtils.cd(directory)
51
+ yield
52
+ ensure
53
+ FileUtils.cd(cwd)
54
+ end
55
+
56
+ def self.create_tar(directory, paths)
57
+ tempfile = File.new('/tmp/out', 'wb')
58
+
59
+ in_directory(directory) do
60
+ begin
61
+ tar = Archive::Tar::Minitar::Output.new("/tmp/out")
62
+ paths.each do |entry|
63
+ Archive::Tar::Minitar.pack_file(entry, tar)
64
+ end
65
+ ensure
66
+ tar.close
67
+ end
68
+ end
69
+ File.new('/tmp/out', 'r')
70
+ end
71
+
72
+ # Relative paths to all the files in the given directory (recursive)
73
+ # (except for anything below a directory starting with ".")
74
+ def self.all_paths(directory)
75
+ result = []
76
+ Find.find(directory) do |path|
77
+ if FileTest.directory?(path)
78
+ if File.basename(path)[0] == ?.
79
+ Find.prune
80
+ end
81
+ else
82
+ result << Pathname.new(path).relative_path_from(Pathname.new(directory)).to_s
83
+ end
84
+ end
85
+ result
86
+ end
87
+
88
+ # Relative path of database file (if it exists)
89
+ def self.database_path(directory)
90
+ path = "data.sqlite"
91
+ path if File.exists?(File.join(directory, path))
92
+ end
5
93
  end
@@ -1,3 +1,3 @@
1
1
  module MorphCLI
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morph-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Landauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-29 00:00:00.000000000 Z
11
+ date: 2014-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -90,7 +90,6 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - .gitignore
92
92
  - Gemfile
93
- - Gemfile.lock
94
93
  - LICENSE.txt
95
94
  - README.md
96
95
  - Rakefile