diff --git a/Library/Homebrew/dev-cmd/cat.rb b/Library/Homebrew/dev-cmd/cat.rb index 0d8c70eecd..e0e6d46fce 100644 --- a/Library/Homebrew/dev-cmd/cat.rb +++ b/Library/Homebrew/dev-cmd/cat.rb @@ -1,62 +1,65 @@ -# typed: true +# typed: strict # frozen_string_literal: true +require "abstract_command" require "cli/parser" module Homebrew - sig { returns(CLI::Parser) } - def self.cat_args - Homebrew::CLI::Parser.new do - description <<~EOS - Display the source of a or . - EOS + module DevCmd + class Cat < AbstractCommand + include FileUtils - switch "--formula", "--formulae", - description: "Treat all named arguments as formulae." - switch "--cask", "--casks", - description: "Treat all named arguments as casks." + cmd_args do + description <<~EOS + Display the source of a or . + EOS - conflicts "--formula", "--cask" + switch "--formula", "--formulae", + description: "Treat all named arguments as formulae." + switch "--cask", "--casks", + description: "Treat all named arguments as casks." - named_args [:formula, :cask], min: 1, without_api: true - end - end + conflicts "--formula", "--cask" - def self.cat - args = cat_args.parse - - cd HOMEBREW_REPOSITORY do - pager = if Homebrew::EnvConfig.bat? - ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path - ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme - ensure_formula_installed!( - "bat", - reason: "displaying / source", - # The user might want to capture the output of `brew cat ...` - # Redirect stdout to stderr - output_to_stderr: true, - ).opt_bin/"bat" - else - "cat" + named_args [:formula, :cask], min: 1, without_api: true end - args.named.to_paths.each do |path| - next path if path.exist? + sig { override.void } + def run + cd HOMEBREW_REPOSITORY do + pager = if Homebrew::EnvConfig.bat? + ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path + ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme + ensure_formula_installed!( + "bat", + reason: "displaying / source", + # The user might want to capture the output of `brew cat ...` + # Redirect stdout to stderr + output_to_stderr: true, + ).opt_bin/"bat" + else + "cat" + end - path = path.basename(".rb") if args.cask? + args.named.to_paths.each do |path| + next path if path.exist? - ofail "#{path}'s source doesn't exist on disk." + path = path.basename(".rb") if args.cask? + + ofail "#{path}'s source doesn't exist on disk." + end + + if Homebrew.failed? + $stderr.puts "The name may be wrong, or the tap hasn't been tapped. Instead try:" + treat_as = "--cask " if args.cask? + treat_as = "--formula " if args.formula? + $stderr.puts " brew info --github #{treat_as}#{args.named.join(" ")}" + return + end + + safe_system pager, *args.named.to_paths + end end - - if Homebrew.failed? - $stderr.puts "The name may be wrong, or the tap hasn't been tapped. Instead try:" - treat_as = "--cask " if args.cask? - treat_as = "--formula " if args.formula? - $stderr.puts " brew info --github #{treat_as}#{args.named.join(" ")}" - return - end - - safe_system pager, *args.named.to_paths end end end diff --git a/Library/Homebrew/test/dev-cmd/cat_spec.rb b/Library/Homebrew/test/dev-cmd/cat_spec.rb index 162e3d4f3d..b00cd32f81 100644 --- a/Library/Homebrew/test/dev-cmd/cat_spec.rb +++ b/Library/Homebrew/test/dev-cmd/cat_spec.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" +require "dev-cmd/cat" -RSpec.describe "brew cat" do - it_behaves_like "parseable arguments" +RSpec.describe Homebrew::DevCmd::Cat do + it_behaves_like "parseable arguments", argv: ["foo"] it "prints the content of a given Formula", :integration_test do formula_file = setup_test_formula "testball"