Sha256: 8d0f73157145402e6735725eecfb9bd41673cdaa1cdb02112a8df84a49699362

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'dotenv'
require 'sequel'

command :server do |c|
  c.syntax = 'helios server'
  c.summary = 'Start running Helios locally'
  c.option '-w', "--[no-]warn", "Warn about possible database issues"

  c.action do |args, options|
    validate_database_settings! unless options.warn == false

    begin
      exec 'foreman start'
    rescue => exception
      say_error exception.message and abort
    end
  end
end

alias_command :s, :server
alias_command :start, :server
alias_command :launch, :server

private

def validate_database_settings!
  Dotenv.load

  say_error "DATABASE_URL environment variable not set in .env or in Rails config/database.yml" and abort if ENV['DATABASE_URL'].nil?

  begin
    db = Sequel.connect(ENV['DATABASE_URL'])
    db.test_connection
  rescue Sequel::DatabaseConnectionError => error
    say_warning %{Error connecting to database: "#{error.message.strip}"}
    case error.message
    when /database "(.+)" does not exist/
      if agree "Would you like to create this database now? (y/n)"
        uri = URI(db.uri)
        host, database = uri.host, uri.path.delete("/")

        log 'createdb', database
        system "createdb -h #{host} #{database}"
      end
    else
      abort unless agree "Continue starting Helios? (y/n)"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
helios-0.2.1 ./lib/helios/commands/server.rb