Skip to content

Commit 6a4b0ba

Browse files
committed
Problem: getting parseable list of packages
Currently this would require some scripting. Solution: add `-f json` option to `pgpm search` While at it, make sure Podman doesn't print on stdout to avoid messing up JSON output. Also, improve search printing a bit by printing search result in progress.
1 parent 2856511 commit 6a4b0ba

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

exe/pgpm

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env ruby
22
# frozen_string_literal: true
33

4+
require "English"
45
require "bundler/setup"
56
require "pgpm"
67
require "dry/cli"
@@ -117,21 +118,44 @@ module Pgpm
117118
include SharedOptions
118119

119120
argument :query, type: :string, default: ".*", desc: "Search query"
121+
option :format, values: %w[text json], default: "text", desc: "Output format", aliases: ["-f"]
120122

121-
def call(query:, args: nil, pkgdir: nil)
123+
def call(query:, args: nil, pkgdir: nil, format: nil)
122124
_ = args
123125

124126
Pgpm.load_packages(pkgdir)
125127

126128
query_regexp = Regexp.new(query, "i")
129+
if format == "json"
130+
puts "["
131+
end
132+
127133
Parallel.filter_map(Pgpm::Package, in_threads: Etc.nprocessors) do |p|
128134
next if p.contrib?
129135

130-
matches = p.all_searchable_texts.any? do |t|
136+
p.all_searchable_texts.any? do |t|
131137
t =~ query_regexp
132138
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 "]"
135159
end
136160
end
137161

lib/pgpm/package/metadata.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def license
4242
self[:latest]&.license
4343
end
4444

45+
def release_date
46+
self[:latest]&.release_date
47+
end
48+
4549
def all_searchable_texts
4650
self[:latest]&.all_searchable_texts || []
4751
end

lib/pgpm/podman.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Pgpm
88
module Podman
99
def self.run(command, unhandled_reboot_mitigation: true, print_stdout: true)
1010
result = TTY::Command.new(printer: :null).run("podman #{command}", pty: true) do |out, err|
11-
print out if print_stdout
12-
print err
11+
warn out if print_stdout
12+
warn err
1313
end
1414

1515
result.out

0 commit comments

Comments
 (0)