From 2ed6d14bb5b93743e6dc1708242c26d76ea61978 Mon Sep 17 00:00:00 2001 From: Marc Ransome Date: Wed, 30 Oct 2024 17:51:44 +0000 Subject: [PATCH] Refactor in AbstractCommand style --- cmd/keg.rb | 72 ++++++++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/cmd/keg.rb b/cmd/keg.rb index e972b4b..cf41b25 100755 --- a/cmd/keg.rb +++ b/cmd/keg.rb @@ -1,60 +1,36 @@ -# typed: true +# typed: strict # frozen_string_literal: true -require "cli/parser" -require "formula" - -class FormulaNotInstalledError < RuntimeError - extend T::Sig - - attr_reader :name - - def initialize(name) - super() - - @name = name - end - - sig { returns(String) } - def to_s - "The formula with name \"#{name}\" is not installed.".strip - end -end +require "abstract_command" module Homebrew - extend T::Sig + module Cmd + class Keg < AbstractCommand + cmd_args do + description <<~EOS + Open the keg directory for in Finder. + EOS - module_function + switch "-p", "--path", + description: "Print the path only." - sig { returns(CLI::Parser) } - def keg_args - Homebrew::CLI::Parser.new do - description <<~EOS - Open the keg directory for in Finder. - EOS - - switch "-p", "--path", - description: "Print the path only." - - named_args [:formula], min: 1 - end - end - - sig { void } - def keg - args = keg_args.parse + named_args [:formula], min: 1 + end - formulae = args.named.to_resolved_formulae + def run + formulae = args.named.to_resolved_formulae - formulae.each do |formula| - if !formula.opt_prefix.exist? - raise FormulaNotInstalledError, formula - end + formulae.each do |formula| + if !formula.opt_prefix.exist? + odie "Formula not installed: #{formula}" + end - if args.path? - puts File.realpath(formula.opt_prefix.to_s) - else - system("open", formula.opt_prefix.to_s) + if args.path? + puts File.realpath(formula.opt_prefix.to_s) + else + system("open", formula.opt_prefix.to_s) + end + end end end end