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 +4 -4
- data/.gitignore +2 -0
- data/Gemfile +1 -1
- data/confswap.gemspec +1 -1
- data/features/pass_environment_variables_with_command_line.feature +17 -0
- data/features/property_file.feature +27 -0
- data/features/support/env.rb +3 -0
- data/features/support/hooks.rb +0 -2
- data/features/variable_replace.feature +34 -0
- data/features/version.feature +1 -1
- data/lib/confswap/command.rb +45 -24
- data/lib/confswap/version.rb +1 -1
- data/spec/property_reader_spec.rb +3 -7
- metadata +8 -6
- data/features/overwrite_existing_file.feature +0 -3
- data/features/swap.feature +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf14a7b12e576ef8ed3a80def9604890cb741567
|
4
|
+
data.tar.gz: a2460ccf7e5e02b1759d2f549633529e849bab01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3d8797cd327f6314d0bb4d529efac96be10e590f4fd59f81dd5b33f2ed9e99a3cd83385cb839cfa9c7e3130526f1695b89343d6242b330059d26fd55317ce9f
|
7
|
+
data.tar.gz: 93491351e0d1b33ad9c2ff34cb66211abd6fcaf442356c11b9876a0ad88066c3a2f24d5af0585c3f4af5c57c43b5f03e02be268f6119309d3e5e6c79d7815cab
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/confswap.gemspec
CHANGED
@@ -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
|
+
"""
|
data/features/support/env.rb
CHANGED
data/features/support/hooks.rb
CHANGED
@@ -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
|
+
|
data/features/version.feature
CHANGED
data/lib/confswap/command.rb
CHANGED
@@ -4,8 +4,8 @@ require 'confswap'
|
|
4
4
|
module Confswap
|
5
5
|
class Command < Clamp::Command
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
super
|
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
|
19
|
-
super
|
18
|
+
def run *args
|
19
|
+
super *args
|
20
20
|
end
|
21
21
|
|
22
22
|
def execute
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
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
|
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
|
data/lib/confswap/version.rb
CHANGED
@@ -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/
|
12
|
-
vars = Confswap::PropertyFileVariableReader.read_variables_from_file 'tmp/
|
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.
|
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-
|
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/
|
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/
|
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/
|
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/
|
102
|
+
- features/variable_replace.feature
|
101
103
|
- features/version.feature
|
102
104
|
- spec/conf_reader_spec.rb
|
103
105
|
- spec/env_reader_spec.rb
|
data/features/swap.feature
DELETED
@@ -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
|
-
|