Created
December 7, 2016 01:41
-
-
Save temyers/795f3f89ac94e8be85b4f9e1c6f298c0 to your computer and use it in GitHub Desktop.
Script to convert a properties file in `key=value` form to use confd properties, for use in Docker containers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## | |
| ## This utility makes it easy to convert a properties file specified in the _file_to_update_ variable to use confd properties. | |
| ## Takes a properties file in the form: | |
| ## `key=value` | |
| ## | |
| ## It reads the file in and creates a new file with _.new_ extension | |
| ## Every property key is converted to a confd path, the property value becomes the default value for the confd parameter | |
| ## Optional properties, denoted by being commented out, e.g. `# key=value`, are converted to optional confd properties. | |
| ## | |
| ## Example: | |
| ## `property.key1=foo` => `property.key1={{getv "/property/key1" "foo"}}` | |
| ## `#property.optional=foo` | |
| ## => ```{{if exists "/property/optional" -}} | |
| ## property.optional={{getv "/property/optional" "foo"}}` | |
| ## {{end -}} | |
| ## ``` | |
| def isCommentOrBlank?(line) | |
| return line.start_with?('#') || line.strip.empty?() | |
| end | |
| file_to_update="path/to/file/to_update.properties" | |
| File.open("#{file_to_update}.new",'w') do |file| | |
| lines = File.open(file_to_update).read | |
| lines.each_line do |line| | |
| optionalProperty=/^#([^=]+)=(.+)$/ | |
| line.match(optionalProperty) do |m| | |
| name=m[1].strip | |
| value=m[2].strip | |
| env_property=name.gsub('.','/').downcase | |
| file.write("{{if exists \"/#{env_property}\" -}}\n") | |
| new_line="#{name} = {{getv \"/#{env_property}\" \"#{value}\"}}\n" | |
| file.write(new_line) | |
| file.write("{{end -}}\n") | |
| end | |
| if ( isCommentOrBlank?(line) ) then | |
| # Don't process comments | |
| file.write(line) | |
| else | |
| name_value = line.split('=') | |
| name=name_value[0].strip | |
| value=name_value[1].strip | |
| env_property=name.gsub('.','/').downcase | |
| new_line="#{name} = {{getv \"/#{env_property}\" \"#{value}\"}}\n" | |
| file.write(new_line) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment