Sha256: 53cb8125a2f78ecf95139325ebe80554b269079eaf2593073d1669705218db5b

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

class Servel::ConfigParser
  attr_reader :config

  def initialize
    @tty_config = TTY::Config.new
    @tty_config.filename = "servel"
    @tty_config.append_path(Dir.pwd)
    @tty_config.append_path((Pathname.new(Dir.home) + ".servel").to_s)
    @tty_config.append_path(Dir.home)
    @tty_config.env_prefix = "servel"
    @tty_config.autoload_env
    @tty_config.read if @tty_config.exist?

    @tty_config.append(*parse_argv, to: :listings)
    parse_binds

    @config = @tty_config.to_h.transform_keys(&:to_sym)
  end

  def parse_argv
    ARGV.map do |arg|
      root, url_root = arg.split(":" , 2)
      { root => url_root }
    end
  end

  def parse_binds
    return parse_ssl_bind if @tty_config.fetch(:cert) && @tty_config.fetch(:key)

    host = @tty_config.fetch(:host)
    port = @tty_config.fetch(:port)
    @tty_config.set(:Host, value: host) if host
    @tty_config.set(:Port, value: port) if port
  end

  def parse_ssl_bind
    host = @tty_config.fetch(:host, default: ::Puma::ConfigDefault::DefaultTCPHost)
    port = @tty_config.fetch(:port, default: ::Puma::ConfigDefault::DefaultTCPPort)
    key = Pathname.new(@tty_config.fetch(:key)).expand_path
    cert = Pathname.new(@tty_config.fetch(:cert)).expand_path

    query = URI.encode_www_form(key: key, cert: cert)
    @tty_config.append("ssl://#{host}:#{port}?#{query}", to: :binds)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
servel-0.33.0 lib/servel/config_parser.rb