require 'colorize' module FaaStRuby FUNCTION_NAME_REGEX = '[a-zA-Z\-_0-9\/\.]{1,}' WORKSPACE_NAME_REGEX = '[a-zA-Z0-im9_]{1}[a-zA-Z0-9\-]{1,}[a-zA-Z0-9_]{1}' FAASTRUBY_YAML = 'faastruby.yml' SPINNER_FORMAT = :spin_2 require 'faastruby/supported_runtimes' require 'faastruby/regions' class CLI 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 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 # 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 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 def self.version_match?(supported, current) supported.each {|supported_version| return true if Gem::Dependency.new('', supported_version).match?('', current)} return false end 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 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" puma_executable = which("puma") error("FATAL: Could not find 'puma' executable.") unless puma_executable exec "#{parsed.join(' ')} #{puma_executable} -C #{puma_config} -p #{server_port} #{args.join(' ')} #{config_ru}" end # Cross-platform way of finding an executable in the $PATH. # # which('ruby') #=> /usr/bin/ruby def self.which(cmd) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each { |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) } end return nil end end end