confmake 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63d07e4c359feb5e4994796f69470a378aee3ad7
4
+ data.tar.gz: 54edb551976c793489cd79d9132a72e4f6e34522
5
+ SHA512:
6
+ metadata.gz: 9ab54b672a6793db2b5ec89cceec45e3b860996f73daa50367d37a51c14c75d51e120e411a98611d54a5ec0bbe35b5b3030fb2a3b4310cc2039188c77767f1aa
7
+ data.tar.gz: 76648a6b0f5e26a54d912db3f37ee674d75020e48b38418ded876e38ae3e1b08ced96d74247376d94e009488c5637952eaa45ca7398ebdf07aa1d4a5d1869d0d
@@ -0,0 +1,53 @@
1
+ ## Ignore bundle lock
2
+ *.lock
3
+ examples
4
+
5
+ ## My little test files
6
+ test.conf
7
+ test.conf.out
8
+ test.out
9
+ test.txt
10
+ test.txt.out
11
+ test.yaml
12
+ sample.properties
13
+ pfile.txt
14
+ your.conf
15
+ spec/tmp/*
16
+
17
+ ## Ignore Vim swp
18
+ *.swp
19
+
20
+ *.gem
21
+ *.rbc
22
+ /.config
23
+ /coverage/
24
+ /InstalledFiles
25
+ /pkg/
26
+ /spec/reports/
27
+ /test/tmp/
28
+ /test/version_tmp/
29
+ /tmp/
30
+
31
+ ## Specific to RubyMotion:
32
+ .dat*
33
+ .repl_history
34
+ build/
35
+
36
+ ## Documentation cache and generated files:
37
+ /.yardoc/
38
+ /_yardoc/
39
+ /doc/
40
+ /rdoc/
41
+
42
+ ## Environment normalisation:
43
+ /.bundle/
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0"
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'clamp'
4
+
5
+ group :development, :test do
6
+ gem 'rake'
7
+ gem 'simplecov'
8
+ gem 'travis-lint'
9
+ gem 'rspec'
10
+ gem 'cucumber', '1.3.18'
11
+ gem 'guard-rspec'
12
+ gem 'aruba'
13
+ end
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,53 @@
1
+ # confmake [![Build Status](https://travis-ci.org/tlcowling/confmake.svg?branch=master)](https://travis-ci.org/tlcowling/confmake) [![Dependency Status](https://gemnasium.com/tlcowling/confmake.svg)](https://gemnasium.com/tlcowling/confmake) [![Code Climate](https://codeclimate.com/github/tlcowling/confmake/badges/gpa.svg)](https://codeclimate.com/github/tlcowling/confmake) [![Gem Version](https://badge.fury.io/rb/confmake.svg)](http://badge.fury.io/rb/confmake)
2
+
3
+ Confmake makes configuration files.
4
+
5
+ Its a command line tool which procedurally generates configuration files from a template.
6
+
7
+ So far, you can use
8
+ 1. Shell environment variables
9
+ 2. Property Lists
10
+
11
+ ## Usage
12
+
13
+ ``
14
+
15
+ ## Requirements
16
+
17
+ Requires ruby version >= 2.0
18
+
19
+ ## Developments
20
+
21
+ Please feel free to fork and whatnot...
22
+
23
+ Get the dependencies
24
+ ``bundle install``
25
+
26
+ Run the tests
27
+ ``rake spec``
28
+ ``rake features``
29
+
30
+ ## Thoughts?
31
+ ###The point?
32
+
33
+ I had the need to procedurally create configuration files when automating the deployment of particular services.
34
+
35
+ ###Why not just use sed and replace in some kind of bash script?
36
+
37
+ I originally used this approach but the script grew unwieldy, maybe I don't do good bash or maybe its just because of lack of testing. Whilst the benefit of a bash script is that it will run as-is on many systems, I find it better to treat a tool like this as software and therefore use the ecosystems already available to manage dependencies and deploy. I also enjoy programming in ruby :)
38
+
39
+ ## Tasks to do and improvement ideas
40
+
41
+ - ~~error message when config contains env variable that doesnt exist~~ Version 0.0.2
42
+ - ~~Read config from properties file~~ Version 0.0.4
43
+ - ~~verbose command~~ Version 0.0.5
44
+ - ~~YAML~~ Version 0.1.0
45
+
46
+ ### Roadmap
47
+ - JSON Version 0.1.1
48
+ - proper logging! 0.1.2
49
+
50
+ - improve verbose messages
51
+ - summary of what will change? dry-run maybe?
52
+ - diff on what has changed in a config if overwriting?
53
+
@@ -0,0 +1,22 @@
1
+ begin
2
+ require 'rubygems'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'cucumber'
6
+ require 'cucumber/rake/task'
7
+
8
+ Cucumber::Rake::Task.new(:features) do |t|
9
+ t.cucumber_opts = "features --format pretty"
10
+ end
11
+
12
+ RSpec::Core::RakeTask.new(:spec)
13
+
14
+ task :bump do
15
+ end
16
+
17
+ task :build => ['bump']
18
+ task :default => ['spec']
19
+ task :test => ['spec','features']
20
+
21
+ rescue LoadError
22
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ $: << File.join(File.dirname(__FILE__), "..", "lib")
5
+ require 'confmake'
6
+ require 'confmake/command'
7
+
8
+ exit(Confmake::Command.run || 0)
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'confmake'
3
+ s.version = '0.1.1'
4
+ s.licenses = ['APACHE2']
5
+ s.summary = "Command line tool to create configuration files"
6
+ s.description = "Create configuration files from templates using environment variables or property lists. Save as text, yaml and json"
7
+ s.authors = ["Tom Cowling"]
8
+ s.email = 'tom.cowling@gmail.com'
9
+ s.homepage = 'https://www.tlcowling.com/gems/confmake'
10
+ s.add_runtime_dependency 'clamp'
11
+ s.add_runtime_dependency 'thor'
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+ end
@@ -0,0 +1,31 @@
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
+ %{USER}
9
+ """
10
+ And a file named "test.properties" with:
11
+ """
12
+ TEST: not giblets
13
+ USER: not alsogiblets
14
+ """
15
+ Scenario: Single environment variable
16
+ When I run `confmake -p test.properties -e TEST=giblets -t test.conf test.conf.out`
17
+ Then a file named "test.conf.out" should exist
18
+ And the file "test.conf.out" should contain:
19
+ """
20
+ giblets
21
+ """
22
+
23
+ Scenario: Multiple environment variable
24
+ When I run `confmake -p test.properties -e TEST=giblets -e USER=alsogiblets -t test.conf test.conf.out`
25
+ Then a file named "test.conf.out" should exist
26
+ And the file "test.conf.out" should contain:
27
+ """
28
+ giblets
29
+ alsogiblets
30
+ """
31
+
@@ -0,0 +1,26 @@
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
+ %{USER}
9
+ """
10
+ Scenario: Single environment variable
11
+ When I run `confmake -e TEST=giblets --include-shell-vars -t test.conf test.conf.out`
12
+ Then a file named "test.conf.out" should exist
13
+ And the file "test.conf.out" should contain:
14
+ """
15
+ giblets
16
+ """
17
+
18
+ Scenario: Multiple environment variable
19
+ When I run `confmake -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
+
@@ -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 `confmake -p sample.properties -t config_template_for_properties.conf sample.conf`
23
+ Then a file named "sample.conf" should exist
24
+ And the file "sample.conf" should contain:
25
+ """
26
+ I like Giblets
27
+ """
@@ -0,0 +1,8 @@
1
+ Feature: Create a json file from the variables passed to confmake
2
+ Scenario: Use default name for yaml
3
+ When PENDING I run `confmake -e TEST=giblets -e array=[1,2,3] --json`
4
+ Then PENDING a file named "your.conf" should exist
5
+ And PENDING the file "your.conf" should contain:
6
+ """
7
+ "{"TEST":"giblets","array":[1,2,3]}"
8
+ """
@@ -0,0 +1,23 @@
1
+ Feature: Create a yaml file from the variables passed to confmake
2
+ Scenario: Use default name for yaml
3
+ When I run `confmake -e TEST=giblets -e array=[1,2,3] --yaml`
4
+ Then a file named "your.conf" should exist
5
+ And the file "your.conf" should contain:
6
+ """
7
+ ---
8
+ TEST: giblets
9
+ array:
10
+ - 1
11
+ - 2
12
+ - 3
13
+ """
14
+
15
+ Scenario: Specify yaml file name and ignore persisted shell variables
16
+ When I run `confmake -e TEST=giblets -e USER=alsogiblets --yaml test.yaml`
17
+ Then a file named "test.yaml" should exist
18
+ And the file "test.yaml" should contain:
19
+ """
20
+ TEST: giblets
21
+ USER: alsogiblets
22
+ """
23
+
@@ -0,0 +1,5 @@
1
+ require 'fileutils'
2
+ require 'aruba/cucumber'
3
+
4
+ tmp_dir = File.expand_path(File.dirname(__FILE__)) + '/../../tmp'
5
+ puts tmp_dir
@@ -0,0 +1,9 @@
1
+ Before do
2
+ end
3
+
4
+ After do
5
+ end
6
+
7
+ Given /^PENDING/ do
8
+ pending
9
+ end
@@ -0,0 +1,27 @@
1
+ Feature: Confswap should replace variables in config template with environment variables
2
+ Background:
3
+ Given a file named "config.template" 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 `confmake -t config.template --include-shell-vars config.template.out`
15
+ Then a file named "config.template.out" should exist
16
+ And the file "config.template.out" should contain:
17
+ """
18
+ giblets
19
+ """
20
+ Scenario: Force an overwrite of a config when a file already exists
21
+ Given a file named "existing.conf" with:
22
+ """
23
+ # This is an exising config
24
+ """
25
+ When I run `confmake -t config.template existing.conf`
26
+ Then a file named "existing.conf" should exist
27
+
@@ -0,0 +1,7 @@
1
+ Feature: See what version of confmake you're running
2
+ Scenario: Type confmake --version
3
+ Given I run `confmake --version`
4
+ Then the output should contain:
5
+ """
6
+ 0.1.1
7
+ """
@@ -0,0 +1,5 @@
1
+ require 'confmake/env_reader'
2
+ require 'confmake/conf_reader'
3
+ require 'confmake/property_reader'
4
+ require 'confmake/bash_parser'
5
+ require 'confmake/version'
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+
3
+ module Confmake
4
+ class BashParser
5
+ def self.parse_user_input user_input
6
+ parsed_input = {}
7
+ user_input.each { |k,v|
8
+ parsed_input[k.to_s] = self.parse v
9
+ }
10
+ parsed_input
11
+ end
12
+
13
+ def self.parse text
14
+ parsed_text = text
15
+ parsed_text = parse_as_array text if text.start_with? "["
16
+ parsed_text = parse_as_object text if text.start_with? "{"
17
+ parsed_text
18
+ end
19
+
20
+ private
21
+ def self.parse_as_array text
22
+ parse_as_json_string text
23
+ end
24
+
25
+ def self.parse_as_object text
26
+ parse_as_json_string text
27
+ end
28
+
29
+ def self.parse_as_json_string text
30
+ begin
31
+ return JSON.parse text
32
+ rescue JSON::ParserError
33
+ return text
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,146 @@
1
+ require 'clamp'
2
+ require 'confmake'
3
+ require 'yaml'
4
+ require 'json'
5
+
6
+ module Confmake
7
+ class Command < Clamp::Command
8
+
9
+ def initialize *args
10
+ @env_variables = {}
11
+ super *args
12
+ end
13
+
14
+ def help *args
15
+ return [
16
+ "This is confmake version #{Confmake::VERSION}",
17
+ super
18
+ ].join("\n")
19
+ end
20
+
21
+ def run *args
22
+ super *args
23
+ end
24
+
25
+ def execute
26
+ if version?
27
+ puts Confmake::VERSION
28
+ return 0
29
+ end
30
+
31
+ if yaml?
32
+ save_as_yaml()
33
+ return 0
34
+ end
35
+
36
+ if json?
37
+ save_as_json()
38
+ return 0
39
+ end
40
+
41
+ if configuration_filename.nil?
42
+ puts 'Specify a template file or use --help for usage information'
43
+ return 0
44
+ end
45
+
46
+ if File.exists? configuration_filename
47
+ swap_config configuration_filename
48
+ return 0
49
+ else
50
+ puts "Error: Configuration template file with name #{configuration_filename} does not exist"
51
+ return 1
52
+ end
53
+ end
54
+
55
+ def gather_variables
56
+ @env_variables = Confmake::EnvironmentVariableReader.read_variables if include_persisted_shell_variables?
57
+
58
+ if !property_file.nil? and File.exists? property_file
59
+ @env_variables = Confmake::PropertyFileVariableReader.read_variables_from_file property_file
60
+ puts @env_variables
61
+ end
62
+
63
+ process_envvars() if envvars
64
+ end
65
+
66
+ def swap_config configuration_filename
67
+ output_filename_default = configuration_filename + '.out' if output_filename.nil?
68
+
69
+ configuration_template = Confmake::ConfigurationFileReader.read configuration_filename
70
+ gather_variables()
71
+
72
+ begin
73
+ output = configuration_template % @env_variables
74
+ rescue KeyError => error
75
+ puts "#{error.message}. Your configuration requires this variable, but no environment variable was found or specified."
76
+ exit(1)
77
+ end
78
+
79
+ write_file output, output_filename || output_filename_default
80
+ end
81
+
82
+ def process_envvars
83
+ envvars.each { |envvar|
84
+ key_and_value = envvar.split(/=/, 2)
85
+ key = key_and_value.first
86
+ value = key_and_value.last
87
+ if key_and_value.count != 2
88
+ puts "Invalid envvar specified #{key} and #{value}"
89
+ return 1
90
+ end
91
+
92
+ @env_variables[key.to_sym]=value
93
+ }
94
+
95
+ if verbose?
96
+ puts "Environment variables:"
97
+ @env_variables.each { |var|
98
+ puts "#{var.first} = #{var.last}"
99
+ }
100
+ end
101
+ end
102
+
103
+ def save_as_yaml
104
+ @env_variables = parse_envvars()
105
+ puts "Writing #{output_filename}"
106
+ env_variables_yaml = @env_variables.to_yaml
107
+ write_file env_variables_yaml, output_filename
108
+ end
109
+
110
+ def save_as_json
111
+ @env_variables = parse_envvars()
112
+ puts "Writing #{output_filename}"
113
+ env_variables_json = @env_variables.to_json
114
+ write_file env_variables_json, output_filename
115
+ end
116
+
117
+ def parse_envvars
118
+ gather_variables()
119
+ parsed_variables = Confmake::BashParser.parse_user_input @env_variables
120
+ puts "Interpreted user variables: #{parsed_variables}" if verbose?
121
+ parsed_variables
122
+ end
123
+
124
+ def write_file output_file_contents, output_filename
125
+ return File.write output_filename, output_file_contents unless File.exists? output_filename
126
+
127
+ if File.exists? output_filename and force?
128
+ puts "Overwriting #{output_filename}..."
129
+ File.write output_filename, output_file_contents
130
+ else
131
+ puts "#{output_filename} already exists, use the --force flag to overwrite"
132
+ end
133
+ end
134
+
135
+ option ['-t', '--template'], "TEMPLATE PATH", 'The path to the configuration template', :attribute_name => :configuration_filename
136
+ option ['-p', '--property-file'], "FILE PATH", 'A path to a property file to use for your template variables', :attribute_name => :property_file
137
+ option ['-e', '--envvar'], "VARIABLE", 'Specify one or more additional environment variables in the form KEY=VALUE', :multivalued => true, :attribute_name => :envvars
138
+ option ['-f','--force'], :flag, "Overwrite file if it already exists", :attribute_name => :force
139
+ option ['-v', '--version'], :flag, "The version of confmake you are running", :attribute_name => :version
140
+ option ['--verbose'], :flag, "Be more verbose"
141
+ option ['-i', '--include-shell-vars'], :flag, "Include any variables part of the shell profile", :attribute_name => :include_persisted_shell_variables
142
+ option ['-y', '--yaml'], :flag, "Create yaml config from variables", :attribute_name => :yaml
143
+ option ['-j', '--json'], :flag, "Create json config from variables", :attribute_name => :json
144
+ parameter "[OUTPUT FILE]", "Path to the configuration file", :attribute_name => :output_filename, :default => "your.conf"
145
+ end
146
+ end
@@ -0,0 +1,7 @@
1
+ module Confmake
2
+ class ConfigurationFileReader
3
+ def self.read config_file_path
4
+ File.read config_file_path if File.exists? config_file_path
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Confmake
2
+ class EnvironmentVariableReader
3
+ def self.read_variables
4
+ Hash[ENV.map { |key, value| [key.to_sym, value] }]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,45 @@
1
+ class Confmake::PropertyFileVariableReader
2
+ def self.read_variables_from_file file_path
3
+ contents = File.read file_path
4
+ self.parse_file_contents contents
5
+ end
6
+
7
+ private
8
+ def self.parse_file_contents contents
9
+ raise InvalidPropertyFileException unless self.valid_property_file_contents? contents
10
+ raw_file_contents = contents.split(/\n/)
11
+ variables = self.ignore_blank_or_comment_lines(raw_file_contents)
12
+ variables_hash = {}
13
+
14
+ variables.each { |variable_line|
15
+ split_variable_line = variable_line.split(/:/, 2)
16
+ variable_key = split_variable_line.first.to_sym
17
+ variable_value = split_variable_line.at(1).strip
18
+
19
+ variables_hash[variable_key] = variable_value
20
+ }
21
+
22
+ variables_hash
23
+ end
24
+
25
+ def self.ignore_blank_or_comment_lines file_contents
26
+ file_contents.reject{|v| v.empty? || self.comment_line?(v) }
27
+ end
28
+
29
+ def self.comment_line? line
30
+ line.start_with? '#'
31
+ end
32
+
33
+ def self.valid_property_file_contents? property_file_contents
34
+ file_contents_not_empty = !property_file_contents.empty?
35
+ file_contents_not_empty
36
+ end
37
+ end
38
+
39
+ class Confmake::PropertyFileVariableReader::InvalidPropertyFileException < Exception
40
+ def initalize message
41
+ super message
42
+ end
43
+ end
44
+
45
+
@@ -0,0 +1,3 @@
1
+ module Confmake
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'confmake/bash_parser'
2
+
3
+ describe 'Confmake::BashParser' do
4
+ describe 'normal string' do
5
+ it 'should not try to parse a normal string' do
6
+ normal_string = Confmake::BashParser.parse 'normal_string'
7
+ expect(normal_string).to be_kind_of String
8
+ expect(normal_string).to_not be_nil
9
+ end
10
+ end
11
+ describe 'Parsing arrays' do
12
+ it 'should attempt to convert a string starting with a [ to a ruby array object' do
13
+ parsed_array = Confmake::BashParser.parse '[1,2,3,4]'
14
+ expect(parsed_array).to be_kind_of Array
15
+ expect(parsed_array).to eq([1,2,3,4])
16
+ end
17
+
18
+ it 'should leave unparseable array as a string' do
19
+ invalid_array_string = Confmake::BashParser.parse '[1,2'
20
+ expect(invalid_array_string).to be_kind_of String
21
+ expect(invalid_array_string).to eq("[1,2")
22
+ end
23
+ end
24
+
25
+ describe 'Ruby hash' do
26
+ it 'should attempt to convert a string starting with a { to a ruby hash, but leave as a string if not possible' do
27
+ parsed_array = Confmake::BashParser.parse '{"a": "1", "b": "2", "c": "3", "d": "4"}'
28
+ expect(parsed_array).to be_kind_of Hash
29
+ expect(parsed_array).to eq({"a" => "1", "b" => "2", "c" => "3", "d" => "4"})
30
+ end
31
+ it 'should leave unparseable object as string' do
32
+ parsed_array = Confmake::BashParser.parse '{a: 1, b: 2, c: 3, d:'
33
+ expect(parsed_array).to be_kind_of String
34
+ expect(parsed_array).to eq('{a: 1, b: 2, c: 3, d:')
35
+ end
36
+ end
37
+
38
+ describe 'Whole User Input' do
39
+ it 'should parse envvars and convert array strings and hash strings to ruby object' do
40
+ user_input = {
41
+ :ARRAY => "[1,2,3,4]",
42
+ :TEST => "test",
43
+ :OBJECT => "{}",
44
+ }
45
+ parsed_input = Confmake::BashParser.parse_user_input user_input
46
+ expect(parsed_input).to be_kind_of Hash
47
+ expect(parsed_input).to eq({
48
+ "ARRAY" => [1,2,3,4],
49
+ "TEST" => "test",
50
+ "OBJECT" => {},
51
+ })
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ require 'confmake/conf_reader'
2
+ require_relative './spec_helper.rb'
3
+
4
+ describe 'Confmake::ConfigFileReader' do
5
+ before(:each) do
6
+ File.write(tmp_dir + '/test.txt', sample_config)
7
+ end
8
+
9
+ it 'should read the contents of a file specified' do
10
+ expect(Confmake::ConfigurationFileReader.read tmp_dir + '/test.txt').to eql(sample_config)
11
+ end
12
+
13
+ it 'should return nil if the file does not exist' do
14
+ expect(Confmake::ConfigurationFileReader.read '/path/does/not/exist.txt').to be_nil
15
+ end
16
+
17
+ after(:each) do
18
+ File.delete(tmp_dir + '/test.txt') if File.exists?(tmp_dir + '/test.txt')
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'confmake/env_reader'
2
+
3
+ describe 'Confmake::EnvironmentVariableReader' do
4
+ it 'should read environment variables and store them in a hash' do
5
+ env_variables = Confmake::EnvironmentVariableReader.read_variables
6
+ expect(env_variables).to be_instance_of Hash
7
+ end
8
+ it 'should store the environment variable FOO=bar in a hash' do
9
+ ENV['FOO']="bar"
10
+ expect(Confmake::EnvironmentVariableReader.read_variables).to include(:FOO => "bar")
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ require 'confmake/property_reader'
2
+
3
+ describe 'Confmake::PropertyFileVariableReader' do
4
+ it 'should split only on first colon and extra newlines' do
5
+ variables = "VAR1: 192.168.0.1\n\n\nVAR2: giblets\nVAR3: this should : : have: colons"
6
+ File.write tmp_dir + '/property_vars', variables
7
+ vars = Confmake::PropertyFileVariableReader.read_variables_from_file tmp_dir + '/property_vars'
8
+
9
+ expect(vars).to be_instance_of Hash
10
+ expect(vars.length).to equal(3)
11
+ expect(vars).to eq({
12
+ :VAR1 => '192.168.0.1',
13
+ :VAR2 => 'giblets',
14
+ :VAR3 => 'this should : : have: colons'
15
+ })
16
+
17
+ end
18
+
19
+ it 'should ignore comments in a property file' do
20
+ file_contents = "# this is a comment \nVAR1: 192.168.0.1\n\n\nVAR2: giblets\nVAR3: this should : : have: colons"
21
+ File.write tmp_dir + '/property_file_with_comments', file_contents
22
+ vars = Confmake::PropertyFileVariableReader.read_variables_from_file tmp_dir + '/property_file_with_comments'
23
+ expect(vars.length).to eq(3)
24
+ expect(vars).to eq({
25
+ :VAR1 => '192.168.0.1',
26
+ :VAR2 => 'giblets',
27
+ :VAR3 => 'this should : : have: colons'
28
+ })
29
+ end
30
+
31
+ it 'should warn for invalid property file when property file is empty' do
32
+ file_contents = ""
33
+ File.write tmp_dir + '/invalid_property_file', file_contents
34
+ expect {
35
+ Confmake::PropertyFileVariableReader.read_variables_from_file tmp_dir + '/invalid_property_file'
36
+ }.to raise_exception Confmake::PropertyFileVariableReader::InvalidPropertyFileException
37
+ end
38
+
39
+ pending 'should warn for invalid property file when property file is invalid' do
40
+ file_contents = "# this is a comment \nVAR1: "
41
+ File.write tmp_dir + '/invalid_property_file', file_contents
42
+ expect {
43
+ Confmake::PropertyFileVariableReader.read_variables_from_file tmp_dir + '/invalid_property_file'
44
+ }.to raise_exception Confmake::PropertyFileVariableReader::InvalidPropertyFileException
45
+ end
46
+ end
@@ -0,0 +1,20 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ def sample_config
5
+ [ "# This is a sample config",
6
+ "# A bunch of lines that will be in a file",
7
+ "# Because thats what its all about",
8
+ "test=%{ITEM1}",
9
+ "test2=%{ITEM2}",
10
+ "test3=%{ITEM3}"
11
+ ].join("\n")
12
+ end
13
+
14
+ def sample_variables
15
+ { :ITEM1 => "localhost", :ITEM2 => "8.8.8.8", :ITEM3 => 1 }
16
+ end
17
+
18
+ def tmp_dir
19
+ File.expand_path(File.dirname(__FILE__)) + '/tmp'
20
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: confmake
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tom Cowling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: clamp
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Create configuration files from templates using environment variables
42
+ or property lists. Save as text, yaml and json
43
+ email: tom.cowling@gmail.com
44
+ executables:
45
+ - confmake
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - Guardfile
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - bin/confmake
57
+ - confmake.gemspec
58
+ - features/env_variable_should_overwrite_properties.feature
59
+ - features/pass_environment_variables_with_command_line.feature
60
+ - features/property_file.feature
61
+ - features/save_conf_json.feature
62
+ - features/save_conf_yaml.feature
63
+ - features/support/env.rb
64
+ - features/support/hooks.rb
65
+ - features/variable_replace.feature
66
+ - features/version.feature
67
+ - lib/confmake.rb
68
+ - lib/confmake/bash_parser.rb
69
+ - lib/confmake/command.rb
70
+ - lib/confmake/conf_reader.rb
71
+ - lib/confmake/env_reader.rb
72
+ - lib/confmake/property_reader.rb
73
+ - lib/confmake/version.rb
74
+ - spec/bash_parser_spec.rb
75
+ - spec/conf_reader_spec.rb
76
+ - spec/env_reader_spec.rb
77
+ - spec/property_reader_spec.rb
78
+ - spec/spec_helper.rb
79
+ - spec/tmp/.gitkeep
80
+ homepage: https://www.tlcowling.com/gems/confmake
81
+ licenses:
82
+ - APACHE2
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.5
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Command line tool to create configuration files
104
+ test_files:
105
+ - features/env_variable_should_overwrite_properties.feature
106
+ - features/pass_environment_variables_with_command_line.feature
107
+ - features/property_file.feature
108
+ - features/save_conf_json.feature
109
+ - features/save_conf_yaml.feature
110
+ - features/support/env.rb
111
+ - features/support/hooks.rb
112
+ - features/variable_replace.feature
113
+ - features/version.feature
114
+ - spec/bash_parser_spec.rb
115
+ - spec/conf_reader_spec.rb
116
+ - spec/env_reader_spec.rb
117
+ - spec/property_reader_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/tmp/.gitkeep