Sha256: ac3a730fcac95e320efe3a6a78b2b113df39a91cec3e78afcc2dde4cbddd7d75

Contents?: true

Size: 1.49 KB

Versions: 56

Compression:

Stored size: 1.49 KB

Contents

# encoding: utf-8
require 'fedux_org_stdlib/require_files'
require_library %w(open3)

require 'fedux_org_stdlib/environment'
require 'fedux_org_stdlib/command/command_result'

module FeduxOrgStdlib
  module Command
    # Run command
    module RunCommand
      include Environment

      # Execute command
      #
      # @param [String] cmd
      #   the command
      #
      # @param [Hash] options
      #   the options for command execution
      #
      # @option options [Hash] env ({])
      #   the environment variables for the command ('VAR' => 'CONTENT')
      #
      # @option options [String] stdin (nil)
      #   the string for stdin of the command
      #
      # @option options [TrueClass,FalseClass] binmode (false)
      #   should the stdin be read a binary or not
      #
      # @return [CommandResult]
      #   the result of the command execution
      #
      # @return [CommandResult]
      #   the result of the command execution
      def run_command(cmd, options = {})
        opts = {
          env: nil,
          stdin: nil,
          binmode: false,
          working_directory: Dir.getwd
        }.merge options

        env = opts[:env] || ENV.to_hash
        stdin = opts[:stdin]
        binmode = opts[:binmode]
        working_directory = opts[:working_directory]

        result = CommandResult.new
        result.stdout, result.stderr, result.status = Open3.capture3(env, cmd, stdin_data: stdin, chdir: working_directory, binmode: binmode)

        result
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.10.6 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.10.5 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.10.4 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.10.3 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.10.2 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.10.1 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.10.0 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.8 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.7 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.6 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.5 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.4 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.2 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.1 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.9.0 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.8.11 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.8.10 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.8.9 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.8.8 lib/fedux_org_stdlib/command/run_command.rb
fedux_org-stdlib-0.8.7 lib/fedux_org_stdlib/command/run_command.rb