Sha256: a867f41e889a9391f2b7ab5b5c6c92ffea8748275850fee323f3126d9618b2d4

Contents?: true

Size: 1.57 KB

Versions: 19

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

#
# This code is based on https://github.com/fohte/rubocop-daemon.
#
# Copyright (c) 2018 Hayato Kawai
#
# The MIT License (MIT)
#
# https://github.com/fohte/rubocop-daemon/blob/master/LICENSE.txt
#
module RuboCop
  module Server
    # This class sends the request read from the socket to server.
    # @api private
    class SocketReader
      Request = Struct.new(:header, :body)
      Header = Struct.new(:token, :cwd, :command, :args)

      def initialize(socket)
        @socket = socket
      end

      def read!
        request = parse_request(@socket.read)

        Helper.redirect(
          stdin: StringIO.new(request.body),
          stdout: @socket,
          stderr: @socket
        ) do
          create_command_instance(request).run
        end
      end

      private

      def parse_request(content)
        raw_header, *body = content.lines

        Request.new(parse_header(raw_header), body.join)
      end

      def parse_header(header)
        token, cwd, command, *args = header.shellsplit
        Header.new(token, cwd, command, args)
      end

      def create_command_instance(request)
        klass = find_command_class(request.header.command)

        klass.new(request.header.args, token: request.header.token, cwd: request.header.cwd)
      end

      def find_command_class(command)
        case command
        when 'stop' then ServerCommand::Stop
        when 'exec' then ServerCommand::Exec
        else
          raise UnknownServerCommandError, "#{command.inspect} is not a valid command"
        end
      end
    end
  end
end

Version data entries

19 entries across 15 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/server/socket_reader.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/server/socket_reader.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/server/socket_reader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/server/socket_reader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/server/socket_reader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/server/socket_reader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/server/socket_reader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/server/socket_reader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/server/socket_reader.rb
rubocop-1.36.0 lib/rubocop/server/socket_reader.rb
rubocop-1.35.1 lib/rubocop/server/socket_reader.rb
rubocop-1.35.0 lib/rubocop/server/socket_reader.rb
rubocop-1.34.1 lib/rubocop/server/socket_reader.rb
rubocop-1.34.0 lib/rubocop/server/socket_reader.rb
rubocop-1.33.0 lib/rubocop/server/socket_reader.rb
rubocop-1.32.0 lib/rubocop/server/socket_reader.rb
rubocop-1.31.2 lib/rubocop/server/socket_reader.rb
rubocop-1.31.1 lib/rubocop/server/socket_reader.rb
rubocop-1.31.0 lib/rubocop/server/socket_reader.rb