Sha256: b8ad0158c59c20e4ded38e02d3cf76220f0af47ad044aedc3c80471730653ff9

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Libexec
  module Exec
    def run(cmd)
      # https://docs.ruby-lang.org/en/master/IO.html#method-c-popen
      IO.popen(cmd) do |pipe|
        print pipe.gets until pipe.eof?
      end
      nil
    end

    def each_line(cmd, &block)
      # https://docs.ruby-lang.org/en/master/IO.html#method-i-each_line
      IO.popen(cmd) do |pipe|
        pipe.each_line(&block)
      end
    end

    def by_ls_1(dir)
      arr = []
      each_line("ls -1 #{dir}") do |line|
        arr.push line.chomp
      end
      arr
    end

    def each_ls_1(dir, &block)
      each_line("ls -1 #{dir}", &block)
    end

    def code(cmd, code = nil, **opts)
      catch_error = code.instance_of?(Integer) || opts[:catch_error] || false
      code ||= opts[:code] || 1

      # https://docs.ruby-lang.org/en/master/Process.html#method-c-last_status
      Process.spawn(cmd)
      Process.wait

      result = $?.exitstatus.zero?

      if catch_error && !result
        exit code
      else
        $?.exitstatus
      end
    rescue Errno::ENOENT => _e
      127
    end

    def output(cmd)
      # https://docs.ruby-lang.org/en/master/Kernel.html#method-i-60
      output = `#{cmd}`
      output.chomp
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libexec-0.1.4 lib/libexec/exec.rb