diff options
author | Michael Kozono <mkozono@gmail.com> | 2017-11-08 12:53:10 -0800 |
---|---|---|
committer | Michael Kozono <mkozono@gmail.com> | 2017-12-01 15:26:40 -0800 |
commit | ab814e4dd3cf06559a95ca5dd19722431314f6fa (patch) | |
tree | 46b685bec5ef64e20941059eea459757ca43830d /lib/gitlab | |
parent | d6435b68c4fc4e0325ec6a3deb807d0e3dd4dec4 (diff) | |
download | gitlab-ce-ab814e4dd3cf06559a95ca5dd19722431314f6fa.tar.gz |
Backport `which` from EE
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/utils.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb index abb3d3a02c3..b3baaf036d8 100644 --- a/lib/gitlab/utils.rb +++ b/lib/gitlab/utils.rb @@ -46,5 +46,22 @@ module Gitlab def random_string Random.rand(Float::MAX.to_i).to_s(36) end + + # See: http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby + # Cross-platform way of finding an executable in the $PATH. + # + # which('ruby') #=> /usr/bin/ruby + def which(cmd, env = ENV) + exts = env['PATHEXT'] ? env['PATHEXT'].split(';') : [''] + + env['PATH'].split(File::PATH_SEPARATOR).each do |path| + exts.each do |ext| + exe = File.join(path, "#{cmd}#{ext}") + return exe if File.executable?(exe) && !File.directory?(exe) + end + end + + nil + end end end |