|
1 | 1 | #!/usr/bin/env ruby
|
2 | 2 | # frozen_string_literal: true
|
3 | 3 |
|
| 4 | +require "English" |
4 | 5 | require "bundler/setup"
|
5 | 6 | require "pgpm"
|
6 | 7 | require "dry/cli"
|
@@ -117,21 +118,44 @@ module Pgpm
|
117 | 118 | include SharedOptions
|
118 | 119 |
|
119 | 120 | argument :query, type: :string, default: ".*", desc: "Search query"
|
| 121 | + option :format, values: %w[text json], default: "text", desc: "Output format", aliases: ["-f"] |
120 | 122 |
|
121 |
| - def call(query:, args: nil, pkgdir: nil) |
| 123 | + def call(query:, args: nil, pkgdir: nil, format: nil) |
122 | 124 | _ = args
|
123 | 125 |
|
124 | 126 | Pgpm.load_packages(pkgdir)
|
125 | 127 |
|
126 | 128 | query_regexp = Regexp.new(query, "i")
|
| 129 | + if format == "json" |
| 130 | + puts "[" |
| 131 | + end |
| 132 | + |
127 | 133 | Parallel.filter_map(Pgpm::Package, in_threads: Etc.nprocessors) do |p|
|
128 | 134 | next if p.contrib?
|
129 | 135 |
|
130 |
| - matches = p.all_searchable_texts.any? do |t| |
| 136 | + p.all_searchable_texts.any? do |t| |
131 | 137 | t =~ query_regexp
|
132 | 138 | end
|
133 |
| - "#{p.package_name}: #{p.description}" if matches |
134 |
| - end.each { |l| puts l } |
| 139 | + output = case format |
| 140 | + when "json" then Oj.dump({ |
| 141 | + name: p.package_name, |
| 142 | + summary: p.summary, |
| 143 | + description: p.description, |
| 144 | + license: p.license, |
| 145 | + versions: p.package_versions.map(&:to_s) |
| 146 | + }, mode: :strict) |
| 147 | + else |
| 148 | + "#{p.package_name}: #{p.description}" |
| 149 | + end |
| 150 | + # printer.send(output, move: true) if matches |
| 151 | + puts output |
| 152 | + rescue StandardError |
| 153 | + warn "Error fetching #{p.package_name}: #{$ERROR_INFO.message}" |
| 154 | + end |
| 155 | + |
| 156 | + return unless format == "json" |
| 157 | + |
| 158 | + puts "]" |
135 | 159 | end
|
136 | 160 | end
|
137 | 161 |
|
|
0 commit comments