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.0.2"

Class Method Summary collapse

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 “.”)



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/morph-cli.rb', line 74

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_pathObject



22
23
24
# File 'lib/morph-cli.rb', line 22

def self.config_path
  File.join(Dir.home, ".morph")
end

.create_tar(directory, paths) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/morph-cli.rb', line 56

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)



89
90
91
92
# File 'lib/morph-cli.rb', line 89

def self.database_path(directory)
  path = "data.sqlite"
  path if File.exists?(File.join(directory, path))
end

.execute(directory, development, env_config) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/morph-cli.rb', line 4

def self.execute(directory, development, env_config)
  puts "Uploading and running..."
  file = MorphCLI.create_tar(directory, MorphCLI.all_paths(directory))
  result = RestClient.post("#{env_config[:base_url]}/run", :api_key => env_config[:api_key], :code => file)
  # Interpret each line separately as json
  result.split("\n").each do |line|
    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

.in_directory(directory) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/morph-cli.rb', line 48

def self.in_directory(directory)
  cwd = FileUtils.pwd
  FileUtils.cd(directory)
  yield
ensure
  FileUtils.cd(cwd)
end

.load_configObject



40
41
42
43
44
45
46
# File 'lib/morph-cli.rb', line 40

def self.load_config
  if File.exists?(config_path)
    YAML.load(File.read(config_path))
  else
    DEFAULT_CONFIG
  end
end

.save_config(config) ⇒ Object



26
27
28
29
# File 'lib/morph-cli.rb', line 26

def self.save_config(config)
  File.open(config_path, "w") {|f| f.write config.to_yaml}
  File.chmod(0600, config_path)
end