diff --git a/lib/puppet/face/module/search.rb b/lib/puppet/face/module/search.rb index 0c488082f..169f64090 100644 --- a/lib/puppet/face/module/search.rb +++ b/lib/puppet/face/module/search.rb @@ -1,90 +1,88 @@ require 'puppet/util/terminal' Puppet::Face.define(:module, '1.0.0') do action(:search) do summary "Search a repository for a module." description <<-EOT Searches a repository for modules whose names, descriptions, or keywords match the provided search term. EOT returns "Array of module metadata hashes" examples <<-EOT Search the default repository for a module: $ puppet module search puppetlabs NAME DESCRIPTION AUTHOR KEYWORDS bacula This is a generic Apache module @puppetlabs backups EOT arguments "" when_invoked do |term, options| - server = Puppet.settings[:module_repository].sub(/^(?!https?:\/\/)/, 'http://') - Puppet.notice "Searching #{server} ..." Puppet::Module::Tool::Applications::Searcher.run(term, options) end when_rendering :console do |results, term, options| return "No results found for '#{term}'." if results.empty? padding = ' ' headers = { 'full_name' => 'NAME', 'desc' => 'DESCRIPTION', 'author' => 'AUTHOR', 'tag_list' => 'KEYWORDS', } min_widths = Hash[ *headers.map { |k,v| [k, v.length] }.flatten ] min_widths['full_name'] = min_widths['author'] = 12 min_width = min_widths.inject(0) { |sum,pair| sum += pair.last } + (padding.length * (headers.length - 1)) terminal_width = [Puppet::Util::Terminal.width, min_width].max columns = results.inject(min_widths) do |hash, result| { 'full_name' => [ hash['full_name'], result['full_name'].length ].max, 'desc' => [ hash['desc'], result['desc'].length ].max, 'author' => [ hash['author'], "@#{result['author']}".length ].max, 'tag_list' => [ hash['tag_list'], result['tag_list'].join(' ').length ].max, } end flex_width = terminal_width - columns['full_name'] - columns['author'] - (padding.length * (headers.length - 1)) tag_lists = results.map { |r| r['tag_list'] } while (columns['tag_list'] > flex_width / 3) longest_tag_list = tag_lists.sort_by { |tl| tl.join(' ').length }.last break if [ [], [term] ].include? longest_tag_list longest_tag_list.delete(longest_tag_list.sort_by { |t| t == term ? -1 : t.length }.last) columns['tag_list'] = tag_lists.map { |tl| tl.join(' ').length }.max end columns['tag_list'] = [ flex_width / 3, tag_lists.map { |tl| tl.join(' ').length }.max, ].max columns['desc'] = flex_width - columns['tag_list'] format = %w{full_name desc author tag_list}.map do |k| "%-#{ [ columns[k], min_widths[k] ].max }s" end.join(padding) + "\n" highlight = proc do |s| s = s.gsub(term, colorize(:green, term)) s = s.gsub(term.gsub('/', '-'), colorize(:green, term.gsub('/', '-'))) if term =~ /\// s end format % [ headers['full_name'], headers['desc'], headers['author'], headers['tag_list'] ] + results.map do |match| name, desc, author, keywords = %w{full_name desc author tag_list}.map { |k| match[k] } desc = desc[0...(columns['desc'] - 3)] + '...' if desc.length > columns['desc'] highlight[format % [ name.sub('/', '-'), desc, "@#{author}", [keywords].flatten.join(' ') ]] end.join end end end diff --git a/lib/puppet/forge.rb b/lib/puppet/forge.rb index 8eeb991ae..722f03b95 100644 --- a/lib/puppet/forge.rb +++ b/lib/puppet/forge.rb @@ -1,96 +1,98 @@ require 'net/http' require 'open-uri' require 'pathname' require 'uri' require 'puppet/forge/cache' require 'puppet/forge/repository' module Puppet::Forge # Return a list of module metadata hashes that match the search query. # This return value is used by the module_tool face install search, # and displayed to on the console. # # Example return value: # # [ # { # "author" => "puppetlabs", # "name" => "bacula", # "tag_list" => ["backup", "bacula"], # "releases" => [{"version"=>"0.0.1"}, {"version"=>"0.0.2"}], # "full_name" => "puppetlabs/bacula", # "version" => "0.0.2", # "project_url" => "http://github.com/puppetlabs/puppetlabs-bacula", # "desc" => "bacula" # } # ] # def self.search(term) + server = Puppet.settings[:module_repository].sub(/^(?!https?:\/\/)/, 'http://') + Puppet.notice "Searching #{server} ..." request = Net::HTTP::Get.new("/modules.json?q=#{URI.escape(term)}") response = repository.make_http_request(request) case response.code when "200" matches = PSON.parse(response.body) else raise RuntimeError, "Could not execute search (HTTP #{response.code})" matches = [] end matches end def self.remote_dependency_info(author, mod_name, version) version_string = version ? "&version=#{version}" : '' request = Net::HTTP::Get.new("/api/v1/releases.json?module=#{author}/#{mod_name}" + version_string) response = repository.make_http_request(request) json = PSON.parse(response.body) rescue {} case response.code when "200" return json else error = json['error'] || '' if error =~ /^Module #{author}\/#{mod_name} has no release/ return [] else raise RuntimeError, "Could not find release information for this module (#{author}/#{mod_name}) (HTTP #{response.code})" end end end def self.get_release_packages_from_repository(install_list) install_list.map do |release| modname, version, file = release cache_path = nil if file begin cache_path = repository.retrieve(file) rescue OpenURI::HTTPError => e raise RuntimeError, "Could not download module: #{e.message}" end else raise RuntimeError, "Malformed response from module repository." end cache_path end end # Locate a module release package on the local filesystem and move it # into the `Puppet.settings[:module_working_dir]`. Do not unpack it, just # return the location of the package on disk. def self.get_release_package_from_filesystem(filename) if File.exist?(File.expand_path(filename)) repository = Repository.new('file:///') uri = URI.parse("file://#{URI.escape(File.expand_path(filename))}") cache_path = repository.retrieve(uri) else raise ArgumentError, "File does not exists: #{filename}" end cache_path end def self.repository @repository ||= Puppet::Forge::Repository.new end end