From dcb83b5aba4462a685e2a1fb14aa7827375d5dcc Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 22 Nov 2024 09:46:08 +0100 Subject: [PATCH] Initial import --- shard.yml | 19 +++++++++++++++++++ src/which.cr | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 shard.yml create mode 100644 src/which.cr diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..c6c1b6f --- /dev/null +++ b/shard.yml @@ -0,0 +1,19 @@ +name: which-cr +version: 0.1.0 + +# authors: +# - name + +# description: | +# Short description of which-cr + +# dependencies: +# pg: +# github: will/crystal-pg +# version: "~> 0.5" + +# development_dependencies: +# webmock: +# github: manastech/webmock.cr + +# license: MIT diff --git a/src/which.cr b/src/which.cr new file mode 100644 index 0000000..06fde2a --- /dev/null +++ b/src/which.cr @@ -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 +