class RackThor

Attributes

router[R]

Public Class Methods

new(cli) click to toggle source
# File lib/rack-thor.rb, line 7
def initialize(cli)
  @cli = cli
  @router = Hanami::Router.new
  cli.all_commands.each do |name, c|
    @router.post "/#{name}", to: ->(env) do
      req = Rack::Request.new(env)
      begin
        $stdout = StringIO.new
        command = @cli.all_commands[name]
        @cli.new([], req.params['options'] || {}, {
          current_command: command,
          command_options: command.options
        }).invoke(name, req.params['args'] || [])
        $stdout.close
        [200, {}, [$stdout.string]]
      ensure
        $stdout = STDOUT
      end
    end
  end
end