Sha256: 73dea24df401f6553aa989c5cf47d692fc5547de9fb21167853dfc53bdd0094a

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# typed: strict
# frozen_string_literal: true

require "sorbet-runtime"
require "pathname"

module Spoom
  extend T::Sig

  SPOOM_PATH = T.let((Pathname.new(__FILE__) / ".." / "..").to_s, String)

  class Error < StandardError; end

  class ExecResult < T::Struct
    extend T::Sig

    const :out, String
    const :err, T.nilable(String)
    const :status, T::Boolean
    const :exit_code, Integer

    sig { returns(String) }
    def to_s
      <<~STR
        ########## STDOUT ##########
        #{out.empty? ? "<empty>" : out}
        ########## STDERR ##########
        #{err&.empty? ? "<empty>" : err}
        ########## STATUS: #{status} ##########
      STR
    end
  end

  class << self
    extend T::Sig

    sig do
      params(
        cmd: String,
        arg: String,
        path: String,
        capture_err: T::Boolean,
      ).returns(ExecResult)
    end
    def exec(cmd, *arg, path: ".", capture_err: true)
      if capture_err
        stdout, stderr, status = T.unsafe(Open3).capture3([cmd, *arg].join(" "), chdir: path)
        ExecResult.new(
          out: stdout,
          err: stderr,
          status: status.success?,
          exit_code: status.exitstatus,
        )
      else
        stdout, status = T.unsafe(Open3).capture2([cmd, *arg].join(" "), chdir: path)
        ExecResult.new(
          out: stdout,
          err: nil,
          status: status.success?,
          exit_code: status.exitstatus,
        )
      end
    end
  end
end

require "spoom/context"
require "spoom/colors"
require "spoom/sorbet"
require "spoom/cli"
require "spoom/version"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spoom-1.1.16 lib/spoom.rb