Sha256: 215391bcd6c40a6ef59410bd1711ffb0fc999cacd6014bcf52dfc9d3a47c3fe4

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module Luffa
  def self.unix_command(cmd, opts={})
    default_opts = {:pass_msg => nil,
                    :fail_msg => nil,
                    :exit_on_nonzero_status => true,
                    :env_vars => {},
                    :log_cmd => true,
                    :obscure_fields => [],
                    :split_cmd => false}
    merged_opts = default_opts.merge(opts)

    obscure_fields = merged_opts[:obscure_fields]

    if not obscure_fields.empty? and merged_opts[:log_cmd]
      obscured = cmd.split(' ').map do |token|
        if obscure_fields.include? token
          "#{token[0,1]}***#{token[token.length-1,1]}"
        else
          token
        end
      end
      Luffa.log_unix_cmd obscured.join(' ')
    elsif merged_opts[:log_cmd]
      Luffa.log_unix_cmd cmd
    end

    exit_on_err = merged_opts[:exit_on_nonzero_status]
    unless exit_on_err
      system 'set +e'
    end

    env_vars = merged_opts[:env_vars]

    if merged_opts[:split_cmd]
      ['|', '>', '&'].each do |character|
        if cmd.include?(character)
          raise "Cannot split command '#{cmd}' because it contains character '#{character}'"
        end
      end

      tokens = cmd.split(' ')
      res = system(env_vars, tokens.shift, *tokens)
    else
      res = system(env_vars, cmd)
    end

    exit_code = $?.exitstatus

    if res
      Luffa.log_pass merged_opts[:pass_msg]
    else
      Luffa.log_fail merged_opts[:fail_msg]
      exit exit_code if exit_on_err
    end
    system 'set -e'
    exit_code
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
luffa-2.1.0 lib/luffa/unix_command.rb
luffa-2.0.0 lib/luffa/unix_command.rb
luffa-1.1.0 lib/luffa/unix_command.rb