Sha256: 8f017ecbe1a7aa90541eacf777cc44d50fe2e0ba84e0032abc1e5c9f56f0ad1d

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/hash/indifferent_access'
require 'shellwords'

module EacRubyUtils
  module Envs
    class Command
      module ExtraOptions
        def chdir(dir)
          duplicate_by_extra_options(chdir: dir)
        end

        def envvar(name, value)
          duplicate_by_extra_options(envvars: envvars.merge(name => value))
        end

        def pipe(other_command)
          duplicate_by_extra_options(pipe: other_command)
        end

        private

        attr_reader :extra_options

        def envvars
          extra_options[:envvars] ||= {}.with_indifferent_access
        end

        def append_envvars(command)
          e = envvars.map { |k, v| "#{Shellwords.escape(k)}=#{Shellwords.escape(v)}" }.join(' ')
          e.present? ? "#{e} #{command}" : command
        end

        def append_pipe(command)
          extra_options[:pipe].present? ? "#{command} | #{extra_options[:pipe].command}" : command
        end

        def append_chdir(command)
          extra_options[:chdir].present? ? "(cd '#{extra_options[:chdir]}' ; #{command} )" : command
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
eac_ruby_utils-0.12.0 lib/eac_ruby_utils/envs/command/extra_options.rb
eac_ruby_utils-0.11.0 lib/eac_ruby_utils/envs/command/extra_options.rb
eac_ruby_utils-0.10.1 lib/eac_ruby_utils/envs/command/extra_options.rb
eac_ruby_utils-0.10.0 lib/eac_ruby_utils/envs/command/extra_options.rb
eac_ruby_utils-0.9.0 lib/eac_ruby_utils/envs/command/extra_options.rb
eac_ruby_utils-0.8.0 lib/eac_ruby_utils/envs/command/extra_options.rb
eac_ruby_utils-0.7.0 lib/eac_ruby_utils/envs/command/extra_options.rb