Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using persistent config file for execution #174

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ language: ruby
os: osx

rvm:
# - 2.5
- 2.6
# - 2.7

before_install:
- brew update
Expand All @@ -21,4 +19,8 @@ script:
- cd /usr/local/Homebrew/Library/Taps/buo/homebrew-cask-upgrade
- rubocop --version
- rubocop
- brew cu pin java
- brew cu pinned
- brew cu -y --no-brew-update
- brew cu unpin java
- brew cu -y --no-brew-update
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ Options:
-q, --quiet Do not show information about installed apps or current options.
-v, --verbose Make output more verbose.
--no-quarantine Pass --no-quarantine option to `brew cask install`.
-i, --interactive Running update in interactive mode
-i, --interactive Running update in interactive mode
--ignore-config Ignores any changes made to a default config
```

Display usage instructions:
Expand All @@ -105,3 +106,22 @@ For every cask it is then possible to use following options:

Pinned apps will not be updated by `brew cu` until they are unpinned.
NB: version pinning in `brew cu` will not prevent `brew cask upgrade` from updating pinned apps.

### Default config

In order to have less verbose execution, it is possible to alter default config under which the upgrade gets executed.
That could be done by changing `~/.brew-cu` config file.
This is the default config setting the default options:
```yaml
---
all: false
force: false
cleanup: false
force_yes: false
no_brew_update: false
quiet: false
verbose: false
interactive: false
```

If any changes to this config were made, those changes can be ignored by passing `--ignore-config` option.
19 changes: 10 additions & 9 deletions brew-cask-upgrade.gemspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Gem::Specification.new do |s|
s.name = "brew-cask-upgrade"
s.version = "2.0.1"
s.summary = "A command line tool for Homebrew Cask"
s.name = "brew-cask-upgrade"
s.version = "2.0.1"
s.summary = "A command line tool for Homebrew Cask"
s.description = "A command line tool for upgrading every outdated app installed by Homebrew Cask"
s.authors = ["buo"]
s.email = "[email protected]"
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
s.homepage = "https://github.com/buo/homebrew-cask-upgrade"
s.license = "MIT"
s.authors = ["buo"]
s.email = "[email protected]"
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
s.homepage = "https://github.com/buo/homebrew-cask-upgrade"
s.license = "MIT"
s.required_ruby_version = 2.6

s.bindir = "bin"
s.bindir = "bin"
s.executables = %w[brew-cask-upgrade]
end
2 changes: 2 additions & 0 deletions cmd/brew-cu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#: If `--no-quarantine` is passed, that option will be added to the install
#: command (see `man brew-cask` for reference)
#:
#: If `--ignore-config` is passed, changes to user config file fill be ignored.
#:
#:`INTERACTIVE MODE`:
#: After listing casks to upgrade you want those casks to be upgraded.
#: By using the option `i` you will step into an interactive mode.
Expand Down
2 changes: 1 addition & 1 deletion lib/bcu/command/pin_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def list_pinned(_args, _options)
def add_cask(cask_name, casks)
casks.push Cask.load_cask(cask_name)
rescue Cask::CaskUnavailableError
Bcu::Pin::Remove.remove_pin cask_name
Bcu::Pin::Remove.remove_pin cask: cask_name
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bcu/command/pin_remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def process(args, options)
# TODO: If we used deprecated --pin option, the value is not any more in the args
pin = options.unpin if pin.nil?

remove_pin pin
remove_pin cask: pin
end

private

def remove_pin(cask, quiet = false)
def remove_pin(cask:, quiet: false)
unless Pin.pinned.include? cask
puts "Not pinned: #{Tty.green}#{cask}#{Tty.reset}" unless quiet
return
Expand Down
27 changes: 11 additions & 16 deletions lib/bcu/command/upgrade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def process(_args, options)
printf "Do you want to upgrade %<count>d app%<s>s or enter [i]nteractive mode [y/i/N]? ",
count: outdated.length,
s: (outdated.length > 1) ? "s" : ""
input = STDIN.gets.strip
input = $stdin.gets.strip

if input.casecmp("i").zero?
options.interactive = true
Expand Down Expand Up @@ -60,7 +60,7 @@ def upgrade(app, options, state_info)
formatting = Formatter.formatting_for_app(state_info, app, options)
printf 'Do you want to upgrade "%<app>s", [p]in it to exclude it from updates or [q]uit [y/p/q/N]? ',
app: Formatter.colorize(app[:token], formatting[0])
input = STDIN.gets.strip
input = $stdin.gets.strip

if input.casecmp("p").zero?
cmd = Bcu::Pin::Add.new
Expand All @@ -78,20 +78,18 @@ def upgrade(app, options, state_info)

ohai "Upgrading #{app[:token]} to #{app[:version]}"
installation_successful = install app, options

if installation_successful
installation_cleanup app, options
end
installation_cleanup app, options if installation_successful
end

def install(app, options)
verbose_flag = options.verbose ? "--verbose" : ""
debug_flag = options.debug ? "--debug" : ""

begin
# Force to install the latest version.
app_str = app[:tap].nil? ? app[:token] : "#{app[:tap]}/#{app[:token]}"
cmd = "brew reinstall #{options.install_options} #{app_str} --force " + verbose_flag
success = system "#{cmd}"
cmd = "brew reinstall #{options.install_options} #{app_str} --force #{verbose_flag} #{debug_flag}"
success = system cmd
rescue
success = false
end
Expand Down Expand Up @@ -120,10 +118,7 @@ def find_outdated_apps(installed, options)
found
end

if installed.empty?
print_install_empty_message options.casks
exit 1
end
odie empty_message(options.casks) if installed.empty?
end

installed.each do |app|
Expand Down Expand Up @@ -152,15 +147,15 @@ def find_outdated_apps(installed, options)
[outdated, state_info]
end

def print_install_empty_message(cask_searched)
def empty_message(cask_searched)
if cask_searched.length == 1
if cask_searched[0].end_with? "*"
onoe "#{Tty.red}No Cask matching \"#{cask_searched[0]}\" is installed.#{Tty.reset}"
"#{Tty.red}No Cask matching \"#{cask_searched[0]}\" is installed.#{Tty.reset}"
else
onoe "#{Tty.red}Cask \"#{cask_searched[0]}\" is not installed.#{Tty.reset}"
"#{Tty.red}Cask \"#{cask_searched[0]}\" is not installed.#{Tty.reset}"
end
else
onoe "#{Tty.red}No casks matching your arguments found.#{Tty.reset}"
"#{Tty.red}No casks matching your arguments found.#{Tty.reset}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bcu/module/pin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Bcu
module Pin
PINS_FILE = File.expand_path(File.dirname(__FILE__) + "/../../../pinned")
PINS_FILE = File.expand_path(File.dirname(__FILE__) + "/../../../pinned").freeze

module_function

Expand Down
100 changes: 83 additions & 17 deletions lib/bcu/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@ class << self
end

def self.parse!(args)
options_struct = Struct.new(:all, :force, :casks, :cleanup, :force_yes, :no_brew_update, :quiet, :verbose,
:install_options, :list_pinned, :pin, :unpin, :interactive, :command)
options = options_struct.new
options.all = false
options.force = false
options.casks = nil
options.cleanup = false
options.force_yes = false
options.no_brew_update = false
options.quiet = false
options.verbose = false
options.install_options = ""
options.list_pinned = false
options.pin = nil
options.unpin = nil
options.interactive = false
options.command = "run"
options = build_config

parser = OptionParser.new do |opts|
opts.banner = "Usage: brew cu [CASK] [options]"

opts.on("--ignore-config") do
options = build_config false
end

opts.on("--debug") do
options.debug = true
end

# Prevent using short -p syntax for pinning
opts.on("-p") do
onoe "invalid option -p, did you mean --pin?"
Expand Down Expand Up @@ -126,4 +118,78 @@ def self.validate_options(options)
exit 1
end
end

def self.build_config(use_config_file: true)
options = use_config_file ? load_default_options : create_default_options

options.casks = nil
options.install_options = ""
options.list_pinned = false
options.pin = nil
options.unpin = nil
options.command = "run"

options
end

def self.load_default_options
config_filename = "#{ENV["HOME"]}/.brew-cu"
unless File.exist?(config_filename)
odebug "Config file doesn't exist, creating"
create_default_config_file config_filename
end
Comment on lines +137 to +140
Copy link
Contributor

@muescha muescha Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't expect here as an user that a normal brew cu creates always the config file.

if there is no config then do not create one.

only if there is an command brew cu --create-config I would expect that a config will be created.

my suggestion: remove this part and create an new command - or leave the creation of this to the user.


default_options = create_default_options
if File.exist?(config_filename)
odebug "Loading configuration from config file"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add config file name

options = {}
File.open(config_filename) do |f|
options = (YAML.safe_load f.read).to_h
odebug "Configuration loaded", options
end
OpenStruct.new options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • at this point i would filter the keys to only allow keys which are known

  • maybe show an error message when keys are unknown - for example if there is a typo

else
# something went wrong while reading config file
odebug "Config file wasn't created, setting default config"
default_options
end
end

def self.create_default_options
default_values = default_config_hash
default_options = OpenStruct.new
default_options.all = default_values["all"]
default_options.force = default_values["force"]
default_options.cleanup = default_values["cleanup"]
default_options.force_yes = default_values["force_yes"]
default_options.no_brew_update = default_values["no_brew_update"]
default_options.quiet = default_values["quiet"]
default_options.verbose = default_values["verbose"]
default_options.interactive = default_values["interactive"]
default_options.debug = default_values["debug"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the value debug is not in default_values

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should a loop over the hash and a default_options[option_name] = default_values[option_name] a little shorter and include all options from hash?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see there you also can write when you don't need to exclude some keys:

default_options = OpenStruct.new(default_values)

default_options
end

def self.create_default_config_file(config_filename)
system "touch #{config_filename}"
File.open(config_filename, "w") do |f|
f.write default_config_hash.to_yaml
end
rescue => e
odebug "RESCUE: File couldn't be created", e
system "rm -f #{config_filename}"
end

def self.default_config_hash
{
"all" => false,
"force" => false,
"cleanup" => false,
"force_yes" => false,
"no_brew_update" => false,
"quiet" => false,
"verbose" => false,
"interactive" => false,
}
end
end
2 changes: 1 addition & 1 deletion lib/extend/cask.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# For backward-compatibility
# See https://github.com/buo/homebrew-cask-upgrade/issues/97
CASKROOM = Cask.methods.include?(:caskroom) ? Cask.caskroom : Cask::Caskroom.path
CASKROOM = (Cask.methods.include?(:caskroom) ? Cask.caskroom : Cask::Caskroom.path).freeze

module Cask
def self.installed_apps
Expand Down