Initial import
This commit is contained in:
commit
dcb83b5aba
2 changed files with 53 additions and 0 deletions
19
shard.yml
Normal file
19
shard.yml
Normal file
|
@ -0,0 +1,19 @@
|
|||
name: which-cr
|
||||
version: 0.1.0
|
||||
|
||||
# authors:
|
||||
# - name <email@example.com>
|
||||
|
||||
# description: |
|
||||
# Short description of which-cr
|
||||
|
||||
# dependencies:
|
||||
# pg:
|
||||
# github: will/crystal-pg
|
||||
# version: "~> 0.5"
|
||||
|
||||
# development_dependencies:
|
||||
# webmock:
|
||||
# github: manastech/webmock.cr
|
||||
|
||||
# license: MIT
|
34
src/which.cr
Normal file
34
src/which.cr
Normal file
|
@ -0,0 +1,34 @@
|
|||
class Which
|
||||
def initialize(@all_matches : Bool = false)
|
||||
end
|
||||
|
||||
def find(programs : Array(String)) : Hash(String, Array(String))
|
||||
results = Hash(String, Array(String)).new
|
||||
|
||||
programs.each do |program|
|
||||
results[program] = find_program(program)
|
||||
end
|
||||
|
||||
results
|
||||
end
|
||||
|
||||
private def find_program(program : String) : Array(String)
|
||||
matches = [] of String
|
||||
|
||||
if program.includes?("/")
|
||||
matches << program if File.executable?(program)
|
||||
else
|
||||
ENV["PATH"].split(File::PATH_SEPARATOR).each do |path_element|
|
||||
path = path_element.not_empty? ? path_element : "."
|
||||
candidate = File.join(path, program)
|
||||
if File.executable?(candidate)
|
||||
matches << candidate
|
||||
break unless @all_matches
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
matches
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in a new issue