mini_hiera 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
+ SHA256:
3
+ metadata.gz: 58140d759970bf1df9fe100deb1038373381332454f0166c6bb5180db2737475
4
+ data.tar.gz: 233d512135e1810c0f0b7bd8f0ea5b978f830f453ae29fa2c53d303f51937c55
5
+ SHA512:
6
+ metadata.gz: ad21f9f936a310d972cab8f87ff46d0da844b4c96d516694a94ff5f4849b3964e810c1739b53dcf5bdcfe6795636ed472071a758b9417fcf2180225be434f17d
7
+ data.tar.gz: 5e69af08a797e2812e18ff73853a7069946112f6036ec97c88b9e01392cc246d19493158d023aa73dd327d68a5e1479e0a488c2ff530c0c947a41f9588c8b574
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/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.7
5
+ before_install: gem install bundler -v 1.16.2
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 mini_hiera.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mini_hiera (0.1.0)
5
+ deep_merge (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ deep_merge (1.2.1)
11
+ diff-lcs (1.3)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ mini_hiera!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Thomas Haggett
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,59 @@
1
+ # Mini Hiera
2
+
3
+ Puppet's Hiera library felt usable as a free-standing hierarchial key lookup library, but they pulled the source back into the Puppet codebase, thereby limiting any potential use outside Puppet itself.
4
+
5
+ This is a free-standing gem that clean-room implements the behaviour of Hiera such that it can be used in any project.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mini_hiera'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ The simplest way to use this gem is to provide a directory of `YAML` files and configure a hierarchy in code:
18
+
19
+ ```yaml
20
+ config/device/device1.yaml:
21
+ test_key: "loaded from device/device1.yaml"
22
+
23
+ config/device/device2.yaml:
24
+ test_key: "loaded from device/device2.yaml"
25
+
26
+ config/site/site1.yaml
27
+ test_key: "loaded from site/site1.yaml"
28
+
29
+ config/type/typeA.yaml
30
+ test_key: "loaded from type/typeA.yaml"
31
+
32
+ config/common.yaml
33
+ test_key: "loaded from common.yaml"
34
+ ```
35
+
36
+ ```ruby
37
+ config = MiniHiera.new('path/to/config', hierarchy: [
38
+ 'device/%{name}',
39
+ 'site/%{site}',
40
+ 'type/%{type}',
41
+ 'common'
42
+ ])
43
+ ```
44
+
45
+ Specific usage of this library can be found demonstrated in the `spec/usage_spec.rb`, which can be executed to ensure it's still accurate!
46
+
47
+ ## Development
48
+
49
+ 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.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshado/mini_hiera.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mini_hiera"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/mini_hiera.rb ADDED
@@ -0,0 +1,132 @@
1
+ require "mini_hiera/version"
2
+ require 'deep_merge'
3
+
4
+ module Enumerable
5
+ def find_value(ifnone=nil, &block)
6
+ each { |*v| o = block.call(*v) and return(o) }
7
+ ifnone
8
+ end
9
+ end
10
+
11
+ class MiniHiera
12
+ CircularReferenceError = Class.new(RuntimeError)
13
+ KeyNotFoundError = Class.new(RuntimeError)
14
+
15
+ attr_reader :config, :options
16
+ def initialize(config, options={})
17
+ @config = config
18
+ @options = options
19
+ end
20
+
21
+ class Context
22
+ def initialize(hiera, data, options)
23
+ @hiera, @data, @options = hiera, data, options
24
+ end
25
+
26
+ def postprocess(value)
27
+ if value.is_a?(String)
28
+ value.gsub(/\%\{([^}]+)\}/) do |match|
29
+ fetch($1)
30
+ end
31
+ elsif value.is_a?(Hash)
32
+ Hash[value.map { |k,v| [k, postprocess(v)] }]
33
+ elsif value.is_a?(Array)
34
+ value.map { |v| postprocess(v) }
35
+ else
36
+ value
37
+ end
38
+ end
39
+
40
+ def error_message_suffix
41
+ if !!(v = Thread.current[:interpolation_stack]) && !v.empty?
42
+ "whilst interpolating keys #{v.reverse.join(", ")}"
43
+ end
44
+ end
45
+
46
+ def detect_circular_interpolation(key)
47
+ Thread.current[:interpolation_stack] ||= []
48
+ raise CircularReferenceError, "Circular reference when resolving key '#{key}'" if Thread.current[:interpolation_stack].include?(key)
49
+ Thread.current[:interpolation_stack] << key
50
+ yield
51
+ ensure
52
+ Thread.current[:interpolation_stack].pop
53
+ end
54
+
55
+ def hiera_data
56
+ @hiera_data ||= @hiera.resolve(@data).inject({}) { |a,v| a.deep_merge(v, @options.fetch(:deep_merge, {})) }
57
+ end
58
+
59
+ DefaultObject = Object.new
60
+ def fetch(key, default=DefaultObject, &default_block)
61
+ key = key.to_s
62
+
63
+ raise ArgumentError, "default value and block specified" if default != DefaultObject && default_block
64
+
65
+ default_block ||= lambda { default == DefaultObject ? raise(KeyNotFoundError, [" ** Unknown key '#{key}'", error_message_suffix].join(" ")) : default }
66
+
67
+ default_block_wrapper = lambda { |*_| default_block.arity == 0 ? default_block.call : default_block.call(key) }
68
+
69
+ data = hiera_data
70
+
71
+ key.split(".").each do |k|
72
+ if data.is_a?(Array)
73
+ raise TypeError, "no implicit conversion of String into Integer" unless k.to_i.to_s == k
74
+ data = data[k.to_i]
75
+ elsif data.is_a?(Hash)
76
+ data = data.fetch(k, &default_block_wrapper)
77
+ else
78
+ data = default_block_wrapper.call
79
+ break
80
+ end
81
+ end
82
+
83
+ detect_circular_interpolation(key) do
84
+ postprocess(data)
85
+ end
86
+ end
87
+
88
+ def [](key)
89
+ fetch(key)
90
+ end
91
+
92
+ def has_key?(key)
93
+ fetch(key)
94
+ true
95
+ rescue MiniHiera::KeyNotFoundError
96
+ false
97
+ end
98
+ end
99
+
100
+ def replace(string, dictionary)
101
+ string.gsub(/\%\{([^}]+)\}/) do |match|
102
+ dictionary.fetch($1, match)
103
+ end
104
+ end
105
+
106
+ CACHE = {}
107
+
108
+ def self.cache(filename)
109
+ if CACHE.keys.include?(filename)
110
+ CACHE[filename]
111
+ else
112
+ CACHE[filename] = yield
113
+ end
114
+ end
115
+
116
+ TYPES = {
117
+ "yaml" => Proc.new { |filename| cache(filename) { YAML.load(File.read(filename), filename) } },
118
+ "json" => Proc.new { |filename| cache(filename) { JSON.load(File.read(filename), filename) } }
119
+ }
120
+
121
+ def load_path(path)
122
+ TYPES.map { |s,proc| ["#{path}.#{s}", proc] }.find_value { |filename,proc| proc.call(filename, binding) if File.exist?(filename) }
123
+ end
124
+
125
+ def resolve(data)
126
+ [data] + @options[:hierarchy].map { |path| load_path(File.join(@config, replace(path, data))) }.compact
127
+ end
128
+
129
+ def context(data={}, options={})
130
+ Context.new(self, data, @options.deep_merge(options))
131
+ end
132
+ end
@@ -0,0 +1,3 @@
1
+ class MiniHiera
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mini_hiera/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mini_hiera"
8
+ spec.version = MiniHiera::VERSION
9
+ spec.authors = ["Thomas Haggett"]
10
+ spec.email = ["thomas@haggett.org"]
11
+
12
+ spec.summary = %q{Hierarchial key/value configuration}
13
+ spec.description = %q{Library to allows a key to be resolved via a hierarchy of context-specific values}
14
+ spec.homepage = "https://thomas.haggett.org/"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency 'deep_merge', '~> 1.0'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_hiera
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Haggett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: deep_merge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Library to allows a key to be resolved via a hierarchy of context-specific
70
+ values
71
+ email:
72
+ - thomas@haggett.org
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/mini_hiera.rb
88
+ - lib/mini_hiera/version.rb
89
+ - mini_hiera.gemspec
90
+ homepage: https://thomas.haggett.org/
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.7.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Hierarchial key/value configuration
114
+ test_files: []