Skip to content

Commit

Permalink
Problem: getting parseable list of packages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yrashk committed Jan 18, 2025
1 parent 2856511 commit 6a4b0ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
32 changes: 28 additions & 4 deletions exe/pgpm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "English"
require "bundler/setup"
require "pgpm"
require "dry/cli"
Expand Down Expand Up @@ -117,21 +118,44 @@ module Pgpm
include SharedOptions

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

def call(query:, args: nil, pkgdir: nil)
def call(query:, args: nil, pkgdir: nil, format: nil)
_ = args

Pgpm.load_packages(pkgdir)

query_regexp = Regexp.new(query, "i")
if format == "json"
puts "["
end

Parallel.filter_map(Pgpm::Package, in_threads: Etc.nprocessors) do |p|
next if p.contrib?

matches = p.all_searchable_texts.any? do |t|
p.all_searchable_texts.any? do |t|
t =~ query_regexp
end
"#{p.package_name}: #{p.description}" if matches
end.each { |l| puts l }
output = case format
when "json" then Oj.dump({
name: p.package_name,
summary: p.summary,
description: p.description,
license: p.license,
versions: p.package_versions.map(&:to_s)
}, mode: :strict)
else
"#{p.package_name}: #{p.description}"
end
# printer.send(output, move: true) if matches
puts output
rescue StandardError
warn "Error fetching #{p.package_name}: #{$ERROR_INFO.message}"
end

return unless format == "json"

puts "]"
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/pgpm/package/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def license
self[:latest]&.license
end

def release_date
self[:latest]&.release_date
end

def all_searchable_texts
self[:latest]&.all_searchable_texts || []
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pgpm/podman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module Pgpm
module Podman
def self.run(command, unhandled_reboot_mitigation: true, print_stdout: true)
result = TTY::Command.new(printer: :null).run("podman #{command}", pty: true) do |out, err|
print out if print_stdout
print err
warn out if print_stdout
warn err
end

result.out
Expand Down

0 comments on commit 6a4b0ba

Please sign in to comment.