confswap 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 215b9df2a80e58dc45557cb8359c50e87077480c
4
- data.tar.gz: b00ef6a97570079f5dd225eba7685347cf8f62bd
3
+ metadata.gz: bf14a7b12e576ef8ed3a80def9604890cb741567
4
+ data.tar.gz: a2460ccf7e5e02b1759d2f549633529e849bab01
5
5
  SHA512:
6
- metadata.gz: 16bf29af5ea5965625970900ae230813e7c7267393212343532114e5b0eb46eeb20e65b23246cf0b2c3dc13d51248b513598c869b12377d969b8d79f05dafcda
7
- data.tar.gz: 9814d9e753eba16381f24d07ad22aeed0d1fb87c79717f4e2ab15b55d65bd238f8f7cc2e23083b2071ca8d04f5407c5085df958538e03ef1011417ba6b09beb8
6
+ metadata.gz: e3d8797cd327f6314d0bb4d529efac96be10e590f4fd59f81dd5b33f2ed9e99a3cd83385cb839cfa9c7e3130526f1695b89343d6242b330059d26fd55317ce9f
7
+ data.tar.gz: 93491351e0d1b33ad9c2ff34cb66211abd6fcaf442356c11b9876a0ad88066c3a2f24d5af0585c3f4af5c57c43b5f03e02be268f6119309d3e5e6c79d7815cab
data/.gitignore CHANGED
@@ -4,8 +4,10 @@
4
4
  ## My little test files
5
5
  test.conf
6
6
  test.conf.out
7
+ test.out
7
8
  test.txt
8
9
  test.txt.out
10
+ sample.properties
9
11
  pfile.txt
10
12
 
11
13
  ## Ignore Vim swp
data/Gemfile CHANGED
@@ -7,7 +7,7 @@ group :development, :test do
7
7
  gem 'simplecov'
8
8
  gem 'travis-lint'
9
9
  gem 'rspec'
10
- gem 'cucumber'
10
+ gem 'cucumber', '1.3.18'
11
11
  gem 'guard-rspec'
12
12
  gem 'aruba'
13
13
  end
data/confswap.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'confswap'
3
- s.version = '0.0.3'
3
+ s.version = '0.0.4'
4
4
  s.licenses = ['APACHE2']
5
5
  s.summary = "Confswap swaps conf variables!"
6
6
  s.description = "Create a configuration from a template using environment variables"
@@ -0,0 +1,17 @@
1
+ Feature: Add environment variables with command line
2
+ Background:
3
+ Given a file named "test.conf" with:
4
+ """
5
+ # This is a test config
6
+ # that has some variables in it
7
+ %{TEST}
8
+ """
9
+ Scenario: Single environment variable
10
+ When I run `confswap -e TEST=giblets test.conf`
11
+ Then a file named "test.conf.out" should exist
12
+ And the file "test.conf.out" should contain:
13
+ """
14
+ giblets
15
+ """
16
+
17
+ Scenario: Multiple environment variable
@@ -0,0 +1,27 @@
1
+ Feature: Confswap should read template variables from a properties file
2
+ Scenario: Fill in a configuration file with variables from a properties file
3
+
4
+ Given a file named "config_template_for_properties.conf" with:
5
+ """
6
+ I like %{VAR1}
7
+ but I really don't like %{VAR2}
8
+ # I can handle comments
9
+ and finally %{VAR3}
10
+ """
11
+ And a file named "sample.properties" with:
12
+ """
13
+ # This is a test config
14
+ # that has some variables in it
15
+ # some comments, and some new lines
16
+ VAR1: Giblets
17
+ VAR2: Not so giblets
18
+
19
+ # This is a descriptive comment
20
+ VAR3: so many giblets
21
+ """
22
+ When I run `confswap -p sample.properties -o sample.conf config_template_for_properties.conf`
23
+ Then a file named "sample.conf" should exist
24
+ And the file "sample.conf" should contain:
25
+ """
26
+ I like Giblets
27
+ """
@@ -1,2 +1,5 @@
1
1
  require 'fileutils'
2
2
  require 'aruba/cucumber'
3
+
4
+ tmp_dir = File.expand_path(File.dirname(__FILE__)) + '/../../tmp'
5
+ puts tmp_dir
@@ -1,7 +1,5 @@
1
1
  Before do
2
- FileUtils.rm_rf '/tmp/confswap'
3
2
  end
4
3
 
5
4
  After do
6
- FileUtils.rm_rf '/tmp/confswap'
7
5
  end
@@ -0,0 +1,34 @@
1
+ Feature: Confswap should replace variables in config template with environment variables
2
+ Background:
3
+ Given a file named "test.conf" with:
4
+ """
5
+ # This is a test config
6
+ # that has some variables in it
7
+ %{TEST}
8
+ """
9
+ And I set the environment variables to:
10
+ | variable | value |
11
+ | TEST | giblets |
12
+
13
+ Scenario: Config file should be created with a default name and contain environment variables
14
+ When I run `confswap test.conf`
15
+ Then a file named "test.conf.out" should exist
16
+ And the file "test.conf.out" should contain:
17
+ """
18
+ giblets
19
+ """
20
+ Scenario: Config file should be created with a specific name and contain environment variables
21
+ When I run `confswap --output=named.conf test.conf`
22
+ Then a file named "named.conf" should exist
23
+ And the file "named.conf" should contain:
24
+ """
25
+ giblets
26
+ """
27
+ Scenario: Force an overwrite of a config when a file already exists
28
+ Given a file named "existing.conf" with:
29
+ """
30
+ # This is an exising config
31
+ """
32
+ When I run `confswap --output=existing.conf test.conf`
33
+ Then a file named "existing.conf" should exist
34
+
@@ -3,5 +3,5 @@ Feature: See what version of confswap you're running
3
3
  Given I run `confswap --version`
4
4
  Then the output should contain:
5
5
  """
6
- 0.0.2
6
+ 0.0.4
7
7
  """
@@ -4,8 +4,8 @@ require 'confswap'
4
4
  module Confswap
5
5
  class Command < Clamp::Command
6
6
 
7
- def initialize(*args)
8
- super(*args)
7
+ def initialize *args
8
+ super *args
9
9
  end
10
10
 
11
11
  def help(*args)
@@ -15,28 +15,28 @@ module Confswap
15
15
  ].join("\n")
16
16
  end
17
17
 
18
- def run(*args)
19
- super(*args)
18
+ def run *args
19
+ super *args
20
20
  end
21
21
 
22
22
  def execute
23
- if version?
24
- puts Confswap::VERSION
25
- return 0
26
- end
27
-
28
- if configuration_filename.nil?
29
- puts 'Specify a template file or use --help for usage information'
30
- return 0
31
- end
32
-
33
- if File.exists? configuration_filename
34
- swap_config configuration_filename
35
- return 0
36
- else
37
- puts "Error: Configuration template file with name #{configuration_filename} does not exist"
38
- return 1
39
- end
23
+ if version?
24
+ puts Confswap::VERSION
25
+ return 0
26
+ end
27
+
28
+ if configuration_filename.nil?
29
+ puts 'Specify a template file or use --help for usage information'
30
+ return 0
31
+ end
32
+
33
+ if File.exists? configuration_filename
34
+ swap_config configuration_filename
35
+ return 0
36
+ else
37
+ puts "Error: Configuration template file with name #{configuration_filename} does not exist"
38
+ return 1
39
+ end
40
40
  end
41
41
 
42
42
  def swap_config configuration_filename
@@ -45,7 +45,28 @@ module Confswap
45
45
  configuration_template = Confswap::ConfigurationFileReader.read configuration_filename
46
46
  env_variables = Confswap::EnvironmentVariableReader.read_variables
47
47
 
48
- if (!property_file.nil?) || (File.exists? property_file)
48
+ if envvars
49
+ envvars.each {|envvar|
50
+ key_and_value = envvar.split(/=/, 2)
51
+ key = key_and_value.first
52
+ value = key_and_value.last
53
+ if key_and_value.count != 2
54
+ puts "Invalid envvar specified #{key} and #{value}"
55
+ return 1
56
+ end
57
+
58
+ env_variables[key.to_sym]=value
59
+ }
60
+
61
+ if verbose?
62
+ puts "Environment variables:"
63
+ env_variables.each { |var|
64
+ puts "#{var.first} = #{var.last}"
65
+ }
66
+ end
67
+ end
68
+
69
+ if !property_file.nil? and File.exists? property_file
49
70
  puts 'pfile specified'
50
71
  env_variables = Confswap::PropertyFileVariableReader.read_variables_from_file property_file
51
72
  p env_variables
@@ -54,7 +75,7 @@ module Confswap
54
75
  begin
55
76
  output = configuration_template % env_variables
56
77
  rescue KeyError => error
57
- puts "#{error.message}. Your configuration specifies this variable, but it was not found as an environment variable."
78
+ puts "#{error.message}. Your configuration requires this variable, but no environment variable was found or specified."
58
79
  exit(1)
59
80
  end
60
81
 
@@ -73,7 +94,7 @@ module Confswap
73
94
  end
74
95
 
75
96
  option ['-p', '--property-file'], "FILE PATH", 'A path to a property file to use for your template variables', :attribute_name => :property_file
76
- option ['-e', '--envvar'], "VARIABLE", 'Specify one or more additional environment variables', :multivalued => true
97
+ option ['-e', '--envvar'], "VARIABLE", 'Specify one or more additional environment variables', :multivalued => true, :attribute_name => :envvars
77
98
  option ['-f','--force'], :flag, "Overwrite file if it already exists", :attribute_name => :force
78
99
  option ['-v', '--version'], :flag, "The version of confswap you are running", :attribute_name => :version
79
100
  option ['-o', '--output'], "FILE PATH", "Specifies the filepath for the file", :attribute_name => :output_filename
@@ -1,3 +1,3 @@
1
1
  module Confswap
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,16 +1,12 @@
1
1
  require 'confswap/property_reader'
2
2
 
3
3
  describe 'Confswap::PropertyFileVariableReader' do
4
- it 'should read environment variables from a file and store them in a hash' do
5
- read_variables = Confswap::PropertyFileVariableReader.read_variables_from_file 'tmp/test1vars'
6
- expect(read_variables).to be_instance_of Hash
7
- end
8
-
9
4
  it 'should split only on first colon and extra newlines' do
10
5
  variables = "VAR1: 192.168.0.1\n\n\nVAR2: giblets\nVAR3: this should : : have: colons"
11
- File.write 'tmp/test2vars', variables
12
- vars = Confswap::PropertyFileVariableReader.read_variables_from_file 'tmp/test2vars'
6
+ File.write 'tmp/property_vars', variables
7
+ vars = Confswap::PropertyFileVariableReader.read_variables_from_file 'tmp/property_vars'
13
8
 
9
+ expect(vars).to be_instance_of Hash
14
10
  expect(vars.length).to equal(3)
15
11
  expect(vars).to eq({
16
12
  :VAR1 => '192.168.0.1',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confswap
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
  - Tom Cowling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-01 00:00:00.000000000 Z
11
+ date: 2015-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clamp
@@ -54,10 +54,11 @@ files:
54
54
  - Rakefile
55
55
  - bin/confswap
56
56
  - confswap.gemspec
57
- - features/overwrite_existing_file.feature
57
+ - features/pass_environment_variables_with_command_line.feature
58
+ - features/property_file.feature
58
59
  - features/support/env.rb
59
60
  - features/support/hooks.rb
60
- - features/swap.feature
61
+ - features/variable_replace.feature
61
62
  - features/version.feature
62
63
  - lib/confswap.rb
63
64
  - lib/confswap/command.rb
@@ -94,10 +95,11 @@ signing_key:
94
95
  specification_version: 4
95
96
  summary: Confswap swaps conf variables!
96
97
  test_files:
97
- - features/overwrite_existing_file.feature
98
+ - features/pass_environment_variables_with_command_line.feature
99
+ - features/property_file.feature
98
100
  - features/support/env.rb
99
101
  - features/support/hooks.rb
100
- - features/swap.feature
102
+ - features/variable_replace.feature
101
103
  - features/version.feature
102
104
  - spec/conf_reader_spec.rb
103
105
  - spec/env_reader_spec.rb
@@ -1,3 +0,0 @@
1
- Feature: Overwrite a config that already exists
2
- Scenario: A file already exists
3
-
@@ -1,26 +0,0 @@
1
- Feature: Confswap should swap template variables
2
- Background:
3
- Given a file named "/tmp/confswap/test.conf" with:
4
- """
5
- # This is a test config
6
- # that has some variables in it
7
- %{TEST}
8
- """
9
- And I set the environment variables to:
10
- | variable | value |
11
- | TEST | giblets |
12
-
13
- Scenario: Test config file with placeholders rename standard
14
- When I run `confswap /tmp/confswap/test.conf`
15
- Then the file "/tmp/confswap/test.conf.out" should contain:
16
- """
17
- giblets
18
- """
19
- Scenario: Test config file specifying the output
20
- When I run `confswap --output=/tmp/confswap/different.conf /tmp/confswap/test.conf`
21
- Then a file named "/tmp/confswap/different.conf" should exist
22
- And the file "/tmp/confswap/different.conf" should contain:
23
- """
24
- giblets
25
- """
26
-