Merge pull request #3084 from laughedelic/cask/search-offline

Offline cask search and listing all available casks
This commit is contained in:
Markus Reiter 2017-09-02 15:28:05 +02:00 committed by GitHub
commit 2f133725a2
4 changed files with 64 additions and 20 deletions

View File

@ -2,8 +2,12 @@ module Hbc
class CLI class CLI
class Search < AbstractCommand class Search < AbstractCommand
def run def run
results = self.class.search(*args) if args.empty?
self.class.render_results(*results) puts Formatter.columns(CLI.nice_listing(Hbc.all_tokens))
else
results = self.class.search(*args)
self.class.render_results(*results)
end
end end
def self.extract_regexp(string) def self.extract_regexp(string)
@ -15,8 +19,17 @@ module Hbc
end end
def self.search_remote(query) def self.search_remote(query)
matches = GitHub.search_code(user: "caskroom", path: "Casks", matches = begin
filename: query, extension: "rb") GitHub.search_code(
user: "caskroom",
path: "Casks",
filename: query,
extension: "rb",
)
rescue GitHub::Error => error
opoo "Error searching on GitHub: #{error}\n"
[]
end
matches.map do |match| matches.map do |match|
tap = Tap.fetch(match["repository"]["full_name"]) tap = Tap.fetch(match["repository"]["full_name"])
next if tap.installed? next if tap.installed?

View File

@ -1,5 +1,5 @@
brew-cask(1) - a friendly binary installer for macOS brew-cask(1) - a friendly binary installer for macOS
======================================================== ====================================================
## SYNOPSIS ## SYNOPSIS
@ -101,9 +101,10 @@ names, and other aspects of this manual are still subject to change.
Reinstall the given Cask. Reinstall the given Cask.
* `search` or `-S` [<text> | /<regexp>/]: * `search` or `-S` [<text> | /<regexp>/]:
Without an argument, display all Casks available for install; otherwise Without an argument, display all locally available Casks for install; no
perform a substring search of known Cask tokens for <text> or, if the online search is performed.
text is delimited by slashes (/<regexp>/), it is interpreted as a Otherwise perform a substring search of known Cask tokens for <text> or,
if the text is delimited by slashes (/<regexp>/), it is interpreted as a
Ruby regular expression. Ruby regular expression.
* `style` [--fix] [ <token> ... ]: * `style` [--fix] [ <token> ... ]:

View File

@ -22,16 +22,36 @@ describe Hbc::CLI::Search, :cask do
EOS EOS
end end
it "returns matches even when online search failed" do
allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason"))
expect {
Hbc::CLI::Search.run("local")
}.to output(<<-EOS.undent).to_stdout
local-caffeine
local-transmission
EOS
.and output(/^Warning: Error searching on GitHub: reason/).to_stderr
end
it "shows that there are no Casks matching a search term that did not result in anything" do it "shows that there are no Casks matching a search term that did not result in anything" do
expect { expect {
Hbc::CLI::Search.run("foo-bar-baz") Hbc::CLI::Search.run("foo-bar-baz")
}.to output("No Cask found for \"foo-bar-baz\".\n").to_stdout.as_tty }.to output(<<-EOS.undent).to_stdout.as_tty
No Cask found for "foo-bar-baz".
EOS
end end
it "lists all available Casks with no search term" do it "doesn't output anything to non-TTY stdout when there are no matches" do
expect { expect { Hbc::CLI::Search.run("foo-bar-baz") }
Hbc::CLI::Search.run .to not_to_output.to_stdout
}.to output(/local-caffeine/).to_stdout.as_tty .and not_to_output.to_stderr
end
it "lists all Casks available offline with no search term" do
allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason"))
expect { Hbc::CLI::Search.run }
.to output(/local-caffeine/).to_stdout.as_tty
.and not_to_output.to_stderr
end end
it "ignores hyphens in search terms" do it "ignores hyphens in search terms" do
@ -55,19 +75,29 @@ describe Hbc::CLI::Search, :cask do
it "accepts a regexp argument" do it "accepts a regexp argument" do
expect { expect {
Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/") Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/")
}.to output("==> Regexp Matches\nlocal-caffeine\n").to_stdout.as_tty }.to output(<<-EOS.undent).to_stdout.as_tty
==> Regexp Matches
local-caffeine
EOS
end end
it "Returns both exact and partial matches" do it "returns both exact and partial matches" do
expect { expect {
Hbc::CLI::Search.run("test-opera") Hbc::CLI::Search.run("test-opera")
}.to output(/^==> Exact Match\ntest-opera\n==> Partial Matches\ntest-opera-mail/).to_stdout.as_tty }.to output(<<-EOS.undent).to_stdout.as_tty
==> Exact Match
test-opera
==> Partial Matches
test-opera-mail
EOS
end end
it "does not search the Tap name" do it "does not search the Tap name" do
expect { expect {
Hbc::CLI::Search.run("caskroom") Hbc::CLI::Search.run("caskroom")
}.to output(/^No Cask found for "caskroom"\.\n/).to_stdout.as_tty }.to output(<<-EOS.undent).to_stdout.as_tty
No Cask found for "caskroom".
EOS
end end
it "doesn't highlight packages that aren't installed" do it "doesn't highlight packages that aren't installed" do

View File

@ -103,7 +103,7 @@ Reinstall the given Cask\.
. .
.TP .TP
\fBsearch\fR or \fB\-S\fR [\fItext\fR | /\fIregexp\fR/] \fBsearch\fR or \fB\-S\fR [\fItext\fR | /\fIregexp\fR/]
Without an argument, display all Casks available for install; otherwise perform a substring search of known Cask tokens for \fItext\fR or, if the text is delimited by slashes (/\fIregexp\fR/), it is interpreted as a Ruby regular expression\. Without an argument, display all locally available Casks for install; no online search is performed\. Otherwise perform a substring search of known Cask tokens for \fItext\fR or, if the text is delimited by slashes (/\fIregexp\fR/), it is interpreted as a Ruby regular expression\.
. .
.TP .TP
\fBstyle\fR [\-\-fix] [ \fItoken\fR \.\.\. ] \fBstyle\fR [\-\-fix] [ \fItoken\fR \.\.\. ]