Skip to content

Commit

Permalink
Merge pull request #7 from marcransome/refactor-command
Browse files Browse the repository at this point in the history
Refactor in AbstractCommand style
  • Loading branch information
marcransome authored Oct 30, 2024
2 parents cef0541 + 2ed6d14 commit fb0dc96
Showing 1 changed file with 24 additions and 48 deletions.
72 changes: 24 additions & 48 deletions cmd/keg.rb
Original file line number Diff line number Diff line change
@@ -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 <formula> 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 <formula> 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
Expand Down

0 comments on commit fb0dc96

Please sign in to comment.