Sha256: 0bbddbc1a8441c7b750550c53ce9d42f0817cd14135b93eee86113fe9eee09ee

Contents?: true

Size: 1.96 KB

Versions: 87

Compression:

Stored size: 1.96 KB

Contents

require 'cli/kit'
require 'English'
require 'fileutils'

module CLI
  module Kit
    class Executor
      def initialize(log_file:)
        FileUtils.mkpath(File.dirname(log_file))
        @log_file = log_file
      end

      def call(command, command_name, args)
        with_traps do
          with_logging do |id|
            command.call(args, command_name)
          rescue => e
            begin
              $stderr.puts "This command ran with ID: #{id}"
              $stderr.puts "Please include this information in any issues/report along with relevant logs"
            rescue SystemCallError
              # Outputting to stderr is best-effort.  Avoid raising another error when outputting debug info so that
              # we can detect and log the original error, which may even be the source of this error.
              nil
            end
            raise e
          end
        end
      end

      private

      def with_logging(&block)
        return yield unless @log_file
        CLI::UI.log_output_to(@log_file) do
          CLI::UI::StdoutRouter.with_id(on_streams: [CLI::UI::StdoutRouter.duplicate_output_to]) do |id|
            block.call(id)
          end
        end
      end

      def with_traps
        twrap('QUIT', method(:quit_handler)) do
          twrap('INFO', method(:info_handler)) do
            yield
          end
        end
      end

      def twrap(signal, handler)
        return yield unless Signal.list.key?(signal)

        begin
          prev_handler = trap(signal, handler)
          yield
        ensure
          trap(signal, prev_handler)
        end
      end

      def quit_handler(_sig)
        z = caller
        CLI::UI.raw do
          $stderr.puts('SIGQUIT: quit')
          $stderr.puts(z)
        end
        exit(CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG)
      end

      def info_handler(_sig)
        z = caller
        CLI::UI.raw do
          $stderr.puts('SIGINFO:')
          $stderr.puts(z)
        end
      end
    end
  end
end

Version data entries

87 entries across 87 versions & 1 rubygems

Version Path
shopify-cli-2.36.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.35.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.34.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.33.1 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.33.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.32.1 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.32.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.31.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.30.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.29.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.28.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.27.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.26.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.25.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.24.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.23.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.22.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.21.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.20.1 vendor/deps/cli-kit/lib/cli/kit/executor.rb
shopify-cli-2.20.0 vendor/deps/cli-kit/lib/cli/kit/executor.rb