Class: FaaStRuby::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/faastruby/cli.rb

Class Method Summary collapse

Class Method Details

.check_regionObject



57
58
59
60
# File 'lib/faastruby/cli.rb', line 57

def self.check_region
  ENV['FAASTRUBY_REGION'] ||= DEFAULT_REGION
  error(["No such region: #{ENV['FAASTRUBY_REGION']}".red, "Valid regions are:  #{FaaStRuby::REGIONS.join(' | ')}"], color: nil) unless FaaStRuby::REGIONS.include?(ENV['FAASTRUBY_REGION'])
end

.check_ruby_versionObject

def self.check_version

latest = RestClient.get('https://faastruby.io/gem/minimum.txt').body rescue '0.0.1'
if Gem::Version.new(FaaStRuby::VERSION) < Gem::Version.new(latest)
  FaaStRuby.error([
    "You are using an old version of the gem. Please run 'gem update faastruby'.".red,
    "Installed version: #{FaaStRuby::VERSION}",
    "Latest version: #{latest}"
  ], color: nil)
end

end



47
48
49
50
# File 'lib/faastruby/cli.rb', line 47

def self.check_ruby_version
  require 'faastruby/supported_runtimes'
  error("Unsupported Ruby version: #{RUBY_VERSION}\nSupported Ruby versions are: #{SUPPORTED_RUBY.join(", ")}") unless version_match?(SUPPORTED_RUBY, RUBY_VERSION)
end

.error(message, color: :red) ⇒ Object



11
12
13
14
15
# File 'lib/faastruby/cli.rb', line 11

def self.error(message, color: :red)
  message.compact.each {|m| STDERR.puts m.colorize(color)} if message.is_a?(Array)
  STDERR.puts message.colorize(color) if message.is_a?(String)
  exit 1
end

.run(command, args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/faastruby/cli.rb', line 17

def self.run(command, args)
  if command.nil?
    require 'faastruby/cli/commands/help'
    FaaStRuby::Command::Help.new(args).run
    return
  end
  check_ruby_version
  start_server(args) if command == 'local'
  # check_version
  check_region
  require 'faastruby/cli/commands'
  # require 'faastruby/cli/package'
  # require 'faastruby/cli/template'
  error("Unknown command: #{command}") unless FaaStRuby::Command::COMMANDS.has_key?(command)

  const = FaaStRuby::Command::COMMANDS[command].call
  const.new(args).run
end

.start_server(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/faastruby/cli.rb', line 62

def self.start_server(args)
  parsed = []
  parsed << 'FAASTRUBY_PROJECT_SYNC_ENABLED=true' if args.delete('--sync')
  parsed << 'DEBUG=true' if args.delete('--debug')
  server_port = '3000'

  args.each_with_index do |arg, i|
    if ['--env', '-e'].include?(arg)
      args.delete_at(i)
      parsed << "FAASTRUBY_PROJECT_DEPLOY_ENVIRONMENT=#{args.delete_at(i)}"
    else ['-p', '--port'].include?(arg)
      args.delete_at(i)
      server_port = args.delete_at(i)
    end
  end
  parsed << "SERVER_PORT=#{server_port}"
  server_dir = "#{Gem::Specification.find_by_name("faastruby").gem_dir}/lib/faastruby/server"
  config_ru = "#{server_dir}/config.ru"
  puma_config = "#{server_dir}/puma.rb"
  exec "#{parsed.join(' ')} puma -C #{puma_config} -p #{server_port} #{args.join(' ')} #{config_ru}"
end

.version_match?(supported, current) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/faastruby/cli.rb', line 52

def self.version_match?(supported, current)
  supported.each {|supported_version| return true if Gem::Dependency.new('', supported_version).match?('', current)}
  return false
end