2016-04-18 12:47:27 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
require 'shellwords'
|
|
|
|
|
|
|
|
class CommandWrapper
|
2018-10-23 13:16:24 +00:00
|
|
|
UNIX_SHELLS = %w{sh bash zsh ksh}.freeze
|
2016-04-18 12:47:27 +00:00
|
|
|
|
|
|
|
def self.wrap(cmd, options)
|
|
|
|
unless options.is_a?(Hash)
|
2017-02-08 22:49:16 +00:00
|
|
|
raise 'All options for the command wrapper must be provided as a hash. '\
|
2016-04-18 12:47:27 +00:00
|
|
|
"You entered: #{options.inspect}. Please consult the documentation."
|
|
|
|
end
|
|
|
|
|
|
|
|
wrap = options[:wrap]
|
2017-11-21 07:49:41 +00:00
|
|
|
raise "Called command wrapper with wrap: #{wrap.inspect}. It must be called with a Proc." if !wrap.nil? && !wrap.is_a?(Proc)
|
|
|
|
return wrap.call(cmd) unless wrap.nil?
|
2016-04-18 12:47:27 +00:00
|
|
|
|
|
|
|
shell = options[:shell]
|
2017-11-21 07:49:41 +00:00
|
|
|
raise "Don't know how to wrap commands for shell: #{shell.inspect}." unless UNIX_SHELLS.include?(shell)
|
2016-04-18 12:47:27 +00:00
|
|
|
|
|
|
|
path = options[:path] || shell
|
|
|
|
args = options[:args] || '-c'
|
|
|
|
path.to_s + ' ' + args + ' ' + Shellwords.escape(cmd)
|
|
|
|
end
|
|
|
|
end
|