@@ -6,27 +6,19 @@ class << self
6
6
end
7
7
8
8
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
26
10
27
11
parser = OptionParser . new do |opts |
28
12
opts . banner = "Usage: brew cu [CASK] [options]"
29
13
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
+
30
22
# Prevent using short -p syntax for pinning
31
23
opts . on ( "-p" ) do
32
24
onoe "invalid option -p, did you mean --pin?"
@@ -126,4 +118,81 @@ def self.validate_options(options)
126
118
exit 1
127
119
end
128
120
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
129
198
end
0 commit comments