mirror of
https://github.com/jimeh/dotfiles.git
synced 2026-02-19 13:46:41 +00:00
fix(xbar/brew-updates): handle unexpected config values better
Also refactor Xbar::Config and and Brew::Common classes a bit to make them more generic, and have more features.
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
# rubocop:disable Layout/LineLength
|
# rubocop:disable Layout/LineLength
|
||||||
|
|
||||||
# <xbar.title>Brew Updates</xbar.title>
|
# <xbar.title>Brew Updates</xbar.title>
|
||||||
# <xbar.version>v2.5.0</xbar.version>
|
# <xbar.version>v2.5.1</xbar.version>
|
||||||
# <xbar.author>Jim Myhrberg</xbar.author>
|
# <xbar.author>Jim Myhrberg</xbar.author>
|
||||||
# <xbar.author.github>jimeh</xbar.author.github>
|
# <xbar.author.github>jimeh</xbar.author.github>
|
||||||
# <xbar.desc>List and manage outdated Homebrew formulas and casks</xbar.desc>
|
# <xbar.desc>List and manage outdated Homebrew formulas and casks</xbar.desc>
|
||||||
@@ -27,8 +27,35 @@
|
|||||||
|
|
||||||
require 'open3'
|
require 'open3'
|
||||||
require 'json'
|
require 'json'
|
||||||
|
require 'set'
|
||||||
|
|
||||||
module Xbar
|
module Xbar
|
||||||
|
class CommandError < StandardError; end
|
||||||
|
|
||||||
|
module Service
|
||||||
|
private
|
||||||
|
|
||||||
|
def config
|
||||||
|
@config ||= Xbar::Config.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def printer
|
||||||
|
@printer ||= ::Xbar::Printer.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def cmd(*args)
|
||||||
|
out, err, s = Open3.capture3(*args)
|
||||||
|
if s.exitstatus != 0
|
||||||
|
msg = "Command failed: #{args.join(' ')}"
|
||||||
|
msg += ": #{err}" unless err.empty?
|
||||||
|
|
||||||
|
raise CommandError, msg
|
||||||
|
end
|
||||||
|
|
||||||
|
out
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Runner
|
class Runner
|
||||||
attr_reader :service
|
attr_reader :service
|
||||||
|
|
||||||
@@ -53,6 +80,12 @@ module Xbar
|
|||||||
merge!(JSON.parse(File.read(filename)))
|
merge!(JSON.parse(File.read(filename)))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def as_set(name)
|
||||||
|
values = self[name]&.to_s&.split(',')&.map(&:strip)&.reject(&:empty?)
|
||||||
|
|
||||||
|
::Set.new(values || [])
|
||||||
|
end
|
||||||
|
|
||||||
def filename
|
def filename
|
||||||
@filename ||= "#{__FILE__}.vars.json"
|
@filename ||= "#{__FILE__}.vars.json"
|
||||||
end
|
end
|
||||||
@@ -147,9 +180,9 @@ module Xbar
|
|||||||
end
|
end
|
||||||
|
|
||||||
module Brew
|
module Brew
|
||||||
class CommandError < StandardError; end
|
|
||||||
|
|
||||||
class Common
|
class Common
|
||||||
|
include Xbar::Service
|
||||||
|
|
||||||
def self.prefix(value = nil)
|
def self.prefix(value = nil)
|
||||||
return @prefix if value.nil? || value == ''
|
return @prefix if value.nil? || value == ''
|
||||||
|
|
||||||
@@ -162,17 +195,6 @@ module Brew
|
|||||||
self.class.prefix
|
self.class.prefix
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_printer
|
|
||||||
@default_printer ||= ::Xbar::Printer.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def cmd(*args)
|
|
||||||
out, err, s = Open3.capture3(*args)
|
|
||||||
raise CommandError, "#{args.join(' ')}: #{err}" if s.exitstatus != 0
|
|
||||||
|
|
||||||
out
|
|
||||||
end
|
|
||||||
|
|
||||||
def brew_path
|
def brew_path
|
||||||
@brew_path ||= brew_path_from_env ||
|
@brew_path ||= brew_path_from_env ||
|
||||||
brew_path_from_which ||
|
brew_path_from_which ||
|
||||||
@@ -181,9 +203,12 @@ module Brew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def brew_path_from_env
|
def brew_path_from_env
|
||||||
return if ENV['VAR_BREW_PATH'].to_s == ''
|
env_value = config['VAR_BREW_PATH']&.to_s&.strip || ''
|
||||||
|
|
||||||
ENV['VAR_BREW_PATH']
|
return if env_value == ''
|
||||||
|
return unless File.exist?(env_value)
|
||||||
|
|
||||||
|
env_value
|
||||||
end
|
end
|
||||||
|
|
||||||
def brew_path_from_which
|
def brew_path_from_which
|
||||||
@@ -191,6 +216,8 @@ module Brew
|
|||||||
return if detect == ''
|
return if detect == ''
|
||||||
|
|
||||||
detect
|
detect
|
||||||
|
rescue Xbar::CommandError
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def brew_path_from_fs_check
|
def brew_path_from_fs_check
|
||||||
@@ -217,13 +244,6 @@ module Brew
|
|||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def brew_update
|
|
||||||
cmd(brew_path, 'update')
|
|
||||||
rescue CommandError
|
|
||||||
# Continue as if nothing happened when brew update fails, as it likely
|
|
||||||
# to be due to another update process is already running.
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Formula
|
class Formula
|
||||||
@@ -259,8 +279,6 @@ module Brew
|
|||||||
prefix ':beers:'
|
prefix ':beers:'
|
||||||
|
|
||||||
def run
|
def run
|
||||||
printer = default_printer
|
|
||||||
|
|
||||||
brew_check(printer)
|
brew_check(printer)
|
||||||
brew_update
|
brew_update
|
||||||
|
|
||||||
@@ -338,23 +356,28 @@ module Brew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_greedy(*args)
|
def add_greedy(*args)
|
||||||
vals = config['VAR_GREEDY']&.split(',') || []
|
vals = greedy_types.clone
|
||||||
vals += args
|
vals += args.map(&:strip).reject(&:empty?)
|
||||||
config['VAR_GREEDY'] = vals.uniq.sort.join(',')
|
|
||||||
|
config['VAR_GREEDY'] = vals.sort.join(',')
|
||||||
config.save
|
config.save
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_greedy(*args)
|
def remove_greedy(*args)
|
||||||
vals = config['VAR_GREEDY']&.split(',') || []
|
vals = greedy_types.clone
|
||||||
vals -= args
|
vals -= args.map(&:strip).reject(&:empty?)
|
||||||
config['VAR_GREEDY'] = vals.uniq.sort.join(',')
|
|
||||||
|
config['VAR_GREEDY'] = vals.sort.join(',')
|
||||||
config.save
|
config.save
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def config
|
def brew_update
|
||||||
@config ||= Xbar::Config.new
|
cmd(brew_path, 'update')
|
||||||
|
rescue Xbar::CommandError
|
||||||
|
# Continue as if nothing happened when brew update fails, as it likely
|
||||||
|
# to be due to another update process is already running.
|
||||||
end
|
end
|
||||||
|
|
||||||
def status_label
|
def status_label
|
||||||
@@ -487,15 +510,15 @@ module Brew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def greedy_types
|
def greedy_types
|
||||||
config['VAR_GREEDY']&.split(',')&.map(&:to_sym) || []
|
@greedy_types ||= config.as_set('VAR_GREEDY')
|
||||||
end
|
end
|
||||||
|
|
||||||
def greedy_latest?
|
def greedy_latest?
|
||||||
greedy_types.include?(:latest)
|
@greedy_latest ||= greedy_types.include?('latest')
|
||||||
end
|
end
|
||||||
|
|
||||||
def greedy_auto_updates?
|
def greedy_auto_updates?
|
||||||
greedy_types.include?(:auto_updates)
|
@greedy_auto_updates ||= greedy_types.include?('auto_updates')
|
||||||
end
|
end
|
||||||
|
|
||||||
def greedy_args
|
def greedy_args
|
||||||
@@ -516,11 +539,19 @@ module Brew
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
updates = Brew::FormulaUpdates.new
|
services = Brew::FormulaUpdates.new
|
||||||
Xbar::Runner.new(updates).run(ARGV)
|
Xbar::Runner.new(services).run(ARGV)
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
puts "ERROR: #{e.message}:\n\t#{e.backtrace.join("\n\t")}"
|
puts ":warning: #{File.basename(__FILE__)}"
|
||||||
exit 1
|
puts '---'
|
||||||
|
puts 'exit status 1'
|
||||||
|
puts '---'
|
||||||
|
puts 'Error:'
|
||||||
|
puts e.message.to_s
|
||||||
|
e.backtrace.each do |line|
|
||||||
|
puts "--#{line}"
|
||||||
|
end
|
||||||
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
# rubocop:enable Style/IfUnlessModifier
|
# rubocop:enable Style/IfUnlessModifier
|
||||||
|
|||||||
Reference in New Issue
Block a user