91 lines
2.5 KiB
Ruby
Raw Normal View History

2016-09-24 13:52:43 +02:00
module Hbc
class CLI
class List < Base
def self.run(*arguments)
@options = {}
@options[:one] = true if arguments.delete("-1")
@options[:versions] = true if arguments.delete("--versions")
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
if arguments.delete("-l")
@options[:one] = true
opoo "Option -l is obsolete! Implying option -1."
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
retval = arguments.any? ? list(*arguments) : list_installed
# retval is ternary: true/false/nil
if retval.nil? && !arguments.any?
opoo "nothing to list" # special case: avoid exit code
elsif retval.nil?
raise CaskError, "nothing to list"
elsif !retval
raise CaskError, "listing incomplete"
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.list(*cask_tokens)
count = 0
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
cask_tokens.each do |cask_token|
odebug "Listing files for Cask #{cask_token}"
begin
cask = Hbc.load(cask_token)
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
if cask.installed?
if @options[:one]
puts cask.token
elsif @options[:versions]
puts format_versioned(cask)
else
installed_caskfile = cask.metadata_master_container_path.join(*cask.timestamped_versions.last, "Casks", "#{cask_token}.rb")
cask = Hbc.load(installed_caskfile)
list_artifacts(cask)
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
count += 1
else
opoo "#{cask} is not installed"
end
rescue CaskUnavailableError => e
onoe e
end
2016-08-18 22:11:42 +03:00
end
2016-09-24 13:52:43 +02:00
count.zero? ? nil : count == cask_tokens.length
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.list_artifacts(cask)
Artifact.for_cask(cask).each do |artifact|
summary = artifact.summary
2016-09-24 13:52:43 +02:00
ohai summary[:english_description], summary[:contents] unless summary.empty?
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.list_installed
installed_casks = Hbc.installed
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
if @options[:one]
puts installed_casks.map(&:to_s)
elsif @options[:versions]
puts installed_casks.map(&method(:format_versioned))
elsif !installed_casks.empty?
puts Formatter.columns(installed_casks.map(&:to_s))
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
installed_casks.empty? ? nil : true
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.format_versioned(cask)
cask.to_s.concat(cask.versions.map(&:to_s).join(" ").prepend(" "))
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def self.help
"with no args, lists installed Casks; given installed Casks, lists staged files"
end
2016-09-24 13:52:43 +02:00
def self.needs_init?
2017-03-06 21:28:34 +01:00
true
2016-09-24 13:52:43 +02:00
end
end
end
2016-08-18 22:11:42 +03:00
end