envin 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c721e50cc691bbb5fffe96555a917897b781da04
4
+ data.tar.gz: 47cb12e3e24688667c38785cbfd9029a780a1ec1
5
+ SHA512:
6
+ metadata.gz: 919e6d64a24a9f7c1942e87ec9510d542db3f6248f0d02a0321ad2792b683421d082b393833f61778aabbe93c4d649fe9a5769d324d4dd6ef2f0d4aacd0a5445
7
+ data.tar.gz: 6dae73e205a1f1269c3ee40d14bb83d50ce400d96922ebadee3bb28456f9bbcf5d331360d660976bbf042c6d02c25c36cce1d4bef2b81ee8bed6a47c8b55f3ca
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in envin.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ envin (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ envin!
15
+
16
+ BUNDLED WITH
17
+ 1.16.0.pre.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Zidni Mubarock
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Envin
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/envin`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'envin'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install envin
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/envin.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/bin/envin ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/envin/cli'
3
+
4
+ begin
5
+ Envin::CLI.parse
6
+ Envin::CLI.run
7
+ rescue => e
8
+ STDERR.puts e.message
9
+ STDERR.puts e.backtrace.join("\n") if $DEBUG
10
+ exit 1
11
+ end
data/envin.gemspec ADDED
@@ -0,0 +1,22 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "envin/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "envin"
8
+ spec.version = Envin::VERSION
9
+ spec.authors = ["Zidni Mubarock"]
10
+ spec.email = ["zidmubarock@gmail.com"]
11
+
12
+ spec.summary = %q{Build yaml file from OS environment variables}
13
+ spec.description = %q{Build yaml file from OS environment variables}
14
+ spec.homepage = "https://github.com/barockok"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.executables = ["envin"]
21
+ spec.require_paths = ["lib"]
22
+ end
data/lib/envin/cli.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'envin/converter'
2
+ require 'optparse'
3
+ module Envin
4
+ module CLI
5
+ extend self
6
+ attr_reader :option
7
+ def parse(args=ARGV)
8
+ opts = {
9
+ rootelement: "production"
10
+ }
11
+
12
+ parser = OptionParser.new do |opt_parser|
13
+ opt_parser.on '-f', '--filepath FILENAME', 'file path' do |arg|
14
+ opts[:filepath] = File.expand_path(arg)
15
+ end
16
+
17
+ opt_parser.on '-p', '--prefix PREFIX', 'env prefix' do |arg|
18
+ opts[:prefix] = arg
19
+ end
20
+
21
+ opt_parser.on '-r', '--root-element ELEMENT', 'root element' do |arg|
22
+ opts[:rootelement] = arg
23
+ end
24
+ end
25
+
26
+ parser.on_tail "-h", "--help", "Show help" do
27
+ puts parser
28
+ die 1
29
+ end
30
+
31
+ parser.parse!(args)
32
+ @option = opts
33
+ end
34
+
35
+ def run
36
+ if !option[:filepath] || !option[:prefix]
37
+ puts "File path & prefix is required"
38
+ exit(1)
39
+ end
40
+ Converter.overwrite(option[:filepath], option[:prefix], option[:rootelement])
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,67 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+
4
+ module Envin
5
+ module Converter
6
+ extend self
7
+ def load_yaml file, root_element="production"
8
+ return {} if !File.exists?(file)
9
+ file_content = ERB.new(File.read(file)).result
10
+ content = YAML.load(file_content)
11
+ (!root_element ? content : content[root_element]) || {}
12
+ end
13
+
14
+ def build_child_split(key_split, v, coll=[], level=0)
15
+ key_identifier = key_split.take(level + 1).join('-')
16
+ lines = ""
17
+ if !coll.include?(key_identifier) && key_split.length != level + 1
18
+ coll << key_identifier
19
+ lines += "#{' ' * level}#{key_split[level].downcase}:\n"
20
+ child_line, _ = build_child_split(key_split, v,coll, level+1)
21
+ lines += child_line
22
+ else
23
+ lines += "#{' ' * (key_split.length - 1) }#{key_split.last.downcase}: #{v}\n"
24
+ end
25
+ return lines, coll
26
+ end
27
+
28
+ def build_yaml_from_env prefix
29
+ env_prefix = ENV.select{|k,v| k =~ /#{prefix}/ }
30
+ coll = []
31
+ lines = ""
32
+ Hash[env_prefix.sort].each do |key, v|
33
+ key_split = key.split("__")
34
+ if key_split.length == 1
35
+ lines += "#{key.gsub(prefix, '').downcase}: #{v}\n"
36
+ elsif key_split.length > 1
37
+ key_split[0] = key_split[0].gsub(prefix, '').downcase
38
+ child_line, coll = build_child_split(key_split, v, coll)
39
+ lines += child_line
40
+ end
41
+ end
42
+
43
+ if lines != ""
44
+ YAML.load(lines)
45
+ else
46
+ return {}
47
+ end
48
+ end
49
+
50
+ def overwrite file, prefix, root_element="production"
51
+ file_exist = File.exist?(file)
52
+ config_content = file_exist ? load_yaml(file, root_element) : {}
53
+ config_env = build_yaml_from_env(prefix)
54
+
55
+ root = {}
56
+ if root_element
57
+ root[root_element] = config_content.merge(config_env)
58
+ else
59
+ root = config_content.merge(config_env)
60
+ end
61
+ new_file_content = YAML.dump(root)
62
+
63
+ File.delete(file) if file_exist
64
+ File.write(file, new_file_content)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Envin
2
+ VERSION = "0.1.0"
3
+ end
data/lib/envin.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "envin/version"
2
+
3
+ module Envin
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: envin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zidni Mubarock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Build yaml file from OS environment variables
14
+ email:
15
+ - zidmubarock@gmail.com
16
+ executables:
17
+ - envin
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - bin/envin
27
+ - envin.gemspec
28
+ - lib/envin.rb
29
+ - lib/envin/cli.rb
30
+ - lib/envin/converter.rb
31
+ - lib/envin/version.rb
32
+ homepage: https://github.com/barockok
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.6.12
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Build yaml file from OS environment variables
56
+ test_files: []