Sha256: 0456c0cab4a29a78c1e8873ca2930da6ad8b8b719246dd4fb008fd7f98e25223

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 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, 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: false)
      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: "",
          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.12 lib/spoom.rb