configliere 0.3.4 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.document +3 -0
  2. data/.watchr +20 -0
  3. data/CHANGELOG.textile +99 -3
  4. data/Gemfile +26 -0
  5. data/Gemfile.lock +54 -0
  6. data/README.textile +162 -138
  7. data/Rakefile +30 -21
  8. data/VERSION +1 -1
  9. data/bin/configliere +77 -77
  10. data/bin/configliere-decrypt +85 -0
  11. data/bin/configliere-delete +85 -0
  12. data/bin/configliere-dump +85 -0
  13. data/bin/configliere-encrypt +85 -0
  14. data/bin/configliere-list +85 -0
  15. data/bin/configliere-set +85 -0
  16. data/configliere.gemspec +53 -23
  17. data/examples/config_block_script.rb +9 -2
  18. data/examples/encrypted_script.rb +28 -16
  19. data/examples/env_var_script.rb +2 -2
  20. data/examples/help_message_demo.rb +16 -0
  21. data/examples/independent_config.rb +28 -0
  22. data/examples/prompt.rb +23 -0
  23. data/examples/simple_script.rb +28 -15
  24. data/examples/simple_script.yaml +1 -1
  25. data/lib/configliere.rb +22 -24
  26. data/lib/configliere/commandline.rb +135 -116
  27. data/lib/configliere/commands.rb +38 -54
  28. data/lib/configliere/config_block.rb +4 -2
  29. data/lib/configliere/config_file.rb +30 -52
  30. data/lib/configliere/crypter.rb +8 -5
  31. data/lib/configliere/deep_hash.rb +368 -0
  32. data/lib/configliere/define.rb +83 -89
  33. data/lib/configliere/encrypted.rb +17 -18
  34. data/lib/configliere/env_var.rb +5 -7
  35. data/lib/configliere/param.rb +37 -64
  36. data/lib/configliere/prompt.rb +23 -0
  37. data/spec/configliere/commandline_spec.rb +156 -57
  38. data/spec/configliere/commands_spec.rb +75 -30
  39. data/spec/configliere/config_block_spec.rb +10 -1
  40. data/spec/configliere/config_file_spec.rb +83 -55
  41. data/spec/configliere/crypter_spec.rb +3 -2
  42. data/spec/configliere/deep_hash_spec.rb +401 -0
  43. data/spec/configliere/define_spec.rb +121 -42
  44. data/spec/configliere/encrypted_spec.rb +53 -20
  45. data/spec/configliere/env_var_spec.rb +24 -4
  46. data/spec/configliere/param_spec.rb +25 -27
  47. data/spec/configliere/prompt_spec.rb +50 -0
  48. data/spec/configliere_spec.rb +3 -9
  49. data/spec/spec_helper.rb +17 -6
  50. metadata +110 -35
  51. data/lib/configliere/core_ext.rb +0 -2
  52. data/lib/configliere/core_ext/blank.rb +0 -93
  53. data/lib/configliere/core_ext/hash.rb +0 -108
  54. data/lib/configliere/core_ext/sash.rb +0 -170
  55. data/spec/configliere/core_ext/hash_spec.rb +0 -78
  56. data/spec/configliere/core_ext/sash_spec.rb +0 -312
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.dirname(__FILE__)+'/../lib'
3
+ require 'configliere'
4
+
5
+ Settings.use :commands
6
+
7
+ Settings.description = %Q{Client for the configliere gem: manipulate configuration and passwords for automated scripts}
8
+
9
+ Settings.define_command :dump, :description => "Show the whole contents of the file"
10
+ Settings.define_command :list, :description => "Show all params in the configliere file."
11
+ Settings.define_command :get, :description => "show values given as commandline args, eg: #{File.basename($0)} get this that"
12
+ Settings.define_command :set, :description => "sets values given as commandline args, eg: #{File.basename($0)} set this=1 that=3"
13
+ Settings.define_command :delete, :description => "sets values given as commandline args, eg: #{File.basename($0)} delete this that"
14
+ Settings.define_command :encrypt, :description => "encrypt the param"
15
+ Settings.define_command :decrypt, :description => "Store the param as decrypted back into the file. Can be undone with 'encrypt'."
16
+
17
+ Settings.define :from, :flag => 'f', :type => :filename, :description => "Configliere config file to load"
18
+ Settings.define :into, :flag => 'i', :type => :filename, :description => "Configliere config file to save into"
19
+
20
+ Settings.resolve!
21
+ ARGV.replace []
22
+
23
+ Store = Configliere.new
24
+ Store.read(Settings.from) if Settings.from
25
+
26
+ #
27
+ # Execute
28
+ #
29
+
30
+ case Settings.command_name
31
+ when :dump
32
+ puts Store.to_hash.to_yaml
33
+
34
+ when :list
35
+ $stderr.puts "Param names in #{Settings.from}:"
36
+ Store.each do |attr, val|
37
+ puts " #{attr}"
38
+ end
39
+
40
+ when :get
41
+ $stderr.puts "Values for #{Settings.rest.join(", ")} from #{Settings.from}"
42
+ Settings.rest.map(&:to_sym).each do |attr|
43
+ puts "%-23s\t%s" % ["#{attr}:", Store[attr]]
44
+ end
45
+
46
+ when :set
47
+ $stderr.puts "Setting #{Settings.rest.join(", ")} for #{Settings.from}"
48
+ Settings.rest.each do |attr_val|
49
+ attr, val = attr_val.split('=', 2)
50
+ attr = attr.to_sym
51
+ if val.nil? then warn "Please specify a value for #{attr}" ; next ; end
52
+ Store[attr] = val
53
+
54
+ puts "%-23s\t%s" % ["#{attr}:", val.inspect]
55
+ end
56
+
57
+ when :delete
58
+ $stderr.puts "Deleting #{Settings.rest.join(", ")} from #{Settings.from}. O, I die, Horatio."
59
+ Settings.rest.map(&:to_sym).each do |attr|
60
+ Store.delete(attr)
61
+ end
62
+
63
+ when :encrypt
64
+ Store.use :encrypted
65
+ $stderr.puts "Encrypting #{Settings.rest.join(", ")} from #{Settings.from}. Fnord"
66
+ Settings.rest.each{|attr| Store.define attr, :encrypted => true }
67
+ Store.encrypt_pass = Settings.encrypt_pass || Settings[:encrypt_pass]
68
+ Store.resolve!
69
+
70
+ when :decrypt
71
+ Store.use :encrypted
72
+ $stderr.puts "Decrypting #{Settings.rest.join(", ")} from #{Settings.from}. Fnord"
73
+ Settings.rest.each{|attr| Store.define attr, :encrypted => true }
74
+ Store.encrypt_pass = Settings.encrypt_pass || Settings[:encrypt_pass]
75
+ Store.resolve!
76
+
77
+ puts Store.to_hash.to_yaml
78
+
79
+ else
80
+ Settings.die "Please use one of the given commands"
81
+ end
82
+
83
+ if Settings.into
84
+ Store.save!(Settings.into)
85
+ end
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.dirname(__FILE__)+'/../lib'
3
+ require 'configliere'
4
+
5
+ Settings.use :commands
6
+
7
+ Settings.description = %Q{Client for the configliere gem: manipulate configuration and passwords for automated scripts}
8
+
9
+ Settings.define_command :dump, :description => "Show the whole contents of the file"
10
+ Settings.define_command :list, :description => "Show all params in the configliere file."
11
+ Settings.define_command :get, :description => "show values given as commandline args, eg: #{File.basename($0)} get this that"
12
+ Settings.define_command :set, :description => "sets values given as commandline args, eg: #{File.basename($0)} set this=1 that=3"
13
+ Settings.define_command :delete, :description => "sets values given as commandline args, eg: #{File.basename($0)} delete this that"
14
+ Settings.define_command :encrypt, :description => "encrypt the param"
15
+ Settings.define_command :decrypt, :description => "Store the param as decrypted back into the file. Can be undone with 'encrypt'."
16
+
17
+ Settings.define :from, :flag => 'f', :type => :filename, :description => "Configliere config file to load"
18
+ Settings.define :into, :flag => 'i', :type => :filename, :description => "Configliere config file to save into"
19
+
20
+ Settings.resolve!
21
+ ARGV.replace []
22
+
23
+ Store = Configliere.new
24
+ Store.read(Settings.from) if Settings.from
25
+
26
+ #
27
+ # Execute
28
+ #
29
+
30
+ case Settings.command_name
31
+ when :dump
32
+ puts Store.to_hash.to_yaml
33
+
34
+ when :list
35
+ $stderr.puts "Param names in #{Settings.from}:"
36
+ Store.each do |attr, val|
37
+ puts " #{attr}"
38
+ end
39
+
40
+ when :get
41
+ $stderr.puts "Values for #{Settings.rest.join(", ")} from #{Settings.from}"
42
+ Settings.rest.map(&:to_sym).each do |attr|
43
+ puts "%-23s\t%s" % ["#{attr}:", Store[attr]]
44
+ end
45
+
46
+ when :set
47
+ $stderr.puts "Setting #{Settings.rest.join(", ")} for #{Settings.from}"
48
+ Settings.rest.each do |attr_val|
49
+ attr, val = attr_val.split('=', 2)
50
+ attr = attr.to_sym
51
+ if val.nil? then warn "Please specify a value for #{attr}" ; next ; end
52
+ Store[attr] = val
53
+
54
+ puts "%-23s\t%s" % ["#{attr}:", val.inspect]
55
+ end
56
+
57
+ when :delete
58
+ $stderr.puts "Deleting #{Settings.rest.join(", ")} from #{Settings.from}. O, I die, Horatio."
59
+ Settings.rest.map(&:to_sym).each do |attr|
60
+ Store.delete(attr)
61
+ end
62
+
63
+ when :encrypt
64
+ Store.use :encrypted
65
+ $stderr.puts "Encrypting #{Settings.rest.join(", ")} from #{Settings.from}. Fnord"
66
+ Settings.rest.each{|attr| Store.define attr, :encrypted => true }
67
+ Store.encrypt_pass = Settings.encrypt_pass || Settings[:encrypt_pass]
68
+ Store.resolve!
69
+
70
+ when :decrypt
71
+ Store.use :encrypted
72
+ $stderr.puts "Decrypting #{Settings.rest.join(", ")} from #{Settings.from}. Fnord"
73
+ Settings.rest.each{|attr| Store.define attr, :encrypted => true }
74
+ Store.encrypt_pass = Settings.encrypt_pass || Settings[:encrypt_pass]
75
+ Store.resolve!
76
+
77
+ puts Store.to_hash.to_yaml
78
+
79
+ else
80
+ Settings.die "Please use one of the given commands"
81
+ end
82
+
83
+ if Settings.into
84
+ Store.save!(Settings.into)
85
+ end
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.dirname(__FILE__)+'/../lib'
3
+ require 'configliere'
4
+
5
+ Settings.use :commands
6
+
7
+ Settings.description = %Q{Client for the configliere gem: manipulate configuration and passwords for automated scripts}
8
+
9
+ Settings.define_command :dump, :description => "Show the whole contents of the file"
10
+ Settings.define_command :list, :description => "Show all params in the configliere file."
11
+ Settings.define_command :get, :description => "show values given as commandline args, eg: #{File.basename($0)} get this that"
12
+ Settings.define_command :set, :description => "sets values given as commandline args, eg: #{File.basename($0)} set this=1 that=3"
13
+ Settings.define_command :delete, :description => "sets values given as commandline args, eg: #{File.basename($0)} delete this that"
14
+ Settings.define_command :encrypt, :description => "encrypt the param"
15
+ Settings.define_command :decrypt, :description => "Store the param as decrypted back into the file. Can be undone with 'encrypt'."
16
+
17
+ Settings.define :from, :flag => 'f', :type => :filename, :description => "Configliere config file to load"
18
+ Settings.define :into, :flag => 'i', :type => :filename, :description => "Configliere config file to save into"
19
+
20
+ Settings.resolve!
21
+ ARGV.replace []
22
+
23
+ Store = Configliere.new
24
+ Store.read(Settings.from) if Settings.from
25
+
26
+ #
27
+ # Execute
28
+ #
29
+
30
+ case Settings.command_name
31
+ when :dump
32
+ puts Store.to_hash.to_yaml
33
+
34
+ when :list
35
+ $stderr.puts "Param names in #{Settings.from}:"
36
+ Store.each do |attr, val|
37
+ puts " #{attr}"
38
+ end
39
+
40
+ when :get
41
+ $stderr.puts "Values for #{Settings.rest.join(", ")} from #{Settings.from}"
42
+ Settings.rest.map(&:to_sym).each do |attr|
43
+ puts "%-23s\t%s" % ["#{attr}:", Store[attr]]
44
+ end
45
+
46
+ when :set
47
+ $stderr.puts "Setting #{Settings.rest.join(", ")} for #{Settings.from}"
48
+ Settings.rest.each do |attr_val|
49
+ attr, val = attr_val.split('=', 2)
50
+ attr = attr.to_sym
51
+ if val.nil? then warn "Please specify a value for #{attr}" ; next ; end
52
+ Store[attr] = val
53
+
54
+ puts "%-23s\t%s" % ["#{attr}:", val.inspect]
55
+ end
56
+
57
+ when :delete
58
+ $stderr.puts "Deleting #{Settings.rest.join(", ")} from #{Settings.from}. O, I die, Horatio."
59
+ Settings.rest.map(&:to_sym).each do |attr|
60
+ Store.delete(attr)
61
+ end
62
+
63
+ when :encrypt
64
+ Store.use :encrypted
65
+ $stderr.puts "Encrypting #{Settings.rest.join(", ")} from #{Settings.from}. Fnord"
66
+ Settings.rest.each{|attr| Store.define attr, :encrypted => true }
67
+ Store.encrypt_pass = Settings.encrypt_pass || Settings[:encrypt_pass]
68
+ Store.resolve!
69
+
70
+ when :decrypt
71
+ Store.use :encrypted
72
+ $stderr.puts "Decrypting #{Settings.rest.join(", ")} from #{Settings.from}. Fnord"
73
+ Settings.rest.each{|attr| Store.define attr, :encrypted => true }
74
+ Store.encrypt_pass = Settings.encrypt_pass || Settings[:encrypt_pass]
75
+ Store.resolve!
76
+
77
+ puts Store.to_hash.to_yaml
78
+
79
+ else
80
+ Settings.die "Please use one of the given commands"
81
+ end
82
+
83
+ if Settings.into
84
+ Store.save!(Settings.into)
85
+ end
data/configliere.gemspec CHANGED
@@ -5,36 +5,47 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{configliere}
8
- s.version = "0.3.4"
8
+ s.version = "0.4.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["mrflip"]
12
- s.date = %q{2011-04-03}
13
- s.default_executable = %q{configliere}
11
+ s.authors = ["infochimps", "mrflip"]
12
+ s.date = %q{2011-05-16}
14
13
  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
14
 
16
15
  "" So, Consigliere of mine, I think you should tell your Don what everyone knows. "" -- Don Corleone
17
16
 
18
17
  Configliere manage settings from many sources: static constants, simple config files, environment variables, commandline options, straight ruby. You don't have to predefine anything, but you can ask configliere to type-convert, require, document or password-obscure any of its fields. Modules can define config settings independently of each other and the main program.
19
18
  }
20
- s.email = %q{flip@infochimps.org}
21
- s.executables = ["configliere"]
19
+ s.email = %q{coders@infochimps.org}
20
+ s.executables = ["configliere", "configliere-decrypt", "configliere-delete", "configliere-dump", "configliere-encrypt", "configliere-list", "configliere-set"]
22
21
  s.extra_rdoc_files = [
23
22
  "LICENSE",
24
23
  "README.textile"
25
24
  ]
26
25
  s.files = [
27
26
  ".document",
27
+ ".watchr",
28
28
  "CHANGELOG.textile",
29
+ "Gemfile",
30
+ "Gemfile.lock",
29
31
  "LICENSE",
30
32
  "README.textile",
31
33
  "Rakefile",
32
34
  "VERSION",
33
35
  "bin/configliere",
36
+ "bin/configliere-decrypt",
37
+ "bin/configliere-delete",
38
+ "bin/configliere-dump",
39
+ "bin/configliere-encrypt",
40
+ "bin/configliere-list",
41
+ "bin/configliere-set",
34
42
  "configliere.gemspec",
35
43
  "examples/config_block_script.rb",
36
44
  "examples/encrypted_script.rb",
37
45
  "examples/env_var_script.rb",
46
+ "examples/help_message_demo.rb",
47
+ "examples/independent_config.rb",
48
+ "examples/prompt.rb",
38
49
  "examples/simple_script.rb",
39
50
  "examples/simple_script.yaml",
40
51
  "lib/configliere.rb",
@@ -42,68 +53,87 @@ Configliere manage settings from many sources: static constants, simple config f
42
53
  "lib/configliere/commands.rb",
43
54
  "lib/configliere/config_block.rb",
44
55
  "lib/configliere/config_file.rb",
45
- "lib/configliere/core_ext.rb",
46
- "lib/configliere/core_ext/blank.rb",
47
- "lib/configliere/core_ext/hash.rb",
48
- "lib/configliere/core_ext/sash.rb",
49
56
  "lib/configliere/crypter.rb",
57
+ "lib/configliere/deep_hash.rb",
50
58
  "lib/configliere/define.rb",
51
59
  "lib/configliere/encrypted.rb",
52
60
  "lib/configliere/env_var.rb",
53
61
  "lib/configliere/param.rb",
62
+ "lib/configliere/prompt.rb",
54
63
  "spec/configliere/commandline_spec.rb",
55
64
  "spec/configliere/commands_spec.rb",
56
65
  "spec/configliere/config_block_spec.rb",
57
66
  "spec/configliere/config_file_spec.rb",
58
- "spec/configliere/core_ext/hash_spec.rb",
59
- "spec/configliere/core_ext/sash_spec.rb",
60
67
  "spec/configliere/crypter_spec.rb",
68
+ "spec/configliere/deep_hash_spec.rb",
61
69
  "spec/configliere/define_spec.rb",
62
70
  "spec/configliere/encrypted_spec.rb",
63
71
  "spec/configliere/env_var_spec.rb",
64
72
  "spec/configliere/param_spec.rb",
73
+ "spec/configliere/prompt_spec.rb",
65
74
  "spec/configliere_spec.rb",
66
75
  "spec/spec.opts",
67
76
  "spec/spec_helper.rb"
68
77
  ]
69
- s.homepage = %q{http://github.com/mrflip/configliere}
78
+ s.homepage = %q{http://infochimps.com/labs}
79
+ s.licenses = ["MIT"]
70
80
  s.require_paths = ["lib"]
71
- s.rubygems_version = %q{1.3.7}
81
+ s.rubygems_version = %q{1.5.0}
72
82
  s.summary = %q{Wise, discreet configuration management}
73
83
  s.test_files = [
74
84
  "examples/config_block_script.rb",
75
85
  "examples/encrypted_script.rb",
76
86
  "examples/env_var_script.rb",
87
+ "examples/help_message_demo.rb",
88
+ "examples/independent_config.rb",
89
+ "examples/prompt.rb",
77
90
  "examples/simple_script.rb",
78
91
  "spec/configliere/commandline_spec.rb",
79
92
  "spec/configliere/commands_spec.rb",
80
93
  "spec/configliere/config_block_spec.rb",
81
94
  "spec/configliere/config_file_spec.rb",
82
- "spec/configliere/core_ext/hash_spec.rb",
83
- "spec/configliere/core_ext/sash_spec.rb",
84
95
  "spec/configliere/crypter_spec.rb",
96
+ "spec/configliere/deep_hash_spec.rb",
85
97
  "spec/configliere/define_spec.rb",
86
98
  "spec/configliere/encrypted_spec.rb",
87
99
  "spec/configliere/env_var_spec.rb",
88
100
  "spec/configliere/param_spec.rb",
101
+ "spec/configliere/prompt_spec.rb",
89
102
  "spec/configliere_spec.rb",
90
103
  "spec/spec_helper.rb"
91
104
  ]
92
105
 
93
106
  if s.respond_to? :specification_version then
94
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
95
107
  s.specification_version = 3
96
108
 
97
109
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
98
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
99
- s.add_development_dependency(%q<yard>, [">= 0"])
110
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.12"])
111
+ s.add_development_dependency(%q<yard>, ["~> 0.6.7"])
112
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
113
+ s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
114
+ s.add_development_dependency(%q<spork>, ["~> 0.9.0.rc5"])
115
+ s.add_development_dependency(%q<RedCloth>, [">= 0"])
116
+ s.add_development_dependency(%q<gorillib>, [">= 0.0.4"])
117
+ s.add_development_dependency(%q<highline>, [">= 1.5.2"])
100
118
  else
101
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
102
- s.add_dependency(%q<yard>, [">= 0"])
119
+ s.add_dependency(%q<bundler>, ["~> 1.0.12"])
120
+ s.add_dependency(%q<yard>, ["~> 0.6.7"])
121
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
122
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
123
+ s.add_dependency(%q<spork>, ["~> 0.9.0.rc5"])
124
+ s.add_dependency(%q<RedCloth>, [">= 0"])
125
+ s.add_dependency(%q<gorillib>, [">= 0.0.4"])
126
+ s.add_dependency(%q<highline>, [">= 1.5.2"])
103
127
  end
104
128
  else
105
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
106
- s.add_dependency(%q<yard>, [">= 0"])
129
+ s.add_dependency(%q<bundler>, ["~> 1.0.12"])
130
+ s.add_dependency(%q<yard>, ["~> 0.6.7"])
131
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
132
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
133
+ s.add_dependency(%q<spork>, ["~> 0.9.0.rc5"])
134
+ s.add_dependency(%q<RedCloth>, [">= 0"])
135
+ s.add_dependency(%q<gorillib>, [">= 0.0.4"])
136
+ s.add_dependency(%q<highline>, [">= 1.5.2"])
107
137
  end
108
138
  end
109
139
 
@@ -8,9 +8,16 @@ Settings.define :dest_time, :type => DateTime
8
8
  Settings :passenger => 'einstein', :dest_time => '1955-11-05'
9
9
 
10
10
  Settings.finally do |c|
11
- p [self, 'finally', c[:passenger], c.passenger]
11
+ p ['(2) takes the settings object as arg', self, c[:passenger], c.passenger]
12
12
  # Einstein the dog should only be sent one minute into the future.
13
13
  c.dest_time = (Time.now + 60) if c.passenger == 'einstein'
14
14
  end
15
+
16
+ Settings.finally{ p ['(3) note that blocks go in order'] }
17
+
18
+ Settings.define :mc_fly, :default => 'wuss',
19
+ :finally => lambda{ p ['(4) here is a block in the define'] ; Settings.mc_fly = 'badass' }
20
+
21
+ p ["(1) :finally blocks are called when you invoke resolve!"]
15
22
  Settings.resolve!
16
- p Settings
23
+ p ['(5) here are the settings', Settings]
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
2
+ $: << File.dirname(__FILE__)+'/../lib'
3
3
  require 'configliere'
4
4
  DUMP_FILENAME = '/tmp/encrypted_script.yml'
5
5
 
@@ -9,6 +9,10 @@ DUMP_FILENAME = '/tmp/encrypted_script.yml'
9
9
  # ./examples/encrypted_script.rb
10
10
  #
11
11
 
12
+ def dump_settings
13
+ puts " #{Settings.inspect} -- #{Settings.encrypt_pass}"
14
+ end
15
+
12
16
  puts %Q{Many times, scripts need to save values you\'d rather not leave as
13
17
  plaintext: API keys, database passwords, etc. Instead of leaving them in plain
14
18
  sight, you may wish to obscure their value on disk and use a secondary password
@@ -17,24 +21,29 @@ to unlock it.
17
21
  View source for this script to see commands you might use (from the irb console
18
22
  or in a standalone script) to store the obscured values for later decryption.}
19
23
 
20
- Settings.use :config_file, :define, :encrypted, :encrypt_pass => 'password1'
21
- Settings.define :password, :encrypted => true, :default => 'plaintext'
24
+ Settings.use :config_file
25
+ Settings.encrypt_pass = 'password1'
26
+ Settings.define :secret, :encrypted => true, :default => 'plaintext'
22
27
  Settings.resolve!
23
28
 
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}):"
29
+ puts "\nIn-memory version still has secret in plaintext..."
30
+ dump_settings
31
+ puts "But the saved version will have encrypted secret (see #{DUMP_FILENAME}):"
27
32
  puts " #{Settings.send(:export).inspect}"
28
33
  Settings.save!(DUMP_FILENAME)
29
34
 
35
+ puts "Let's reset the Settings:"
36
+ Settings.delete :secret
37
+ dump_settings
30
38
  puts "If we now load the saved file, the parameter's value is decrypted on resolve:"
31
- Settings[:password] = 'nothing up my sleeve'
32
39
  Settings.read('/tmp/encrypted_script.yml')
33
- Settings.resolve!
34
- puts " #{Settings.inspect}"
35
-
36
- # unset encrypt_pass
37
- Settings.encrypt_pass = nil
40
+ begin
41
+ Settings.resolve!
42
+ rescue StandardError => e
43
+ warn " #{e.class}: #{e}"
44
+ warn "\nTry rerunning with \n ENCRYPT_PASS=password1 #{$0} #{$ARGV}"
45
+ end
46
+ dump_settings
38
47
 
39
48
  puts %Q{\nOf course, in your script you\'ll have to supply the decryption
40
49
  password. The best thing is to use an environment variable -- a user can spy on
@@ -42,11 +51,14 @@ your commandline parameters using "ps" or "top". The following will fail unless
42
51
  you supply the correct password ("password1") in the ENCRYPT_PASS environment
43
52
  variable:\n\n}
44
53
 
45
- Settings.define :encrypt_pass, :env_var => 'ENCRYPT_PASS'
54
+ Settings.encrypt_pass = ENV['ENCRYPT_PASS'] # this will happen normally, but we overrode it above
55
+
46
56
  Settings.read('/tmp/encrypted_script.yml')
47
57
  begin
48
58
  Settings.resolve!
49
- puts " #{Settings.inspect}"
50
- rescue RuntimeError => e
51
- warn " #{e}"
59
+ puts "You guessed the password!"
60
+ dump_settings
61
+ rescue StandardError => e
62
+ warn " #{e.class}: #{e}"
63
+ warn "\nTry rerunning with \n ENCRYPT_PASS=password1 #{$0} #{$ARGV}"
52
64
  end