-
Notifications
You must be signed in to change notification settings - Fork 91
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
base: master
Are you sure you want to change the base?
Changes from all commits
264df84
64c08d3
97027af
83a4f9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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?" | ||
|
@@ -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 | ||
|
||
default_options = create_default_options | ||
if File.exist?(config_filename) | ||
odebug "Loading configuration from config file" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should a loop over the hash and a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.