confswap 0.0.4 → 0.0.5
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/README.md +42 -15
- data/confswap.gemspec +1 -1
- data/features/pass_environment_variables_with_command_line.feature +10 -1
- data/features/property_file.feature +1 -1
- data/features/variable_replace.feature +5 -12
- data/features/version.feature +1 -1
- data/lib/confswap/command.rb +33 -29
- data/lib/confswap/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bec574be3e75c3c3c48fc0d197b14950858589fa
|
4
|
+
data.tar.gz: 767d089e13677553e48d6794f396a531cec85a29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48becfecd91dabdb948fcbbdbaecc18da43c1c6f53e8677958c13007b74ab6d81cae3023ad5ef66e9acb20822b76625eda20e8baa089d697d5acfa2495ce1fcc
|
7
|
+
data.tar.gz: 8f45a1492502985c4dbe6a6373e833ac6997e67ab81066cbefe362423e87eaccce679621263012b139886cd7c05f0a42a51707403b440ffc17faeadde3a1e392
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# confswap [](https://travis-ci.org/tlcowling/confswap) [](https://gemnasium.com/tlcowling/confswap) [](https://codeclimate.com/github/tlcowling/confswap) [](http://badge.fury.io/rb/confswap)
|
2
2
|
:cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow: :cow2: :cow:
|
3
3
|
|
4
|
-
|
5
|
-
|
6
4
|
A simple command line tool to take a configuration file with template placeholders and replace those placeholders with environment variables.
|
7
5
|
|
8
6
|
The variables can come explicitly from the command, or read from a properties file.
|
@@ -11,23 +9,50 @@ The variables can come explicitly from the command, or read from a properties fi
|
|
11
9
|
|
12
10
|
Configuration files come in different shapes and forms, confswap doesn't care if its toml or yaml
|
13
11
|
|
12
|
+
At the moment, you can run confswap in a few different ways. By default it uses any environment varables that are available as it runs.
|
13
|
+
|
14
|
+
So, given the following template
|
15
|
+
|
16
|
+
```
|
17
|
+
# sample1.template
|
18
|
+
# Template to use environment variables
|
19
|
+
user=%{USER}
|
20
|
+
lang=%{LANG}
|
21
|
+
```
|
22
|
+
|
23
|
+
### Environment Variables
|
24
|
+
|
25
|
+
```confswap --output=sample1.conf sample1.template```
|
26
|
+
|
27
|
+
will use the USER and LANG variables in your shell to populate the template. You can use ``env`` to see your current environment variables.
|
28
|
+
|
29
|
+
additionally you can add additional variables to use, or overwrite extisting env variables during the running of the command via
|
30
|
+
|
31
|
+
1. The envvar flag (which takes multiple variables which must be separated by an = sign
|
32
|
+
|
33
|
+
```confswap --output=sample2.conf --envvar USER=something --envvar LANG=en_US.UTF-8 sample1.template
|
34
|
+
|
35
|
+
2. explicit setting:
|
36
|
+
|
37
|
+
```USER=someoneelse LANG=en_IE.UTF-8 confswap --output=sample3.conf sample1.template```
|
38
|
+
|
39
|
+
### Properties File
|
40
|
+
|
41
|
+
You can specify a property file containing keys and values such as
|
14
42
|
```
|
15
|
-
#
|
43
|
+
# sample.properties
|
16
44
|
|
17
|
-
|
45
|
+
USER: anotherone
|
46
|
+
```
|
18
47
|
|
19
|
-
|
20
|
-
splenetic_juice_factor=%{SPLENETIC_JUICE_FACTOR}
|
21
|
-
```
|
48
|
+
Then read it using
|
22
49
|
|
23
|
-
|
50
|
+
```confswap --output=sample3.conf -p sample.properties sample1.template```
|
24
51
|
|
25
|
-
confswap
|
26
52
|
|
27
53
|
## Requirements
|
28
54
|
|
29
|
-
Requires ruby version
|
30
|
-
Bundle install the gems
|
55
|
+
Requires ruby version >= 2.0
|
31
56
|
|
32
57
|
## Developments
|
33
58
|
|
@@ -38,8 +63,12 @@ Get the dependencies
|
|
38
63
|
|
39
64
|
Run the tests
|
40
65
|
``rake spec``
|
66
|
+
``rake features``
|
41
67
|
|
42
68
|
## Thoughts?
|
69
|
+
The point?
|
70
|
+
|
71
|
+
I had the need to procedurally create configuration files when automating the deployment of particular services.
|
43
72
|
|
44
73
|
Why not just use sed and replace in some kind of bash script?
|
45
74
|
|
@@ -47,12 +76,10 @@ I originally used this approach but the script grew unwieldy, maybe I don't do g
|
|
47
76
|
|
48
77
|
## Tasks to do and improvement ideas
|
49
78
|
|
50
|
-
- ~~error message when config contains env variable that doesnt exist~~
|
51
|
-
- ~~
|
52
|
-
- Read config from properties file
|
79
|
+
- ~~error message when config contains env variable that doesnt exist~~ Version 0.0.2
|
80
|
+
- ~~Read config from properties file~~ Version 0.0.4
|
53
81
|
- verbose command
|
54
82
|
- summary of what will change? dry-run maybe?
|
55
83
|
- diff on what has changed in a config if overwriting?
|
56
84
|
- test command.rb
|
57
|
-
- document...
|
58
85
|
|
data/confswap.gemspec
CHANGED
@@ -5,9 +5,10 @@ Feature: Add environment variables with command line
|
|
5
5
|
# This is a test config
|
6
6
|
# that has some variables in it
|
7
7
|
%{TEST}
|
8
|
+
%{USER}
|
8
9
|
"""
|
9
10
|
Scenario: Single environment variable
|
10
|
-
When I run `confswap -e TEST=giblets test.conf`
|
11
|
+
When I run `confswap -e TEST=giblets -t test.conf test.conf.out`
|
11
12
|
Then a file named "test.conf.out" should exist
|
12
13
|
And the file "test.conf.out" should contain:
|
13
14
|
"""
|
@@ -15,3 +16,11 @@ Feature: Add environment variables with command line
|
|
15
16
|
"""
|
16
17
|
|
17
18
|
Scenario: Multiple environment variable
|
19
|
+
When I run `confswap -e TEST=giblets -e USER=alsogiblets -t test.conf test.conf.out`
|
20
|
+
Then a file named "test.conf.out" should exist
|
21
|
+
And the file "test.conf.out" should contain:
|
22
|
+
"""
|
23
|
+
giblets
|
24
|
+
alsogiblets
|
25
|
+
"""
|
26
|
+
|
@@ -19,7 +19,7 @@ Feature: Confswap should read template variables from a properties file
|
|
19
19
|
# This is a descriptive comment
|
20
20
|
VAR3: so many giblets
|
21
21
|
"""
|
22
|
-
When I run `confswap -p sample.properties -
|
22
|
+
When I run `confswap -p sample.properties -t config_template_for_properties.conf sample.conf`
|
23
23
|
Then a file named "sample.conf" should exist
|
24
24
|
And the file "sample.conf" should contain:
|
25
25
|
"""
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Feature: Confswap should replace variables in config template with environment variables
|
2
2
|
Background:
|
3
|
-
Given a file named "
|
3
|
+
Given a file named "config.template" with:
|
4
4
|
"""
|
5
5
|
# This is a test config
|
6
6
|
# that has some variables in it
|
@@ -11,16 +11,9 @@ Feature: Confswap should replace variables in config template with environment v
|
|
11
11
|
| TEST | giblets |
|
12
12
|
|
13
13
|
Scenario: Config file should be created with a default name and contain environment variables
|
14
|
-
When I run `confswap
|
15
|
-
Then a file named "
|
16
|
-
And the file "
|
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:
|
14
|
+
When I run `confswap -t config.template config.template.out`
|
15
|
+
Then a file named "config.template.out" should exist
|
16
|
+
And the file "config.template.out" should contain:
|
24
17
|
"""
|
25
18
|
giblets
|
26
19
|
"""
|
@@ -29,6 +22,6 @@ Feature: Confswap should replace variables in config template with environment v
|
|
29
22
|
"""
|
30
23
|
# This is an exising config
|
31
24
|
"""
|
32
|
-
When I run `confswap
|
25
|
+
When I run `confswap -t config.template existing.conf`
|
33
26
|
Then a file named "existing.conf" should exist
|
34
27
|
|
data/features/version.feature
CHANGED
data/lib/confswap/command.rb
CHANGED
@@ -4,11 +4,13 @@ require 'confswap'
|
|
4
4
|
module Confswap
|
5
5
|
class Command < Clamp::Command
|
6
6
|
|
7
|
+
@env_variables
|
8
|
+
|
7
9
|
def initialize *args
|
8
10
|
super *args
|
9
11
|
end
|
10
12
|
|
11
|
-
def help
|
13
|
+
def help *args
|
12
14
|
return [
|
13
15
|
"This is confswap version #{Confswap::VERSION}",
|
14
16
|
super
|
@@ -43,37 +45,18 @@ module Confswap
|
|
43
45
|
output_filename_default = configuration_filename + '.out' if output_filename.nil?
|
44
46
|
|
45
47
|
configuration_template = Confswap::ConfigurationFileReader.read configuration_filename
|
46
|
-
env_variables = Confswap::EnvironmentVariableReader.read_variables
|
47
|
-
|
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
|
48
|
+
@env_variables = Confswap::EnvironmentVariableReader.read_variables
|
49
|
+
|
50
|
+
process_envvars() if envvars
|
68
51
|
|
69
52
|
if !property_file.nil? and File.exists? property_file
|
70
53
|
puts 'pfile specified'
|
71
|
-
env_variables = Confswap::PropertyFileVariableReader.read_variables_from_file property_file
|
72
|
-
|
54
|
+
@env_variables = Confswap::PropertyFileVariableReader.read_variables_from_file property_file
|
55
|
+
puts @env_variables
|
73
56
|
end
|
74
57
|
|
75
58
|
begin
|
76
|
-
output = configuration_template % env_variables
|
59
|
+
output = configuration_template % @env_variables
|
77
60
|
rescue KeyError => error
|
78
61
|
puts "#{error.message}. Your configuration requires this variable, but no environment variable was found or specified."
|
79
62
|
exit(1)
|
@@ -82,6 +65,27 @@ module Confswap
|
|
82
65
|
write_file output, output_filename || output_filename_default
|
83
66
|
end
|
84
67
|
|
68
|
+
def process_envvars
|
69
|
+
envvars.each { |envvar|
|
70
|
+
key_and_value = envvar.split(/=/, 2)
|
71
|
+
key = key_and_value.first
|
72
|
+
value = key_and_value.last
|
73
|
+
if key_and_value.count != 2
|
74
|
+
puts "Invalid envvar specified #{key} and #{value}"
|
75
|
+
return 1
|
76
|
+
end
|
77
|
+
|
78
|
+
@env_variables[key.to_sym]=value
|
79
|
+
}
|
80
|
+
|
81
|
+
if verbose?
|
82
|
+
puts "Environment variables:"
|
83
|
+
@env_variables.each { |var|
|
84
|
+
puts "#{var.first} = #{var.last}"
|
85
|
+
}
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
85
89
|
def write_file output_file_contents, output_filename
|
86
90
|
return File.write output_filename, output_file_contents unless File.exists? output_filename
|
87
91
|
|
@@ -93,12 +97,12 @@ module Confswap
|
|
93
97
|
end
|
94
98
|
end
|
95
99
|
|
100
|
+
option ['-t', '--template'], "TEMPLATE PATH", 'The path to the configuration template', :attribute_name => :configuration_filename
|
96
101
|
option ['-p', '--property-file'], "FILE PATH", 'A path to a property file to use for your template variables', :attribute_name => :property_file
|
97
|
-
option ['-e', '--envvar'], "VARIABLE", 'Specify one or more additional environment variables', :multivalued => true, :attribute_name => :envvars
|
102
|
+
option ['-e', '--envvar'], "VARIABLE", 'Specify one or more additional environment variables in the form KEY=VALUE', :multivalued => true, :attribute_name => :envvars
|
98
103
|
option ['-f','--force'], :flag, "Overwrite file if it already exists", :attribute_name => :force
|
99
104
|
option ['-v', '--version'], :flag, "The version of confswap you are running", :attribute_name => :version
|
100
|
-
option ['-o', '--output'], "FILE PATH", "Specifies the filepath for the file", :attribute_name => :output_filename
|
101
105
|
option ['--verbose'], :flag, "Be more verbose"
|
102
|
-
parameter "[
|
106
|
+
parameter "[OUTPUT FILE]", "Path to the configuration file", :attribute_name => :output_filename, :default => "your.conf"
|
103
107
|
end
|
104
108
|
end
|
data/lib/confswap/version.rb
CHANGED
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.5
|
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-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: clamp
|