# encoding: utf-8 require 'fedux_org_stdlib/core_ext/file/which' module FeduxOrgStdlib class CommandFinder private attr_reader :search_paths, :alternatives public # Finds path to command # # It also considers other executables for one command # # @param [String, Array] alternatives # The executables to be used # # @param [String, Array] search_paths # The search path as multiple paths as array or string where single # search paths are concatenated via ':' def initialize(alternatives:, search_paths:) @alternatives = Array(alternatives) @search_paths = Array(search_paths) end # Find path to command def path alternatives.each do |e| next unless path = File.which(e, search_paths.join(':')) return path end nil end # Return known commands def known_commands @alternatives end end end