Skip to content

Commit 9f30914

Browse files
committed
Using persistent config file for execution
- storing default execution configuration in user home - adding option to ignore changes made to user config - updating README - fixing rubocop offenses
1 parent 0a37ff2 commit 9f30914

File tree

9 files changed

+135
-46
lines changed

9 files changed

+135
-46
lines changed

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ Options:
7474
-q, --quiet Do not show information about installed apps or current options.
7575
-v, --verbose Make output more verbose.
7676
--no-quarantine Pass --no-quarantine option to `brew cask install`.
77-
-i, --interactive Running update in interactive mode
77+
-i, --interactive Running update in interactive mode
78+
--ignore-config Ignores any changes made to a default config
7879
```
7980

8081
Display usage instructions:
@@ -94,3 +95,22 @@ For every cask it is then possible to use following options:
9495

9596
Pinned apps will not be updated by `brew cu` until they are unpinned.
9697
NB: version pinning in `brew cu` will not prevent `brew cask upgrade` from updating pinned apps.
98+
99+
### Default config
100+
101+
In order to have less verbose execution, it is possible to alter default config under which the upgrade gets executed.
102+
That could be done by changing `~/.brew-cu` config file.
103+
This is the default config setting the default options:
104+
```yaml
105+
---
106+
all: false
107+
force: false
108+
cleanup: false
109+
force_yes: false
110+
no_brew_update: false
111+
quiet: false
112+
verbose: false
113+
interactive: false
114+
```
115+
116+
If any changes to this config were made, those changes can be ignored by passing `--ignore-config` option.

brew-cask-upgrade.gemspec

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
Gem::Specification.new do |s|
2-
s.name = "brew-cask-upgrade"
3-
s.version = "2.0.1"
4-
s.summary = "A command line tool for Homebrew Cask"
2+
s.name = "brew-cask-upgrade"
3+
s.version = "2.0.1"
4+
s.summary = "A command line tool for Homebrew Cask"
55
s.description = "A command line tool for upgrading every outdated app installed by Homebrew Cask"
6-
s.authors = ["buo"]
7-
s.email = "[email protected]"
8-
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
9-
s.homepage = "https://github.com/buo/homebrew-cask-upgrade"
10-
s.license = "MIT"
6+
s.authors = ["buo"]
7+
s.email = "[email protected]"
8+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
9+
s.homepage = "https://github.com/buo/homebrew-cask-upgrade"
10+
s.license = "MIT"
11+
s.required_ruby_version = 2.6
1112

12-
s.bindir = "bin"
13+
s.bindir = "bin"
1314
s.executables = %w[brew-cask-upgrade]
1415
end

cmd/brew-cu.rb

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#: If `--no-quarantine` is passed, that option will be added to the install
4949
#: command (see `man brew-cask` for reference)
5050
#:
51+
#: If `--ignore-config` is passed, changes to user config file fill be ignored.
52+
#:
5153
#:`INTERACTIVE MODE`:
5254
#: After listing casks to upgrade you want those casks to be upgraded.
5355
#: By using the option `i` you will step into an interactive mode.

lib/bcu/command/pin_list.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def list_pinned(_args, _options)
2121
def add_cask(cask_name, casks)
2222
casks.push Cask.load_cask(cask_name)
2323
rescue Cask::CaskUnavailableError
24-
Bcu::Pin::Remove.remove_pin cask_name
24+
Bcu::Pin::Remove.remove_pin cask:cask_name
2525
end
2626
end
2727
end

lib/bcu/command/pin_remove.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ def process(args, options)
88
# TODO: If we used deprecated --pin option, the value is not any more in the args
99
pin = options.unpin if pin.nil?
1010

11-
remove_pin pin
11+
remove_pin cask:pin
1212
end
1313

1414
private
1515

16-
def remove_pin(cask, quiet = false)
16+
def remove_pin(cask:, quiet: false)
1717
unless Pin.pinned.include? cask
1818
puts "Not pinned: #{Tty.green}#{cask}#{Tty.reset}" unless quiet
1919
return

lib/bcu/command/upgrade.rb

+11-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def process(_args, options)
3232
printf "Do you want to upgrade %<count>d app%<s>s or enter [i]nteractive mode [y/i/N]? ",
3333
count: outdated.length,
3434
s: (outdated.length > 1) ? "s" : ""
35-
input = STDIN.gets.strip
35+
input = $stdin.gets.strip
3636

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

6565
if input.casecmp("p").zero?
6666
cmd = Bcu::Pin::Add.new
@@ -74,19 +74,17 @@ def upgrade(app, options, state_info)
7474

7575
ohai "Upgrading #{app[:token]} to #{app[:version]}"
7676
installation_successful = install app, options
77-
78-
if installation_successful
79-
installation_cleanup app, options
80-
end
77+
installation_cleanup app, options if installation_successful
8178
end
8279

8380
def install(app, options)
8481
verbose_flag = options.verbose ? "--verbose" : ""
82+
debug_flag = options.debug ? "--debug" : ""
8583

8684
begin
8785
# Force to install the latest version.
88-
cmd = "brew reinstall #{options.install_options} #{app[:token]} --force " + verbose_flag
89-
success = system "#{cmd}"
86+
cmd = "brew reinstall #{options.install_options} #{app[:token]} --force #{verbose_flag} #{debug_flag}"
87+
success = system cmd
9088
rescue
9189
success = false
9290
end
@@ -116,8 +114,7 @@ def find_outdated_apps(installed, options)
116114
end
117115

118116
if installed.empty?
119-
print_install_empty_message options.casks
120-
exit 1
117+
odie empty_message(options.casks)
121118
end
122119
end
123120

@@ -147,15 +144,15 @@ def find_outdated_apps(installed, options)
147144
[outdated, state_info]
148145
end
149146

150-
def print_install_empty_message(cask_searched)
147+
def empty_message(cask_searched)
151148
if cask_searched.length == 1
152149
if cask_searched[0].end_with? "*"
153-
onoe "#{Tty.red}No Cask matching \"#{cask_searched[0]}\" is installed.#{Tty.reset}"
150+
"#{Tty.red}No Cask matching \"#{cask_searched[0]}\" is installed.#{Tty.reset}"
154151
else
155-
onoe "#{Tty.red}Cask \"#{cask_searched[0]}\" is not installed.#{Tty.reset}"
152+
"#{Tty.red}Cask \"#{cask_searched[0]}\" is not installed.#{Tty.reset}"
156153
end
157154
else
158-
onoe "#{Tty.red}No casks matching your arguments found.#{Tty.reset}"
155+
"#{Tty.red}No casks matching your arguments found.#{Tty.reset}"
159156
end
160157
end
161158
end

lib/bcu/module/pin.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Bcu
22
module Pin
3-
PINS_FILE = File.expand_path(File.dirname(__FILE__) + "/../../../pinned")
3+
PINS_FILE = File.expand_path(File.dirname(__FILE__) + "/../../../pinned").freeze
44

55
module_function
66

lib/bcu/options.rb

+86-17
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,19 @@ class << self
66
end
77

88
def self.parse!(args)
9-
options_struct = Struct.new(:all, :force, :casks, :cleanup, :force_yes, :no_brew_update, :quiet, :verbose,
10-
:install_options, :list_pinned, :pin, :unpin, :interactive, :command)
11-
options = options_struct.new
12-
options.all = false
13-
options.force = false
14-
options.casks = nil
15-
options.cleanup = false
16-
options.force_yes = false
17-
options.no_brew_update = false
18-
options.quiet = false
19-
options.verbose = false
20-
options.install_options = ""
21-
options.list_pinned = false
22-
options.pin = nil
23-
options.unpin = nil
24-
options.interactive = false
25-
options.command = "run"
9+
options = build_config
2610

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

14+
opts.on("--ignore-config") do
15+
options = build_config false
16+
end
17+
18+
opts.on("--debug") do
19+
options.debug = true
20+
end
21+
3022
# Prevent using short -p syntax for pinning
3123
opts.on("-p") do
3224
onoe "invalid option -p, did you mean --pin?"
@@ -126,4 +118,81 @@ def self.validate_options(options)
126118
exit 1
127119
end
128120
end
121+
122+
def self.build_config(use_config_file = true)
123+
if use_config_file
124+
options = load_default_options
125+
else
126+
options = create_default_options
127+
end
128+
options.casks = nil
129+
options.install_options = ""
130+
options.list_pinned = false
131+
options.pin = nil
132+
options.unpin = nil
133+
options.command = "run"
134+
135+
options
136+
end
137+
138+
def self.load_default_options
139+
config_filename = "#{ENV["HOME"]}/.brew-cu"
140+
unless File.exist?(config_filename)
141+
odebug "Config file doesn't exist, creating"
142+
create_default_config_file config_filename
143+
end
144+
145+
default_options = create_default_options
146+
if File.exist?(config_filename)
147+
odebug "Loading configuration from config file"
148+
handle = File.open(config_filename)
149+
options = YAML::load handle.read
150+
odebug "Configuration loaded", options
151+
OpenStruct.new(options.to_h)
152+
else
153+
# something went wrong while reading config file
154+
odebug "Config file wasn't created, setting default config"
155+
default_options
156+
end
157+
end
158+
159+
def self.create_default_options
160+
default_values = default_config_hash
161+
default_options = OpenStruct.new
162+
default_options.all = default_values["all"]
163+
default_options.force = default_values["force"]
164+
default_options.cleanup = default_values["cleanup"]
165+
default_options.force_yes = default_values["force_yes"]
166+
default_options.no_brew_update = default_values["no_brew_update"]
167+
default_options.quiet = default_values["quiet"]
168+
default_options.verbose = default_values["verbose"]
169+
default_options.interactive = default_values["interactive"]
170+
default_options.debug = default_values["debug"]
171+
default_options
172+
end
173+
174+
def self.create_default_config_file(config_filename)
175+
begin
176+
system "touch #{config_filename}"
177+
handle = File.open(config_filename, "w")
178+
handle.write default_config_hash.to_yaml
179+
handle.close
180+
rescue Exception => e
181+
odebug "RESCUE: File couldn't be created", e
182+
system "rm -f #{config_filename}"
183+
end
184+
end
185+
186+
def self.default_config_hash
187+
{
188+
"all" => false,
189+
"force" => false,
190+
"cleanup" => false,
191+
"force_yes" => false,
192+
"no_brew_update" => false,
193+
"quiet" => false,
194+
"verbose" => false,
195+
"interactive" => false,
196+
}
197+
end
129198
end

lib/extend/cask.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# For backward-compatibility
22
# See https://github.com/buo/homebrew-cask-upgrade/issues/97
3-
CASKROOM = Cask.methods.include?(:caskroom) ? Cask.caskroom : Cask::Caskroom.path
3+
CASKROOM = Cask.methods.include?(:caskroom) ? Cask.caskroom.freeze : Cask::Caskroom.path.freeze
44

55
module Cask
66
def self.installed_apps

0 commit comments

Comments
 (0)