Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

info: add requires_sudo? and --machine-readable #8819

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/man/brew-cask.1
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ Display the homepage associated with a given Cask in a browser\.
With no arguments, display the project page \fIhttp://caskroom\.io\fR\.
.
.TP
\fBinfo\fR or \fBabv\fR \fItoken\fR [ \fItoken\fR \.\.\. ]
Display information about the given Cask\.
\fBinfo\fR or \fBabv\fR \fItoken\fR [ \fItoken\fR \.\.\. ] [\-\-machine\-readable]
Display information about the given Cask\. With \fB\-\-machine-readable\fR, output in JSON format\.
.
.TP
\fBinstall [\-\-force]\fR \fItoken\fR [ \fItoken\fR \.\.\. ]
Expand Down
4 changes: 4 additions & 0 deletions lib/hbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ def installed?
staged_path.exist?
end

def requires_sudo?
artifacts[:pkg].any?
end

def to_s
@token
end
Expand Down
44 changes: 39 additions & 5 deletions lib/hbc/cli/info.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
require 'json'

class Hbc::CLI::Info < Hbc::CLI::Base
def self.run(*args)
cask_tokens = cask_tokens_from(args)
raise Hbc::CaskUnspecifiedError if cask_tokens.empty?
if args.include?('--machine-readable')
machine_readable_info(cask_tokens)
else
human_readable_info(cask_tokens)
end
end

def self.help
"displays information about the given Cask"
end

def self.human_readable_info(cask_tokens)
cask_tokens.each do |cask_token|
odebug "Getting info for Cask #{cask_token}"
cask = Hbc.load(cask_token)
Expand All @@ -10,8 +24,18 @@ def self.run(*args)
end
end

def self.help
"displays information about the given Cask"
def self.machine_readable_info(cask_tokens)
cask_tokens.each do |cask_token|
cask = Hbc.load(cask_token)
puts JSON.pretty_generate(
name: cask.to_s,
full_name: formatted_full_name(cask),
installed: cask.installed?,
homepage: cask.homepage,
github_url: github_url(cask),
requires_sudo: cask.requires_sudo?,
)
end
end

def self.info(cask)
Expand All @@ -26,17 +50,19 @@ def self.info(cask)
#{formatted_full_name(cask) }
#{cask.homepage or 'No Homepage'}
#{installation}
#{github_info(cask) or 'No GitHub URL'}
#{github_url(cask) or 'No GitHub URL'}
#{requires_sudo(cask)}
#{artifact_info(cask) or 'No Artifact Info'}
PURPOSE
end


def self.formatted_full_name(cask)
# todo transitional: make name a required stanza, and then stop substituting cask.token here
cask.full_name.empty? ? cask.token : cask.full_name.join(', ')
end

def self.github_info(cask)
def self.github_url(cask)
cask_token = cask.token
cask_token = cask.class.all_tokens.detect { |t| t.split("/").last == cask_token } unless cask_token =~ /\//
return nil unless cask_token.respond_to?(:length) and cask_token.length > 0
Expand All @@ -60,7 +86,7 @@ def self.artifact_info(cask)
retval = ''
Hbc::DSL::ClassMethods.ordinary_artifact_types.each do |type|
if cask.artifacts[type].length > 0
retval = "#{Tty.blue.bold}==>#{Tty.white} Contents#{Tty.reset}\n" unless retval.length > 0
retval = heading("Contents") unless retval.length > 0
cask.artifacts[type].each do |artifact|
activatable_item = type == :stage_only ? '<none>' : artifact.first
retval.concat " #{activatable_item} (#{type.to_s})\n"
Expand All @@ -69,4 +95,12 @@ def self.artifact_info(cask)
end
retval.sub!(/\n\Z/, '')
end

def self.requires_sudo(cask)
heading("Needs sudo to install?") + cask.requires_sudo?.to_s
end

def self.heading(message)
"#{Tty.blue.bold}==>#{Tty.white} #{message}#{Tty.reset}\n"
end
end
26 changes: 26 additions & 0 deletions test/cask/cli/info_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
http://example.com/local-caffeine
Not installed
https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/local-caffeine.rb
==> Needs sudo to install?
false
==> Contents
Caffeine.app (app)
CLIOUTPUT
Expand All @@ -23,13 +25,17 @@
http://example.com/local-caffeine
Not installed
https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/local-caffeine.rb
==> Needs sudo to install?
false
==> Contents
Caffeine.app (app)
local-transmission: 2.61
local-transmission
http://example.com/local-transmission
Not installed
https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/local-transmission.rb
==> Needs sudo to install?
false
==> Contents
Transmission.app (app)
CLIOUTPUT
Expand Down Expand Up @@ -57,6 +63,8 @@
http://example.com/local-caffeine
Not installed
https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/with-caveats.rb
==> Needs sudo to install?
false
==> Contents
Caffeine.app (app)
==> Caveats
Expand All @@ -82,6 +90,8 @@
http://example.com/local-caffeine
Not installed
https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/with-conditional-caveats.rb
==> Needs sudo to install?
false
==> Contents
Caffeine.app (app)
CLIOUTPUT
Expand All @@ -102,4 +112,20 @@
}.must_raise Hbc::CaskUnspecifiedError
end
end

describe 'with --machine-readable' do
it 'outputs JSON' do
out, err = capture_io do
Hbc::CLI::Info.run('local-caffeine', '--machine-readable')
end

parsed_info = JSON.parse(out)
parsed_info['name'].must_equal 'local-caffeine'
parsed_info['full_name'].must_equal 'local-caffeine'
parsed_info['installed'].must_equal false
parsed_info['homepage'].must_equal 'http://example.com/local-caffeine'
parsed_info['github_url'].must_equal 'https://github.com/caskroom/homebrew-testcasks/blob/master/Casks/local-caffeine.rb'
parsed_info['requires_sudo'].must_equal false
end
end
end
14 changes: 14 additions & 0 deletions test/cask_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,18 @@
c.metadata_versioned_container_path.to_s.must_equal(metadata_path.to_s)
end
end

describe "requires_sudo?" do
it "is true with a cask that has pkg artifacts" do
cask = Hbc.load('with-installable')

cask.requires_sudo?.must_equal true
end

it "is false when there are no pkg artifacts" do
cask = Hbc.load('tarball')

cask.requires_sudo?.must_equal false
end
end
end
1 change: 1 addition & 0 deletions test/support/Casks/Casks