configliere 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.textile CHANGED
@@ -1,5 +1,9 @@
1
1
  Note that while the version # is still in the 0.0.x range, the interface may change arbitrarily while we figure out the best simplest convenientest powerfullest way to arrange things
2
2
 
3
+ h2. Version 0.0.4 2010-01-16
4
+
5
+ * Cured a variety of issues noted by "@monad":http://github.com/monad -- thanks for the feedback!
6
+
3
7
  h2. Version 0.0.3 2010-01-15
4
8
 
5
9
  * @Settings.param@ now only works for params that have been @#define@'d :
@@ -13,7 +17,7 @@ h2. Version 0.0.3 2010-01-15
13
17
  </pre>
14
18
 
15
19
  * Note that you *must* use symbols as keys (except for dotted notation for deep keys). See the README.
16
- * You must now call @#use_environment@ as @Settings.use_environment :param => 'ENV_VAR'@. (The param is used as the key everywhere else, made this consistent).
20
+ * You must now define environment variables using @Settings.env_vars :param => 'ENV_VAR'@. The order was switched to consistently use param as the key. Also, @environment@ was renamed to @env_var@ to avoid confusion with the "development/test/production" convention from rails and friends.
17
21
  * die takes an error code as option
18
22
  * Added example scripts for encrypted and config_block scripts
19
23
  * The directory path to a config_file will now be created automatically
data/README.textile CHANGED
@@ -23,19 +23,19 @@ Here's a simple example, using params from a config file and the command line. A
23
23
  <pre>
24
24
  #/usr/bin/env ruby
25
25
  require 'configliere'
26
-
27
- Settings.use(:commandline, :config_file,
28
- :dish => 'spoon', :cow => 'moon')
26
+ Settings.use :commandline, :config_file
27
+
28
+ Settings :dish => 'spoon', :cow => 'moon'
29
29
  Settings.read 'my_script.yaml' # reads ~/.configliere/my_script.yaml
30
30
  Settings.resolve!
31
31
 
32
32
  p Settings</pre>
33
33
 
34
- Output:
34
+ Output, when run with commandline parameters as shown:
35
35
 
36
36
  <pre>
37
- ./simple_script.rb --sprats.wife=fat --spider=drainspout
38
- {:spider=>"drainspout", :cat=>"hat", :sprats=>{:wife=>"fat", :jack=>"lean"}, :cow=>"moon"} </pre>
37
+ ./simple_script.rb --sprats.wife=fat --spider=waterspout
38
+ {:spider=>"waterspout", :cat=>"hat", :sprats=>{:wife=>"fat", :jack=>"lean"}, :cow=>"moon"} </pre>
39
39
 
40
40
  For an extensive usage in production, see the "wukong gem.":http://github.com/mrflip/wukong
41
41
 
@@ -48,7 +48,7 @@ h3. Design goals:
48
48
  ** Simple config files
49
49
  ** Environment variables
50
50
  ** Commandline options
51
- ** Ruby block called when all other options are in place
51
+ ** Ruby block called when all other options are in place
52
52
  * *Code of Silence*. Most commandline parsers force you to pre-define all your parameters in a centralized and wordy syntax. In configliere, you pre-define nothing -- commandline parameters map directly to values in the Configliere hash.
53
53
  * *Can hide your assets*. Rather than storing passwords and API keys in plain sight, configliere has a protection racket that can obscure values when stored to disk.
54
54
 
@@ -56,7 +56,7 @@ fuhgeddaboudit.
56
56
 
57
57
  h2. Settings structure
58
58
 
59
- Configliere settings are just a plain old normal hash.
59
+ Configliere settings are just a plain old normal hash.
60
60
 
61
61
  You can define static defaults in your module
62
62
 
@@ -74,11 +74,11 @@ You can define static defaults in your module
74
74
  :password => '',
75
75
  })
76
76
  </pre>
77
-
77
+
78
78
  Retrieve them as:
79
79
 
80
80
  <pre>
81
- # hash keys
81
+ # hash keys
82
82
  Settings[:dest_time] #=> '1955-11-05'
83
83
  # deep keys
84
84
  Settings[:delorean][:power_source] #=> 'plutonium'
@@ -89,12 +89,13 @@ Retrieve them as:
89
89
  Settings['delorean.missing'] #=> nil
90
90
  Settings['delorean.missing.fail'] #=> nil
91
91
  # method-like (no deep keys tho)
92
- Settings.dest_time #=> '1955-11-05'
92
+ Settings.dest_time #=> '1955-11-05'
93
93
  </pre>
94
94
 
95
95
  h3. Shortcut syntax for deep keys
96
96
 
97
- You can use a 'dotted key' like 'delorean.power_source' as simple notation for a deep key: Settings['delorean.power_source'] is equivalent to Settings[:delorean][:power_source]. You can use a dotted key in any simple reference:
97
+ You can use a 'dotted key' like 'delorean.power_source' as simple notation for a deep key: @Settings['delorean.power_source']@ is equivalent to @Settings[:delorean][:power_source]@. You can use a dotted key in any simple reference:
98
+
98
99
  <pre>
99
100
  Settings['delorean.power_source'] = "Mr. Fusion"
100
101
  Settings[:delorean][:power_source]
@@ -102,10 +103,11 @@ You can use a 'dotted key' like 'delorean.power_source' as simple notation for a
102
103
  Settings.delete('delorean.power_source')
103
104
  #=> "Mr. Fusion"
104
105
  Settings
105
- #=> { :delorean => {} }
106
+ #=> { :delorean => {} }
106
107
  </pre>
107
108
 
108
109
  Intermediate keys "auto-vivify" (automatically create any intervening hashes):
110
+
109
111
  <pre>
110
112
  Settings['one.two.three'] = "To tha Fo'"
111
113
  # Settings is { :one => { :two => { :three => "To tha Fo'" } }, :delorean => { :power_source => "Mr. Fusion" }
@@ -125,7 +127,7 @@ h3. Only basic functionality loaded by default
125
127
  Configliere doesn't load any other functionality by default -- you may not want to load config files, or environment variable handling, and so forth. You can require each file directly, or call @Configliere.use@ with a list of mixins (:all to load all functionality).
126
128
 
127
129
  <pre>
128
- Configliere.use :config_file, :define # Load config files and pre-define
130
+ Configliere.use :config_file, :define # Load config files and pre-define
129
131
  Configliere.use :all # all of them
130
132
  </pre>
131
133
 
@@ -151,7 +153,7 @@ You can instead supply a path to a config file. If a bare filename (no '/') is
151
153
  </pre>
152
154
 
153
155
  When you read directly from a file you should leave off the top-level settings group:
154
-
156
+
155
157
  <pre>
156
158
  # Settings for version II.
157
159
  :dest_time: 2015-11-05
@@ -167,32 +169,41 @@ Save defaults by inserting a line like:
167
169
  Settings.save('/etc/time_machine.yaml') # overwrites /etc/time_machine.yaml
168
170
  Settings.save('time_machine.yaml') # overwrites ~/.configliere/time_machine.yaml
169
171
  </pre>
170
-
172
+
173
+ You're free to use as many config files as you like. Loading a config file sets values immediately, so later-loaded files win out over earlier-loaded ones:
174
+
175
+ <pre>
176
+ Settings.read('time_machine_global.yaml')
177
+ Settings.read('time_machine_site.yaml')
178
+ </pre>
179
+
171
180
  h2. Environment Variables
172
181
 
173
182
  <pre>
174
- Settings.use_environment 'DEST_TIME', :password => 'TM_PASS', 'delorean.power_source' => 'POWER_SOURCE'
183
+ Settings.env_vars 'DEST_TIME', :password => 'TM_PASS', 'delorean.power_source' => 'POWER_SOURCE'
175
184
  </pre>
176
185
 
177
- As usual, dotted keys set the corresponeding nested key (@'delorean.power_source'@ sets @Settings[:delorean][:power_source]@). You can alternatively set up environment variables with @define 'delorean.power_source', :environment => 'POWER_SOURCE'@ - see below.
186
+ As usual, dotted keys set the corresponeding nested key (@'delorean.power_source'@ sets @Settings[:delorean][:power_source]@). As shown below, you may also use #define to set up environment variables: @define 'delorean.power_source', :environment => 'POWER_SOURCE'@.
178
187
 
179
- **NOTE**: The interface to #use_environment has changed since v0.2. You must now call it as @Settings.use_environment :param => 'ENV_VAR'@, (since everywhere else the param is used as the key).
188
+ Environment variables are demonstrated in "examples/simple_script.rb":http://github.com/mrflip/configliere/tree/master/examples/simple_script.rb and "examples/env_var_script.rb":http://github.com/mrflip/configliere/tree/master/examples/env_var_script.rb
189
+
190
+ **NOTE**: The interface to #env_vars has changed since v0.2, see "CHANGELOG.textile":CHANGELOG.textile
180
191
 
181
192
  h2. Command-line parameters
182
193
 
183
194
  <pre>
184
195
  # Head back
185
- time_machine --delorean-power_source='1.21 gigawatt lightning strike' --dest_time=1985-11-05
196
+ time_machine --delorean-power_source='1.21 gigawatt lightning strike' --dest_time=1985-11-05
186
197
  # (in the time_machine script:)
187
198
  Settings.use :commandline
188
199
  </pre>
189
200
 
190
201
  Interpretation of command-line parameters:
191
202
  * *name-val params*: @--param=val@ sets @Configliere[:param]@ to val.
192
- * *boolean params*: @--param@ sets @Configliere[:param]@ to be true. @--param=""@ sets @Configliere[:param]@ to be nil.
203
+ * *boolean params*: @--param@ sets @Configliere[:param]@ to be true. @--param=""@ sets @Configliere[:param]@ to be nil.
193
204
  * *scoped params*: @--group-sub_group-param=val@ sets @Configliere[:group][:subgroup][:param]@ to val (and similarly for boolean parameters).
194
205
  ** A dash or dot within a parameter name scopes that parameter: @--group.sub_group.param=val@ and @--group-sub_group-param=val@ do the same thing. A _ within a parameter name is left as part of the segment.
195
- ** Only @[\w\.\-]+@ are accepted in parameter names.
206
+ ** Only @[\w\.\-]+@ are accepted in parameter names.
196
207
  * *Settings.rest*: anything else is stored, in order, in @Settings.rest@.
197
208
  * *stop marker*: a @--@ alone stops parameter processing and tosses all remaining params (not including the @--@) into Settings.rest.
198
209
 
@@ -204,6 +215,9 @@ Here are some things you don't get:
204
215
 
205
216
  If you want more, you might like the Trollop gem. If you enjoy wordy nightmares, use "getoptlog":http://linuxdevcenter.com/pub/a/linux/2003/09/18/ruby_csv.html?page=2 from the ruby standard library.
206
217
 
218
+ Commandline parameters are demonstrated in "examples/simple_script.rb":http://github.com/mrflip/configliere/tree/master/examples/simple_script.rb and
219
+ "examples/env_var_script.rb":http://github.com/mrflip/configliere/tree/master/examples/env_var_script.rb
220
+
207
221
  h2. Fancy Parameters
208
222
 
209
223
  You don't have to pre-define parameters, but you can:
@@ -211,15 +225,17 @@ You don't have to pre-define parameters, but you can:
211
225
  <pre>
212
226
  Settings.use :define
213
227
  Settings.define :dest_time, :type => Date, :description => 'Arrival time'
214
- Settings.define 'delorean.power_source', :environment => 'POWER_SOURCE', :description => 'Delorean subsytem supplying power to the Flux Capacitor.'
228
+ Settings.define 'delorean.power_source', :env_var => 'POWER_SOURCE', :description => 'Delorean subsytem supplying power to the Flux Capacitor.'
215
229
  Settings.define :password, :required => true, :encrypted => true
216
230
  </pre>
217
231
 
218
- * *:type*: converts params to a desired form.
232
+ * *:type*: converts params to a desired form.
219
233
  * *:description*: documents a param.
220
- * *:required*: marks params required.
234
+ * *:required*: marks params required.
221
235
  * *:encrypted*: marks params to be obscured when saved to disk. See [#Encrypted Parameters] below for caveats.
222
- * *:environment*: take param from given environment variable if set.
236
+ * *:env_var*: take param from given environment variable if set.
237
+
238
+ Defined parameters are demonstrated in most of the "example scripts":http://github.com/mrflip/configliere/tree/master/examples
223
239
 
224
240
  h3. Type Conversion
225
241
 
@@ -261,6 +277,8 @@ bq. There are two kinds of cryptography in this world: cryptography that will s
261
277
 
262
278
  Configliere provides the latter. Anyone with access to the script, its config files and the config file passphrase can recover the plaintext password. Still, there's a difference between immediate access and having to find a paperclip and jimmy open your annoying older brother's stupid journal.
263
279
 
280
+ Encrypted parameters are demonstrated in "examples/encrypted_script.rb":http://github.com/mrflip/configliere/tree/master/examples/encrypted_script.rb
281
+
264
282
  h2. Ruby Block
265
283
 
266
284
  <pre>
@@ -277,6 +295,8 @@ h2. Ruby Block
277
295
 
278
296
  Configliere 'finally' blocks are invoked when you call @#resolve!@. They're guaranteed to be called at the end of the resolve chain, and before the validate chain.
279
297
 
298
+ Config blocks are demonstrated in "examples/config_block.rb":http://github.com/mrflip/configliere/tree/master/examples/config_block.rb
299
+
280
300
  h2. Independent Settings
281
301
 
282
302
  All of the above examples use the global variable @Settings@, defined in configliere.rb. You're free to define your own settings universe though:
@@ -298,7 +318,7 @@ Values in here don't overlap with the Settings object or any other settings univ
298
318
  h2. Project info
299
319
 
300
320
  h3. Note on Patches/Pull Requests
301
-
321
+
302
322
  * Fork the project.
303
323
  * Make your feature addition or bug fix.
304
324
  * Add tests for it. This is important so I don't break it in a
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/configliere.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{configliere}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mrflip"]
12
- s.date = %q{2010-01-15}
12
+ s.date = %q{2010-01-18}
13
13
  s.default_executable = %q{configliere}
14
14
  s.description = %q{ You've got a script. It's got some settings. Some settings are for this module, some are for that module. Most of them don't change. Except on your laptop, where the paths are different. Or when you're in production mode. Or when you're testing from the command line.
15
15
 
@@ -33,11 +33,9 @@ Configliere manage settings from many sources: static constants, simple config f
33
33
  "VERSION",
34
34
  "bin/configliere",
35
35
  "configliere.gemspec",
36
- "examples/commandline_script.rb",
37
- "examples/commandline_script.yaml",
38
- "examples/config_block.rb",
36
+ "examples/config_block_script.rb",
39
37
  "examples/encrypted_script.rb",
40
- "examples/foo.yaml",
38
+ "examples/env_var_script.rb",
41
39
  "examples/simple_script.rb",
42
40
  "examples/simple_script.yaml",
43
41
  "lib/configliere.rb",
@@ -51,7 +49,7 @@ Configliere manage settings from many sources: static constants, simple config f
51
49
  "lib/configliere/crypter.rb",
52
50
  "lib/configliere/define.rb",
53
51
  "lib/configliere/encrypted.rb",
54
- "lib/configliere/environment.rb",
52
+ "lib/configliere/env_var.rb",
55
53
  "lib/configliere/param.rb",
56
54
  "spec/configliere/commandline_spec.rb",
57
55
  "spec/configliere/config_block_spec.rb",
@@ -61,7 +59,7 @@ Configliere manage settings from many sources: static constants, simple config f
61
59
  "spec/configliere/crypter_spec.rb",
62
60
  "spec/configliere/define_spec.rb",
63
61
  "spec/configliere/encrypted_spec.rb",
64
- "spec/configliere/environment_spec.rb",
62
+ "spec/configliere/env_var_spec.rb",
65
63
  "spec/configliere/param_spec.rb",
66
64
  "spec/configliere_spec.rb",
67
65
  "spec/spec.opts",
@@ -81,13 +79,13 @@ Configliere manage settings from many sources: static constants, simple config f
81
79
  "spec/configliere/crypter_spec.rb",
82
80
  "spec/configliere/define_spec.rb",
83
81
  "spec/configliere/encrypted_spec.rb",
84
- "spec/configliere/environment_spec.rb",
82
+ "spec/configliere/env_var_spec.rb",
85
83
  "spec/configliere/param_spec.rb",
86
84
  "spec/configliere_spec.rb",
87
85
  "spec/spec_helper.rb",
88
- "examples/commandline_script.rb",
89
- "examples/config_block.rb",
86
+ "examples/config_block_script.rb",
90
87
  "examples/encrypted_script.rb",
88
+ "examples/env_var_script.rb",
91
89
  "examples/simple_script.rb"
92
90
  ]
93
91
 
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.dirname(__FILE__)+'/../lib'
3
+ require 'configliere'
4
+ Settings.use :config_block
5
+
6
+ Settings.define :passenger
7
+ Settings.define :dest_time, :type => DateTime
8
+ Settings :passenger => 'einstein', :dest_time => '1955-11-05'
9
+
10
+ Settings.finally do |c|
11
+ p [self, 'finally', c[:passenger], c.passenger]
12
+ # Einstein the dog should only be sent one minute into the future.
13
+ c.dest_time = (Time.now + 60) if c.passenger == 'einstein'
14
+ end
15
+ Settings.resolve!
16
+ p Settings
@@ -1,17 +1,52 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'rubygems'
2
3
  require 'configliere'
3
-
4
4
  DUMP_FILENAME = '/tmp/encrypted_script.yml'
5
- Settings.use :config_file, :define, :encrypt_pass => 'password1'
6
- Settings.define :password, :encrypted => true
7
5
 
8
- Settings :password => 'plaintext'
6
+ #
7
+ # Example usage:
8
+ # export ENCRYPT_PASS=password1
9
+ # ./examples/encrypted_script.rb
10
+ #
11
+
12
+ puts %Q{Many times, scripts need to save values you\'d rather not leave as
13
+ plaintext: API keys, database passwords, etc. Instead of leaving them in plain
14
+ sight, you may wish to obscure their value on disk and use a secondary password
15
+ to unlock it.
16
+
17
+ View source for this script to see commands you might use (from the irb console
18
+ or in a standalone script) to store the obscured values for later decryption.}
19
+
20
+ Settings.use :config_file, :define, :encrypted, :encrypt_pass => 'password1'
21
+ Settings.define :password, :encrypted => true, :default => 'plaintext'
9
22
  Settings.resolve!
10
- p ["saved version will have encrypted password (see #{DUMP_FILENAME}).", Settings.send(:export)]
11
- p ['live version still has password in plaintext', Settings]
23
+
24
+ puts "\nIn-memory version still has password in plaintext..."
25
+ puts " #{Settings.inspect}"
26
+ puts "But the saved version will have encrypted password (see #{DUMP_FILENAME}):"
27
+ puts " #{Settings.send(:export).inspect}"
12
28
  Settings.save!(DUMP_FILENAME)
13
29
 
14
- Settings[:password] = 'before read'
30
+ puts "If we now load the saved file, the parameter's value is decrypted on resolve:"
31
+ Settings[:password] = 'nothing up my sleeve'
15
32
  Settings.read('/tmp/encrypted_script.yml')
16
33
  Settings.resolve!
17
- p ["value is decrypted on resolve.", Settings]
34
+ puts " #{Settings.inspect}"
35
+
36
+ # unset encrypt_pass
37
+ Settings.encrypt_pass = nil
38
+
39
+ puts %Q{\nOf course, in your script you\'ll have to supply the decryption
40
+ password. The best thing is to use an environment variable -- a user can spy on
41
+ your commandline parameters using "ps" or "top". The following will fail unless
42
+ you supply the correct password ("password1") in the ENCRYPT_PASS environment
43
+ variable:\n\n}
44
+
45
+ Settings.define :encrypt_pass, :env_var => 'ENCRYPT_PASS'
46
+ Settings.read('/tmp/encrypted_script.yml')
47
+ begin
48
+ Settings.resolve!
49
+ puts " #{Settings.inspect}"
50
+ rescue RuntimeError => e
51
+ warn " #{e}"
52
+ end
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.dirname(__FILE__)+'/../lib'
3
+ require 'configliere'
4
+
5
+ Settings.use :define, :env_var, :commandline
6
+ Settings.define :underpants, :env_var => 'UNDERPANTS', :default => "boxers"
7
+ Settings.resolve!
8
+
9
+ puts %Q{
10
+ Configliere can take parameter values from its defaults, from the commandline, or from the environment.
11
+ Compare:
12
+
13
+ ./env_var_script.rb # value from default
14
+ ./env_var_script.rb --underpants=briefs # value from commandline
15
+ UNDERPANTS="commando" ./env_var_script.rb # value from environment variable
16
+ UNDERPANTS="commando" ./env_var_script.rb --underpants=briefs # commandline wins
17
+
18
+ }
19
+
20
+ puts %Q{Using
21
+ * the default setting of: #{Settings.param_definitions[:underpants][:default].inspect}
22
+ * the environment variable: #{ENV['UNDERPANTS'].inspect}
23
+ * the commandline setting: #{ARGV.grep(/^--underpants/).inspect}
24
+ your configliere advises that
25
+ #{Settings.inspect}
26
+ }
@@ -2,20 +2,26 @@
2
2
  $: << File.dirname(__FILE__)+'/../lib'
3
3
  require 'configliere'
4
4
 
5
- # Intro text
6
- puts %Q{
7
- This is a demo of the Configliere interface. It takse settings
8
- Try running it as
9
- ./examples/simple_script.rb --sprats.wife=fat --spider=drainspout
10
- with those args, we
11
- expect: {:spider=>"drainspout", :cat=>"hat", :sprats=>{:wife=>"fat", :jack=>"lean"}, :cow=>"moon"}
12
- }
5
+ puts "This is a demo of Configliere in a simple script."
6
+ Settings.use :commandline, :config_file
13
7
 
14
- Settings.use(:commandline, :config_file,
15
- :cat => 'bag', :cow => 'moon')
16
- Settings.read File.dirname(__FILE__)+'/simple_script.yaml'
8
+ puts "You can set default values:"
9
+ Settings.defaults :cat => 'bag', :cow => 'moon'
10
+ puts " #{Settings.inspect}"
11
+
12
+ config_filename = File.dirname(__FILE__)+'/simple_script.yaml'
13
+ puts "\nYou can load values from a file -- in this case, #{config_filename} -- which overrides the defaults:"
14
+ Settings.read config_filename
15
+ puts " #{Settings.inspect}"
16
+
17
+ puts %Q{\nTry running the script with commandline parameters, for example
18
+ #{$0} --sprats.wife=fat --spider=waterspout
19
+ In this case, you used
20
+ #{$0} #{ARGV.join(" ")}
21
+ and so the final parameter values are}
17
22
  Settings.resolve!
23
+ puts " #{Settings.inspect}"
18
24
 
19
- # Print results
20
- print ' actual: '
21
- p Settings
25
+ saved_filename = '/tmp/simple_script_saved.yaml'
26
+ puts %Q{\nYou can save the defaults out to a config file. These settings have been written to #{saved_filename}}
27
+ Settings.save!(saved_filename)
data/lib/configliere.rb CHANGED
@@ -14,7 +14,7 @@ module Configliere
14
14
  Configliere::Param.new *args, &block
15
15
  end
16
16
 
17
- ALL_MIXINS = [:define, :encrypted, :environment, :config_file, :commandline, :config_block]
17
+ ALL_MIXINS = [:define, :encrypted, :env_var, :config_file, :commandline, :config_block]
18
18
  def self.use *mixins
19
19
  mixins = ALL_MIXINS if mixins.include?(:all)
20
20
  mixins.each do |mixin|
@@ -18,7 +18,7 @@ module Configliere
18
18
  def resolve!
19
19
  process_argv!
20
20
  dump_help_if_requested
21
- begin ; super() ; rescue NoMethodError ; nil ; end
21
+ super()
22
22
  end
23
23
 
24
24
  #
@@ -88,7 +88,7 @@ module Configliere
88
88
  help_str = [ usage ]
89
89
  help_str += [ "\nParams:", descriptions.sort_by{|p,d| p.to_s }.map{|param, desc| " --%-25s %s"%[param.to_s+':', desc]}.join("\n"), ] if respond_to?(:descriptions)
90
90
  # help_str += ["\nCommands", commands.map{|cmd, desc| " %-20s %s"%[cmd.to_s+':', desc]}.join("\n")] if respond_to?(:commands)
91
- help_str += [ "\nEnvironment Variables can be used to set:", params_from_environment.map{|param, env| " %-27s %s"%[env.to_s+':', param]}.join("\n"), ] if respond_to?(:params_from_environment)
91
+ help_str += [ "\nEnvironment Variables can be used to set:", params_from_env_vars.map{|param, env| " %-27s %s"%[env.to_s+':', param]}.join("\n"), ] if respond_to?(:params_from_env_vars)
92
92
  help_str.join("\n")
93
93
  end
94
94
 
@@ -32,7 +32,7 @@ module Configliere
32
32
  # The resolve! for config_block is made to run last of all in the +resolve!+
33
33
  # chain, and runs each +finally+ block in the order it was defined.
34
34
  def resolve!
35
- begin ; super() ; rescue NoMethodError ; nil ; end
35
+ super
36
36
  resolve_finally_blocks!
37
37
  self
38
38
  end
@@ -13,10 +13,10 @@ module Configliere
13
13
  #
14
14
  def define param, definitions={}
15
15
  self.param_definitions[param].merge! definitions
16
- self.use(:environment) if definitions.include?(:encrypted)
17
- self.use(:encrypted) if definitions.include?(:encrypted)
16
+ self.use(:env_var) if definitions.include?(:env_var)
17
+ self.use(:encrypted) if definitions.include?(:encrypted)
18
18
  self[param] = definitions[:default] if definitions.include?(:default)
19
- self.environment_variables definitions[:environment], param if definitions.include?(:environment)
19
+ self.env_vars definitions[:env_var], param if definitions.include?(:env_var)
20
20
  end
21
21
 
22
22
  def param_definitions
@@ -27,13 +27,13 @@ module Configliere
27
27
  # performs type coercion
28
28
  def resolve!
29
29
  resolve_types!
30
- begin ; super() ; rescue NoMethodError ; nil ; end
30
+ super()
31
31
  self
32
32
  end
33
33
 
34
34
  def validate!
35
35
  validate_requireds!
36
- begin ; super() ; rescue NoMethodError ; nil ; end
36
+ super()
37
37
  true
38
38
  end
39
39
 
@@ -163,17 +163,17 @@ module Configliere
163
163
  # Settings.foo = 4
164
164
  # Settings.foo #=> 4
165
165
  def method_missing meth, *args
166
- meth.to_s =~ /^(\w+)(=)?$/
167
- name, setter = [$1, $2]
168
- super unless name && param_definitions.include?(name)
166
+ meth.to_s =~ /^(\w+)(=)?$/ or return super
167
+ name, setter = [$1.to_sym, $2]
168
+ return(super) unless param_definitions.include?(name)
169
169
  if setter && (args.size == 1)
170
- self[$1] = args.first
170
+ self[name] = args.first
171
171
  elsif (!setter) && args.empty?
172
- self[meth]
172
+ self[name]
173
173
  else super ; end
174
174
  end
175
- end
176
175
 
176
+ end
177
177
  Param.class_eval do
178
178
  include Configliere::Define
179
179
  end
@@ -9,7 +9,7 @@ module Configliere
9
9
  # then calls the next step in the resolve! chain.
10
10
  def resolve!
11
11
  resolve_encrypted!
12
- begin ; super() ; rescue NoMethodError ; nil ; end
12
+ super()
13
13
  self
14
14
  end
15
15
 
@@ -2,31 +2,31 @@ require 'yaml'
2
2
  Configliere.use :define
3
3
  module Configliere
4
4
  #
5
- # Environment -- load configuration from environment variables
5
+ # EnvVar -- load configuration from environment variables
6
6
  #
7
- module Environment
8
- def environment_variables *envs
7
+ module EnvVar
8
+ def env_vars *envs
9
9
  envs.each do |env|
10
10
  case env
11
11
  when Hash
12
12
  env.each do |param, env|
13
- adopt_environment_variable! param, env
13
+ adopt_env_var! param, env
14
14
  end
15
15
  else
16
16
  param = env.to_s.downcase.to_sym
17
- adopt_environment_variable! param, env
17
+ adopt_env_var! param, env
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
- def params_from_environment
23
- definitions_for(:environment)
22
+ def params_from_env_vars
23
+ definitions_for(:env_var)
24
24
  end
25
25
 
26
26
  protected
27
- def adopt_environment_variable! param, env
27
+ def adopt_env_var! param, env
28
28
  env = env.to_s
29
- param_definitions[param][:environment] ||= env
29
+ param_definitions[param][:env_var] ||= env
30
30
  val = ENV[env]
31
31
  self[param] = val if val
32
32
  end
@@ -34,7 +34,7 @@ module Configliere
34
34
 
35
35
  Param.class_eval do
36
36
  # include read / save operations
37
- include Environment
37
+ include EnvVar
38
38
  end
39
39
  end
40
40
 
@@ -1,9 +1,6 @@
1
1
  require 'configliere/core_ext/sash.rb'
2
2
  module Configliere
3
3
  class ParamParent < ::Hash
4
- def finally *args, &block
5
- nil #no-op
6
- end
7
4
  # default export method: self
8
5
  def export
9
6
  to_hash
@@ -11,9 +8,8 @@ module Configliere
11
8
  # terminate resolution chain
12
9
  def resolve!
13
10
  end
14
-
11
+ # terminate validation chain
15
12
  def validate!
16
- true
17
13
  end
18
14
  end
19
15
 
@@ -63,12 +59,12 @@ module Configliere
63
59
 
64
60
  # Finalize and validate params
65
61
  def resolve!
66
- begin ; super() ; rescue NoMethodError ; nil ; end
62
+ super()
67
63
  validate!
68
64
  end
69
65
  # Check that all defined params are valid
70
66
  def validate!
71
- begin ; super() ; rescue NoMethodError ; nil ; end
67
+ super()
72
68
  end
73
69
 
74
70
  def []= param, val
@@ -101,11 +97,6 @@ module Configliere
101
97
  self.deep_merge!(hsh) unless hsh.nil?
102
98
  end
103
99
 
104
- # see Configliere::ConfigBlock#finally
105
- def finally *args, &block
106
- use :config_block
107
- super
108
- end
109
100
  protected
110
101
  # @param key<Object> The key to convert.
111
102
  #
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- Configliere.use :environment
2
+ Configliere.use :env_var
3
3
 
4
- describe "Configliere::Environment" do
4
+ describe "Configliere::EnvVar" do
5
5
  before do
6
6
  @config = Configliere::Param.new
7
7
  end
@@ -9,13 +9,13 @@ describe "Configliere::Environment" do
9
9
  describe 'loads environment variables' do
10
10
  it 'loads a simple value into the corresponding symbolic key' do
11
11
  ENV.should_receive(:[]).with('HOME').and_return('/fake/path')
12
- @config.environment_variables 'HOME'
12
+ @config.env_vars 'HOME'
13
13
  @config[:home].should == '/fake/path'
14
14
  end
15
15
  it 'loads a hash into the individual params' do
16
16
  ENV.should_receive(:[]).with('HOME').and_return('/fake/path')
17
17
  ENV.should_receive(:[]).with('POWER_SUPPLY').and_return('1.21 jigawatts')
18
- @config.environment_variables :home => 'HOME', 'delorean.power_supply' => 'POWER_SUPPLY'
18
+ @config.env_vars :home => 'HOME', 'delorean.power_supply' => 'POWER_SUPPLY'
19
19
  @config[:home].should == '/fake/path'
20
20
  @config[:delorean][:power_supply].should == '1.21 jigawatts'
21
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configliere
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - mrflip
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-15 00:00:00 -06:00
12
+ date: 2010-01-18 00:00:00 -06:00
13
13
  default_executable: configliere
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -52,11 +52,9 @@ files:
52
52
  - VERSION
53
53
  - bin/configliere
54
54
  - configliere.gemspec
55
- - examples/commandline_script.rb
56
- - examples/commandline_script.yaml
57
- - examples/config_block.rb
55
+ - examples/config_block_script.rb
58
56
  - examples/encrypted_script.rb
59
- - examples/foo.yaml
57
+ - examples/env_var_script.rb
60
58
  - examples/simple_script.rb
61
59
  - examples/simple_script.yaml
62
60
  - lib/configliere.rb
@@ -70,7 +68,7 @@ files:
70
68
  - lib/configliere/crypter.rb
71
69
  - lib/configliere/define.rb
72
70
  - lib/configliere/encrypted.rb
73
- - lib/configliere/environment.rb
71
+ - lib/configliere/env_var.rb
74
72
  - lib/configliere/param.rb
75
73
  - spec/configliere/commandline_spec.rb
76
74
  - spec/configliere/config_block_spec.rb
@@ -80,7 +78,7 @@ files:
80
78
  - spec/configliere/crypter_spec.rb
81
79
  - spec/configliere/define_spec.rb
82
80
  - spec/configliere/encrypted_spec.rb
83
- - spec/configliere/environment_spec.rb
81
+ - spec/configliere/env_var_spec.rb
84
82
  - spec/configliere/param_spec.rb
85
83
  - spec/configliere_spec.rb
86
84
  - spec/spec.opts
@@ -122,11 +120,11 @@ test_files:
122
120
  - spec/configliere/crypter_spec.rb
123
121
  - spec/configliere/define_spec.rb
124
122
  - spec/configliere/encrypted_spec.rb
125
- - spec/configliere/environment_spec.rb
123
+ - spec/configliere/env_var_spec.rb
126
124
  - spec/configliere/param_spec.rb
127
125
  - spec/configliere_spec.rb
128
126
  - spec/spec_helper.rb
129
- - examples/commandline_script.rb
130
- - examples/config_block.rb
127
+ - examples/config_block_script.rb
131
128
  - examples/encrypted_script.rb
129
+ - examples/env_var_script.rb
132
130
  - examples/simple_script.rb
@@ -1,74 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems' ; $: << File.dirname(__FILE__)+'/../lib'
3
- require 'pp'
4
- # module Configliere ; DEFAULT_CONFIG_FILENAME = File.dirname(__FILE__)+'/commandline_script.yaml' end
5
- require 'configliere'
6
-
7
- Settings.use :all
8
-
9
- puts %Q{
10
- This is a demo of the Configliere interface. It parses all command line options to load keys, global options, etc.
11
-
12
- Try running it as
13
-
14
- PLACES='go' NOISES='who' ./examples/simple_script.rb --cat=hat
15
-
16
- which should create
17
-
18
- expect: {:password=>"zike_bike", :horton=>{:hears_a=>"who"}, :key=>"asdf", :cat=>"hat", :things=>["thing_1", "thing_2"], :rate_per_hour=>10, :places=>"go", :wocket=>"pocket"}
19
- }
20
-
21
- # describe and define params
22
- Settings.define :cat, :description => 'The type of feline haberdashery to include in the story', :required => true, :type => Symbol
23
- Settings.define :wocket, :description => 'where the wocket is residing'
24
- Settings.define :password, :encrypted => true
25
-
26
- # static settings
27
- Settings :wocket => 'pocket', :key => 'asdf'
28
- # from environment
29
- Settings.environment_variables 'PLACES', 'NOISES' => 'horton.hears_a'
30
-
31
- # from config file
32
- Settings.read(File.dirname(__FILE__)+'/commandline_script.yaml')
33
-
34
- # from finally block
35
- Settings.finally do |c|
36
- c.lorax = 'tree'
37
- end
38
-
39
- # bookkeeping
40
- Settings.resolve!
41
- # Get the value for param[:key] from the keyboard if missing
42
- Settings.param_or_ask :key
43
-
44
- # Print results
45
- print ' actual: '
46
- p Settings
47
-
48
-
49
- fiddle = Configliere.new; fiddle.encrypt_pass = 'pass'
50
- fiddle.define 'amazon.api.key', :encrypted => true
51
- fiddle['amazon.api.encrypted_key'] = "{bo\335\256nt2Rc\016\244\216c\030\2627g\233%\300\035l\225\325\305z\207LR\333\035"
52
- fiddle.resolve!
53
- puts 'expect: [nil, "bite_me"]'
54
- print "actual: "; p [fiddle['amazon.api.encrypted_key'], fiddle['amazon.api.key'], fiddle.send(:export)]
55
- #
56
- fiddle = Configliere.new; fiddle.encrypt_pass = 'pass'
57
- fiddle.define 'amazon.api.key', :encrypted => true
58
- fiddle['amazon.api.key'] = 'bite_me'
59
- fiddle.resolve!
60
- puts 'expect: [nil, "bite_me"]'
61
- print "actual: "; p [fiddle['amazon.api.encrypted_key'], fiddle['amazon.api.key'], fiddle.send(:export)]
62
- #
63
- fiddle = Configliere.new; fiddle.encrypt_pass = 'pass'
64
- fiddle.define 'amazon.api.key', :encrypted => true
65
- fiddle['amazon.api.encrypted_key'] = "{bo\335\256nt2Rc\016\244\216c\030\2627g\233%\300\035l\225\325\305z\207LR\333\035"
66
- # fiddle.resolve!
67
- puts 'expect: ["{bo\335\256nt2Rc\016\244\216c\030\2627g\233%\300\035l\225\325\305z\207LR\333\035", nil]'
68
- print "actual: "; p [fiddle['amazon.api.encrypted_key'], fiddle['amazon.api.key'], fiddle.send(:export)]
69
-
70
-
71
-
72
- # save to disk
73
- # you can check that :password and :api_key have been properly encrypted.
74
- Settings.save! File.dirname(__FILE__)+'/foo.yaml'
@@ -1,8 +0,0 @@
1
- ---
2
- :encrypted_password: !binary |
3
- el58XhZZYLJ+IZZ1/Q1ab/JSyI7KuYyj8+dgf2vW9Fw=
4
-
5
- :things:
6
- - thing_1
7
- - thing_2
8
- :rate_per_hour: 10
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'configliere'
3
-
4
- Settings :passenger => 'einstein', :dest_time => '1955-11-05', 'delorean.power_source' => :plutonium
5
- Settings.finally do |c|
6
- p [self, 'finally', c[:passenger], c.passenger]
7
- # Einstein the dog should only be sent one minute into the future.
8
- dest_time = (Time.now + 60) if c.passenger == 'einstein'
9
- end
10
- Settings.resolve!
11
- p Settings
data/examples/foo.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- :horton:
3
- :hears_a: who
4
- :wocket: pocket
5
- :encrypted_password: !binary |
6
- BdwUvxy0xWh2oB5yWKm/BZ099E6pi+6Al+k09PoPik4=
7
-
8
- :things:
9
- - thing_1
10
- - thing_2
11
- :lorax: tree
12
- :rate_per_hour: 10
13
- :cat: :hat
14
- :key: asdf
15
- :places: go