bogo-config 0.2.2 → 0.3.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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +6 -0
- data/CONTRIBUTING.md +10 -14
- data/LICENSE +2 -2
- data/bogo-config.gemspec +3 -2
- data/lib/bogo/config/configuration.rb +5 -0
- data/lib/bogo/config/version.rb +6 -0
- data/lib/bogo/config.rb +336 -0
- data/lib/bogo-config/config.rb +1 -333
- data/lib/bogo-config/configuration.rb +1 -5
- data/lib/bogo-config/version.rb +1 -6
- data/lib/bogo-config.rb +1 -5
- metadata +24 -9
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 | 
            -
             | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 7da489503a17b15365f4cd593463a8ac802094372a87206f59a913260ce82be6
         | 
| 4 | 
            +
              data.tar.gz: 3fef45e894abbfa146f97b279525cd61a48d491c4b673f2358378daccb2c761c
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3751427127cc766c2ea37b1c5e47ceab0840f787c7ca6208e3d63e837ab8911132ed72922e5e7ab9fcac19e3dab89f80821325168d70a5c2938fe75f8b24e33b
         | 
| 7 | 
            +
              data.tar.gz: 82ecad0397121a70a174df6f6183a7caece1bc662abb1b192586105887c1166dd035f3e8cec559c9afa08a85d77131bb6783a107aa87e10f2af9dba4981c4308
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/CONTRIBUTING.md
    CHANGED
    
    | @@ -1,22 +1,18 @@ | |
| 1 1 | 
             
            # Contributing
         | 
| 2 2 |  | 
| 3 | 
            -
            ##  | 
| 3 | 
            +
            ## Fixes
         | 
| 4 4 |  | 
| 5 | 
            -
             | 
| 5 | 
            +
            Have a fix to some bug you want to submit? Well you're
         | 
| 6 | 
            +
            awesome. Please just include a description of the bug
         | 
| 7 | 
            +
            (or link to originating issue) and test coverage on the
         | 
| 8 | 
            +
            modifications.
         | 
| 6 9 |  | 
| 7 | 
            -
             | 
| 10 | 
            +
            ## New Features
         | 
| 8 11 |  | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
            ## Pull requests
         | 
| 14 | 
            -
             | 
| 15 | 
            -
            * https://github.com/spox/bogo-config/pulls
         | 
| 16 | 
            -
             | 
| 17 | 
            -
            Please base all pull requests of the `develop` branch. Merges to
         | 
| 18 | 
            -
            `master` only occur through the `develop` branch. Pull requests
         | 
| 19 | 
            -
            based on `master` will likely be cherry picked.
         | 
| 12 | 
            +
            Have a new feature you want to add? Well you're awesome
         | 
| 13 | 
            +
            too! It may be a good idea to submit an issue first to
         | 
| 14 | 
            +
            describe the desired feature and get any feedback. Please
         | 
| 15 | 
            +
            be sure to include tests.
         | 
| 20 16 |  | 
| 21 17 | 
             
            ## Issues
         | 
| 22 18 |  | 
    
        data/LICENSE
    CHANGED
    
    | @@ -1,4 +1,4 @@ | |
| 1 | 
            -
               Copyright  | 
| 1 | 
            +
               Copyright 2022 Chris Roberts
         | 
| 2 2 |  | 
| 3 3 | 
             
               Licensed under the Apache License, Version 2.0 (the "License");
         | 
| 4 4 | 
             
               you may not use this file except in compliance with the License.
         | 
| @@ -10,4 +10,4 @@ | |
| 10 10 | 
             
               distributed under the License is distributed on an "AS IS" BASIS,
         | 
| 11 11 | 
             
               WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         | 
| 12 12 | 
             
               See the License for the specific language governing permissions and
         | 
| 13 | 
            -
               limitations under the License.
         | 
| 13 | 
            +
               limitations under the License.
         | 
    
        data/bogo-config.gemspec
    CHANGED
    
    | @@ -1,5 +1,5 @@ | |
| 1 1 | 
             
            $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
         | 
| 2 | 
            -
            require 'bogo | 
| 2 | 
            +
            require 'bogo/config/version'
         | 
| 3 3 | 
             
            Gem::Specification.new do |s|
         | 
| 4 4 | 
             
              s.name = 'bogo-config'
         | 
| 5 5 | 
             
              s.version = Bogo::Config::VERSION.version
         | 
| @@ -15,6 +15,7 @@ Gem::Specification.new do |s| | |
| 15 15 | 
             
              s.add_runtime_dependency 'multi_xml'
         | 
| 16 16 | 
             
              s.add_runtime_dependency 'attribute_struct'
         | 
| 17 17 | 
             
              s.add_development_dependency 'minitest'
         | 
| 18 | 
            -
              s.add_development_dependency ' | 
| 18 | 
            +
              s.add_development_dependency 'rexml'
         | 
| 19 | 
            +
              s.add_development_dependency 'rake'
         | 
| 19 20 | 
             
              s.files = Dir['lib/**/*'] + %w(bogo-config.gemspec README.md CHANGELOG.md CONTRIBUTING.md LICENSE)
         | 
| 20 21 | 
             
            end
         | 
    
        data/lib/bogo/config.rb
    ADDED
    
    | @@ -0,0 +1,336 @@ | |
| 1 | 
            +
            require 'bogo'
         | 
| 2 | 
            +
            require 'yaml'
         | 
| 3 | 
            +
            require 'multi_xml'
         | 
| 4 | 
            +
            require 'multi_json'
         | 
| 5 | 
            +
            require 'attribute_struct'
         | 
| 6 | 
            +
            require 'forwardable'
         | 
| 7 | 
            +
            require 'bogo/config/version'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            autoload :Configuration, 'bogo/config/configuration'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            module Bogo
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              class Config
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                # Exception wrapper class used for configuration file load failure
         | 
| 16 | 
            +
                class FileLoadError < LoadError
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  # @return [Exception]
         | 
| 19 | 
            +
                  attr_reader :original
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def initialize(message, original_exception = nil)
         | 
| 22 | 
            +
                    super(message)
         | 
| 23 | 
            +
                    @original = original_exception
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  # @return [Fixnum]
         | 
| 27 | 
            +
                  def exit_code
         | 
| 28 | 
            +
                    222
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                class << self
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                  include Bogo::Memoization
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  # Reload any registered `Bogo::Config` instances
         | 
| 38 | 
            +
                  #
         | 
| 39 | 
            +
                  # @return [TrueClass]
         | 
| 40 | 
            +
                  def reload!
         | 
| 41 | 
            +
                    obj_ids = memoize(:bogo_reloadable_configs, :global)
         | 
| 42 | 
            +
                    objects = Thread.exclusive do
         | 
| 43 | 
            +
                      ObjectSpace.each_object.find_all do |obj|
         | 
| 44 | 
            +
                        obj_ids.include?(obj.object_id)
         | 
| 45 | 
            +
                      end
         | 
| 46 | 
            +
                    end
         | 
| 47 | 
            +
                    objects.map(&:init!)
         | 
| 48 | 
            +
                    memoize(:bogo_reloadable_configs, :global).delete_if do |oid|
         | 
| 49 | 
            +
                      !obj_ids.include?(oid)
         | 
| 50 | 
            +
                    end
         | 
| 51 | 
            +
                    true
         | 
| 52 | 
            +
                  end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                  # Register config instance to auto reload on HUP
         | 
| 55 | 
            +
                  #
         | 
| 56 | 
            +
                  # @param config [Bogo::Config]
         | 
| 57 | 
            +
                  # @return [TrueClass]
         | 
| 58 | 
            +
                  def reloadable(config)
         | 
| 59 | 
            +
                    if(config.is_a?(Bogo::Config))
         | 
| 60 | 
            +
                      reloader
         | 
| 61 | 
            +
                      memoize(:bogo_reloadable_configs, :global){ [] }.push(config.object_id).uniq!
         | 
| 62 | 
            +
                    else
         | 
| 63 | 
            +
                      raise TypeError.new "Expecting type `Bogo::Config`. Received: `#{config.class}`"
         | 
| 64 | 
            +
                    end
         | 
| 65 | 
            +
                    true
         | 
| 66 | 
            +
                  end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                  # Internal reloader
         | 
| 69 | 
            +
                  #
         | 
| 70 | 
            +
                  # @return [Thread]
         | 
| 71 | 
            +
                  def reloader
         | 
| 72 | 
            +
                    memoize(:bogo_config_reloader, :global) do
         | 
| 73 | 
            +
                      Thread.new do
         | 
| 74 | 
            +
                        begin
         | 
| 75 | 
            +
                          loop do
         | 
| 76 | 
            +
                            begin
         | 
| 77 | 
            +
                              sleep
         | 
| 78 | 
            +
                            rescue SignalException => e
         | 
| 79 | 
            +
                              if(e.signm == 'SIGHUP')
         | 
| 80 | 
            +
                                if(ENV['BOGO_DEBUG'])
         | 
| 81 | 
            +
                                  $stdout.puts 'SIGHUP encountered. Reloading `Bogo::Config` instances.'
         | 
| 82 | 
            +
                                end
         | 
| 83 | 
            +
                                Bogo::Config.reload!
         | 
| 84 | 
            +
                              else
         | 
| 85 | 
            +
                                raise
         | 
| 86 | 
            +
                              end
         | 
| 87 | 
            +
                            end
         | 
| 88 | 
            +
                          end
         | 
| 89 | 
            +
                        rescue => e
         | 
| 90 | 
            +
                          if(ENV['BOGO_DEBUG'])
         | 
| 91 | 
            +
                            $stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
         | 
| 92 | 
            +
                          end
         | 
| 93 | 
            +
                          retry
         | 
| 94 | 
            +
                        end
         | 
| 95 | 
            +
                      end
         | 
| 96 | 
            +
                    end
         | 
| 97 | 
            +
                  end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                end
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                include Bogo::Lazy
         | 
| 102 | 
            +
                extend Forwardable
         | 
| 103 | 
            +
             | 
| 104 | 
            +
                # @return [String] configuration path
         | 
| 105 | 
            +
                attr_reader :path
         | 
| 106 | 
            +
                # @return [String, Hash]
         | 
| 107 | 
            +
                attr_reader :initial
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                # Create new instance
         | 
| 110 | 
            +
                #
         | 
| 111 | 
            +
                # @param path_or_hash [String, Hash] file/directory path or base Hash
         | 
| 112 | 
            +
                # @return [self]
         | 
| 113 | 
            +
                def initialize(path_or_hash=nil)
         | 
| 114 | 
            +
                  super()
         | 
| 115 | 
            +
                  @initial = path_or_hash
         | 
| 116 | 
            +
                  @data = Smash.new
         | 
| 117 | 
            +
                  init!
         | 
| 118 | 
            +
                end
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                # Enables automatic reloading on SIGHUP
         | 
| 121 | 
            +
                #
         | 
| 122 | 
            +
                # @return [TrueClass]
         | 
| 123 | 
            +
                def reloadable!
         | 
| 124 | 
            +
                  Bogo::Config.reloadable(self)
         | 
| 125 | 
            +
                  true
         | 
| 126 | 
            +
                end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
                # Freeze underlying configuration data
         | 
| 129 | 
            +
                #
         | 
| 130 | 
            +
                # @return [self]
         | 
| 131 | 
            +
                def immutable!
         | 
| 132 | 
            +
                  @data = data.to_smash(:freeze)
         | 
| 133 | 
            +
                end
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                # Intialize the configuration
         | 
| 136 | 
            +
                #
         | 
| 137 | 
            +
                # @return [self]
         | 
| 138 | 
            +
                def init!
         | 
| 139 | 
            +
                  if(initial.is_a?(String))
         | 
| 140 | 
            +
                    @path = initial.dup
         | 
| 141 | 
            +
                    hash = load!
         | 
| 142 | 
            +
                  else
         | 
| 143 | 
            +
                    hash = initial
         | 
| 144 | 
            +
                  end
         | 
| 145 | 
            +
                  if(hash)
         | 
| 146 | 
            +
                    is_immutable = data.frozen?
         | 
| 147 | 
            +
                    # TODO: synchronize here
         | 
| 148 | 
            +
                    load_data(hash)
         | 
| 149 | 
            +
                    @data = hash.to_smash.deep_merge(data.to_smash)
         | 
| 150 | 
            +
                    @data.to_smash(:freeze) if is_immutable
         | 
| 151 | 
            +
                  end
         | 
| 152 | 
            +
                  self
         | 
| 153 | 
            +
                end
         | 
| 154 | 
            +
             | 
| 155 | 
            +
                # Allow Smash like behavior
         | 
| 156 | 
            +
                def_delegators(*([:data] + (Smash.public_instance_methods - Object.public_instance_methods)))
         | 
| 157 | 
            +
             | 
| 158 | 
            +
                # Override to force consistent data access (removes dirty
         | 
| 159 | 
            +
                # functionality)
         | 
| 160 | 
            +
                #
         | 
| 161 | 
            +
                # @return [Smash]
         | 
| 162 | 
            +
                def dirty
         | 
| 163 | 
            +
                  data
         | 
| 164 | 
            +
                end
         | 
| 165 | 
            +
             | 
| 166 | 
            +
                # @return [String]
         | 
| 167 | 
            +
                def to_json(*args)
         | 
| 168 | 
            +
                  MultiJson.dump(data, *args)
         | 
| 169 | 
            +
                end
         | 
| 170 | 
            +
             | 
| 171 | 
            +
                # Load configuration from file(s)
         | 
| 172 | 
            +
                #
         | 
| 173 | 
            +
                # @return [self]
         | 
| 174 | 
            +
                def load!
         | 
| 175 | 
            +
                  if(path)
         | 
| 176 | 
            +
                    if(File.directory?(path))
         | 
| 177 | 
            +
                      conf = Dir.glob(File.join(path, '*')).sort.inject(Smash.new) do |memo, file_path|
         | 
| 178 | 
            +
                        memo.deep_merge(load_file(file_path))
         | 
| 179 | 
            +
                      end
         | 
| 180 | 
            +
                    elsif(File.file?(path))
         | 
| 181 | 
            +
                      conf = load_file(path)
         | 
| 182 | 
            +
                    else
         | 
| 183 | 
            +
                      raise Errno::ENOENT.new path
         | 
| 184 | 
            +
                    end
         | 
| 185 | 
            +
                    conf
         | 
| 186 | 
            +
                  end
         | 
| 187 | 
            +
                end
         | 
| 188 | 
            +
             | 
| 189 | 
            +
                # Load configuration file
         | 
| 190 | 
            +
                #
         | 
| 191 | 
            +
                # @param file_path [String] path to file
         | 
| 192 | 
            +
                # @return [Smash]
         | 
| 193 | 
            +
                def load_file(file_path)
         | 
| 194 | 
            +
                  result = nil
         | 
| 195 | 
            +
                  errors = Smash.new
         | 
| 196 | 
            +
                  error = nil
         | 
| 197 | 
            +
                  begin
         | 
| 198 | 
            +
                    result = case File.extname(file_path)
         | 
| 199 | 
            +
                             when '.yaml', '.yml'
         | 
| 200 | 
            +
                               yaml_load(file_path)
         | 
| 201 | 
            +
                             when '.json'
         | 
| 202 | 
            +
                               json_load(file_path)
         | 
| 203 | 
            +
                             when '.xml'
         | 
| 204 | 
            +
                               xml_load(file_path)
         | 
| 205 | 
            +
                             when '.rb'
         | 
| 206 | 
            +
                               struct_load(file_path)
         | 
| 207 | 
            +
                             else
         | 
| 208 | 
            +
                               [:struct_load, :json_load, :yaml_load, :xml_load].each do |loader|
         | 
| 209 | 
            +
                                 begin
         | 
| 210 | 
            +
                                   result = send(loader, file_path)
         | 
| 211 | 
            +
                                   break
         | 
| 212 | 
            +
                                 rescue StandardError, ScriptError => e
         | 
| 213 | 
            +
                                   errors[loader] = e
         | 
| 214 | 
            +
                                   if(ENV['BOGO_DEBUG'])
         | 
| 215 | 
            +
                                     $stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
         | 
| 216 | 
            +
                                   end
         | 
| 217 | 
            +
                                 end
         | 
| 218 | 
            +
                               end
         | 
| 219 | 
            +
                               result
         | 
| 220 | 
            +
                             end
         | 
| 221 | 
            +
                  rescue => error
         | 
| 222 | 
            +
                  end
         | 
| 223 | 
            +
                  unless(result)
         | 
| 224 | 
            +
                    raise FileLoadError.new(
         | 
| 225 | 
            +
                      "Failed to load configuration from file (#{file_path})",
         | 
| 226 | 
            +
                      error || extract_error_for(file_path, errors)
         | 
| 227 | 
            +
                    )
         | 
| 228 | 
            +
                  end
         | 
| 229 | 
            +
                  result
         | 
| 230 | 
            +
                end
         | 
| 231 | 
            +
             | 
| 232 | 
            +
                # Read and parse YAML file
         | 
| 233 | 
            +
                #
         | 
| 234 | 
            +
                # @param file_path
         | 
| 235 | 
            +
                # @return [Smash]
         | 
| 236 | 
            +
                def yaml_load(file_path)
         | 
| 237 | 
            +
                  YAML.load(File.read(file_path)).to_smash
         | 
| 238 | 
            +
                end
         | 
| 239 | 
            +
             | 
| 240 | 
            +
                # Read and parse JSON file
         | 
| 241 | 
            +
                #
         | 
| 242 | 
            +
                # @param file_path
         | 
| 243 | 
            +
                # @return [Smash]
         | 
| 244 | 
            +
                def json_load(file_path)
         | 
| 245 | 
            +
                  MultiJson.load(File.read(file_path)).to_smash
         | 
| 246 | 
            +
                end
         | 
| 247 | 
            +
             | 
| 248 | 
            +
                # Read and parse XML file
         | 
| 249 | 
            +
                #
         | 
| 250 | 
            +
                # @param file_path
         | 
| 251 | 
            +
                # @return [Smash]
         | 
| 252 | 
            +
                # @note supar ENTERPRISE
         | 
| 253 | 
            +
                def xml_load(file_path)
         | 
| 254 | 
            +
                  result = MultiXml.parse(File.read(file_path)).to_smash[:configuration]
         | 
| 255 | 
            +
                  xml_format(result)
         | 
| 256 | 
            +
                end
         | 
| 257 | 
            +
             | 
| 258 | 
            +
                # Format XML types
         | 
| 259 | 
            +
                #
         | 
| 260 | 
            +
                # @param result [Smash]
         | 
| 261 | 
            +
                # @return [Smash]
         | 
| 262 | 
            +
                def xml_format(result)
         | 
| 263 | 
            +
                  Smash[result.map{|k,v| [k, xml_format_value(v)]}]
         | 
| 264 | 
            +
                end
         | 
| 265 | 
            +
             | 
| 266 | 
            +
                # Format XML value types
         | 
| 267 | 
            +
                #
         | 
| 268 | 
            +
                # @param value [Object]
         | 
| 269 | 
            +
                # @return [Object]
         | 
| 270 | 
            +
                def xml_format_value(value)
         | 
| 271 | 
            +
                  case value
         | 
| 272 | 
            +
                  when Hash
         | 
| 273 | 
            +
                    xml_format(value)
         | 
| 274 | 
            +
                  when Array
         | 
| 275 | 
            +
                    value.map{|v| xml_format_value(v)}
         | 
| 276 | 
            +
                  else
         | 
| 277 | 
            +
                    value.strip!
         | 
| 278 | 
            +
                    if(value == 'true')
         | 
| 279 | 
            +
                      true
         | 
| 280 | 
            +
                    elsif(value == 'false')
         | 
| 281 | 
            +
                      false
         | 
| 282 | 
            +
                    elsif(value.to_i.to_s == value)
         | 
| 283 | 
            +
                      value.to_i
         | 
| 284 | 
            +
                    elsif(value.to_f.to_s == value)
         | 
| 285 | 
            +
                      value.to_f
         | 
| 286 | 
            +
                    else
         | 
| 287 | 
            +
                      value
         | 
| 288 | 
            +
                    end
         | 
| 289 | 
            +
                  end
         | 
| 290 | 
            +
                end
         | 
| 291 | 
            +
             | 
| 292 | 
            +
                # Read and parse AttributeStruct file
         | 
| 293 | 
            +
                #
         | 
| 294 | 
            +
                # @param file_path
         | 
| 295 | 
            +
                # @return [Smash]
         | 
| 296 | 
            +
                def struct_load(file_path)
         | 
| 297 | 
            +
                  if(eval_disabled?)
         | 
| 298 | 
            +
                    raise 'Ruby based configuration evaluation is currently disabled!'
         | 
| 299 | 
            +
                  else
         | 
| 300 | 
            +
                    result = Module.new.instance_eval(
         | 
| 301 | 
            +
                      IO.read(file_path), file_path, 1
         | 
| 302 | 
            +
                    )
         | 
| 303 | 
            +
                    result._dump.to_smash
         | 
| 304 | 
            +
                  end
         | 
| 305 | 
            +
                end
         | 
| 306 | 
            +
             | 
| 307 | 
            +
                # @return [TrueClass, FalseClass]
         | 
| 308 | 
            +
                def eval_enabled?
         | 
| 309 | 
            +
                  ENV['BOGO_CONFIG_DISABLE_EVAL'].to_s.downcase != 'true'
         | 
| 310 | 
            +
                end
         | 
| 311 | 
            +
             | 
| 312 | 
            +
                # @return [TrueClass, FalseClass]
         | 
| 313 | 
            +
                def eval_disabled?
         | 
| 314 | 
            +
                  !eval_enabled?
         | 
| 315 | 
            +
                end
         | 
| 316 | 
            +
             | 
| 317 | 
            +
                # Extract appropriate execption based on path
         | 
| 318 | 
            +
                #
         | 
| 319 | 
            +
                # @param path [String]
         | 
| 320 | 
            +
                # @param errors [Hash]
         | 
| 321 | 
            +
                # @return [Exception, NilClass]
         | 
| 322 | 
            +
                def extract_error_for(path, errors)
         | 
| 323 | 
            +
                  content = File.read(path).strip
         | 
| 324 | 
            +
                  if(content.start_with?('{'))
         | 
| 325 | 
            +
                    errors[:json_load]
         | 
| 326 | 
            +
                  elsif(content.start_with?('<'))
         | 
| 327 | 
            +
                    errors[:xml_load]
         | 
| 328 | 
            +
                  elsif(content.match(/\.new\s*(do|\{)/))
         | 
| 329 | 
            +
                    errors[:struct_load]
         | 
| 330 | 
            +
                  else
         | 
| 331 | 
            +
                    errors[:yaml_load]
         | 
| 332 | 
            +
                  end
         | 
| 333 | 
            +
                end
         | 
| 334 | 
            +
             | 
| 335 | 
            +
              end
         | 
| 336 | 
            +
            end
         | 
    
        data/lib/bogo-config/config.rb
    CHANGED
    
    | @@ -1,333 +1 @@ | |
| 1 | 
            -
            require ' | 
| 2 | 
            -
            require 'multi_xml'
         | 
| 3 | 
            -
            require 'multi_json'
         | 
| 4 | 
            -
            require 'attribute_struct'
         | 
| 5 | 
            -
            require 'forwardable'
         | 
| 6 | 
            -
             | 
| 7 | 
            -
            require 'bogo-config'
         | 
| 8 | 
            -
             | 
| 9 | 
            -
            module Bogo
         | 
| 10 | 
            -
             | 
| 11 | 
            -
              class Config
         | 
| 12 | 
            -
             | 
| 13 | 
            -
                # Exception wrapper class used for configuration file load failure
         | 
| 14 | 
            -
                class FileLoadError < LoadError
         | 
| 15 | 
            -
             | 
| 16 | 
            -
                  # @return [Exception]
         | 
| 17 | 
            -
                  attr_reader :original
         | 
| 18 | 
            -
             | 
| 19 | 
            -
                  def initialize(message, original_exception = nil)
         | 
| 20 | 
            -
                    super(message)
         | 
| 21 | 
            -
                    @original = original_exception
         | 
| 22 | 
            -
                  end
         | 
| 23 | 
            -
             | 
| 24 | 
            -
                  # @return [Fixnum]
         | 
| 25 | 
            -
                  def exit_code
         | 
| 26 | 
            -
                    222
         | 
| 27 | 
            -
                  end
         | 
| 28 | 
            -
             | 
| 29 | 
            -
                end
         | 
| 30 | 
            -
             | 
| 31 | 
            -
                class << self
         | 
| 32 | 
            -
             | 
| 33 | 
            -
                  include Bogo::Memoization
         | 
| 34 | 
            -
             | 
| 35 | 
            -
                  # Reload any registered `Bogo::Config` instances
         | 
| 36 | 
            -
                  #
         | 
| 37 | 
            -
                  # @return [TrueClass]
         | 
| 38 | 
            -
                  def reload!
         | 
| 39 | 
            -
                    obj_ids = memoize(:bogo_reloadable_configs, :global)
         | 
| 40 | 
            -
                    objects = Thread.exclusive do
         | 
| 41 | 
            -
                      ObjectSpace.each_object.find_all do |obj|
         | 
| 42 | 
            -
                        obj_ids.include?(obj.object_id)
         | 
| 43 | 
            -
                      end
         | 
| 44 | 
            -
                    end
         | 
| 45 | 
            -
                    objects.map(&:init!)
         | 
| 46 | 
            -
                    memoize(:bogo_reloadable_configs, :global).delete_if do |oid|
         | 
| 47 | 
            -
                      !obj_ids.include?(oid)
         | 
| 48 | 
            -
                    end
         | 
| 49 | 
            -
                    true
         | 
| 50 | 
            -
                  end
         | 
| 51 | 
            -
             | 
| 52 | 
            -
                  # Register config instance to auto reload on HUP
         | 
| 53 | 
            -
                  #
         | 
| 54 | 
            -
                  # @param config [Bogo::Config]
         | 
| 55 | 
            -
                  # @return [TrueClass]
         | 
| 56 | 
            -
                  def reloadable(config)
         | 
| 57 | 
            -
                    if(config.is_a?(Bogo::Config))
         | 
| 58 | 
            -
                      reloader
         | 
| 59 | 
            -
                      memoize(:bogo_reloadable_configs, :global){ [] }.push(config.object_id).uniq!
         | 
| 60 | 
            -
                    else
         | 
| 61 | 
            -
                      raise TypeError.new "Expecting type `Bogo::Config`. Received: `#{config.class}`"
         | 
| 62 | 
            -
                    end
         | 
| 63 | 
            -
                    true
         | 
| 64 | 
            -
                  end
         | 
| 65 | 
            -
             | 
| 66 | 
            -
                  # Internal reloader
         | 
| 67 | 
            -
                  #
         | 
| 68 | 
            -
                  # @return [Thread]
         | 
| 69 | 
            -
                  def reloader
         | 
| 70 | 
            -
                    memoize(:bogo_config_reloader, :global) do
         | 
| 71 | 
            -
                      Thread.new do
         | 
| 72 | 
            -
                        begin
         | 
| 73 | 
            -
                          loop do
         | 
| 74 | 
            -
                            begin
         | 
| 75 | 
            -
                              sleep
         | 
| 76 | 
            -
                            rescue SignalException => e
         | 
| 77 | 
            -
                              if(e.signm == 'SIGHUP')
         | 
| 78 | 
            -
                                if(ENV['BOGO_DEBUG'])
         | 
| 79 | 
            -
                                  $stdout.puts 'SIGHUP encountered. Reloading `Bogo::Config` instances.'
         | 
| 80 | 
            -
                                end
         | 
| 81 | 
            -
                                Bogo::Config.reload!
         | 
| 82 | 
            -
                              else
         | 
| 83 | 
            -
                                raise
         | 
| 84 | 
            -
                              end
         | 
| 85 | 
            -
                            end
         | 
| 86 | 
            -
                          end
         | 
| 87 | 
            -
                        rescue => e
         | 
| 88 | 
            -
                          if(ENV['BOGO_DEBUG'])
         | 
| 89 | 
            -
                            $stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
         | 
| 90 | 
            -
                          end
         | 
| 91 | 
            -
                          retry
         | 
| 92 | 
            -
                        end
         | 
| 93 | 
            -
                      end
         | 
| 94 | 
            -
                    end
         | 
| 95 | 
            -
                  end
         | 
| 96 | 
            -
             | 
| 97 | 
            -
                end
         | 
| 98 | 
            -
             | 
| 99 | 
            -
                include Bogo::Lazy
         | 
| 100 | 
            -
                extend Forwardable
         | 
| 101 | 
            -
             | 
| 102 | 
            -
                # @return [String] configuration path
         | 
| 103 | 
            -
                attr_reader :path
         | 
| 104 | 
            -
                # @return [String, Hash]
         | 
| 105 | 
            -
                attr_reader :initial
         | 
| 106 | 
            -
             | 
| 107 | 
            -
                # Create new instance
         | 
| 108 | 
            -
                #
         | 
| 109 | 
            -
                # @param path_or_hash [String, Hash] file/directory path or base Hash
         | 
| 110 | 
            -
                # @return [self]
         | 
| 111 | 
            -
                def initialize(path_or_hash=nil)
         | 
| 112 | 
            -
                  @initial = path_or_hash
         | 
| 113 | 
            -
                  @data = Smash.new
         | 
| 114 | 
            -
                  init!
         | 
| 115 | 
            -
                end
         | 
| 116 | 
            -
             | 
| 117 | 
            -
                # Enables automatic reloading on SIGHUP
         | 
| 118 | 
            -
                #
         | 
| 119 | 
            -
                # @return [TrueClass]
         | 
| 120 | 
            -
                def reloadable!
         | 
| 121 | 
            -
                  Bogo::Config.reloadable(self)
         | 
| 122 | 
            -
                  true
         | 
| 123 | 
            -
                end
         | 
| 124 | 
            -
             | 
| 125 | 
            -
                # Freeze underlying configuration data
         | 
| 126 | 
            -
                #
         | 
| 127 | 
            -
                # @return [self]
         | 
| 128 | 
            -
                def immutable!
         | 
| 129 | 
            -
                  @data = data.to_smash(:freeze)
         | 
| 130 | 
            -
                end
         | 
| 131 | 
            -
             | 
| 132 | 
            -
                # Intialize the configuration
         | 
| 133 | 
            -
                #
         | 
| 134 | 
            -
                # @return [self]
         | 
| 135 | 
            -
                def init!
         | 
| 136 | 
            -
                  if(initial.is_a?(String))
         | 
| 137 | 
            -
                    @path = initial.dup
         | 
| 138 | 
            -
                    hash = load!
         | 
| 139 | 
            -
                  else
         | 
| 140 | 
            -
                    hash = initial
         | 
| 141 | 
            -
                  end
         | 
| 142 | 
            -
                  if(hash)
         | 
| 143 | 
            -
                    is_immutable = data.frozen?
         | 
| 144 | 
            -
                    # TODO: synchronize here
         | 
| 145 | 
            -
                    load_data(hash)
         | 
| 146 | 
            -
                    @data = hash.to_smash.deep_merge(data.to_smash)
         | 
| 147 | 
            -
                    @data.to_smash(:freeze) if is_immutable
         | 
| 148 | 
            -
                  end
         | 
| 149 | 
            -
                  self
         | 
| 150 | 
            -
                end
         | 
| 151 | 
            -
             | 
| 152 | 
            -
                # Allow Smash like behavior
         | 
| 153 | 
            -
                def_delegators *([:data, :[]] + (Smash.public_instance_methods - Object.public_instance_methods))
         | 
| 154 | 
            -
             | 
| 155 | 
            -
                # Override to force consistent data access (removes dirty
         | 
| 156 | 
            -
                # functionality)
         | 
| 157 | 
            -
                #
         | 
| 158 | 
            -
                # @return [Smash]
         | 
| 159 | 
            -
                def dirty
         | 
| 160 | 
            -
                  data
         | 
| 161 | 
            -
                end
         | 
| 162 | 
            -
             | 
| 163 | 
            -
                # @return [String]
         | 
| 164 | 
            -
                def to_json(*args)
         | 
| 165 | 
            -
                  MultiJson.dump(data, *args)
         | 
| 166 | 
            -
                end
         | 
| 167 | 
            -
             | 
| 168 | 
            -
                # Load configuration from file(s)
         | 
| 169 | 
            -
                #
         | 
| 170 | 
            -
                # @return [self]
         | 
| 171 | 
            -
                def load!
         | 
| 172 | 
            -
                  if(path)
         | 
| 173 | 
            -
                    if(File.directory?(path))
         | 
| 174 | 
            -
                      conf = Dir.glob(File.join(path, '*')).sort.inject(Smash.new) do |memo, file_path|
         | 
| 175 | 
            -
                        memo.deep_merge(load_file(file_path))
         | 
| 176 | 
            -
                      end
         | 
| 177 | 
            -
                    elsif(File.file?(path))
         | 
| 178 | 
            -
                      conf = load_file(path)
         | 
| 179 | 
            -
                    else
         | 
| 180 | 
            -
                      raise Errno::ENOENT.new path
         | 
| 181 | 
            -
                    end
         | 
| 182 | 
            -
                    conf
         | 
| 183 | 
            -
                  end
         | 
| 184 | 
            -
                end
         | 
| 185 | 
            -
             | 
| 186 | 
            -
                # Load configuration file
         | 
| 187 | 
            -
                #
         | 
| 188 | 
            -
                # @param file_path [String] path to file
         | 
| 189 | 
            -
                # @return [Smash]
         | 
| 190 | 
            -
                def load_file(file_path)
         | 
| 191 | 
            -
                  result = nil
         | 
| 192 | 
            -
                  errors = Smash.new
         | 
| 193 | 
            -
                  error = nil
         | 
| 194 | 
            -
                  begin
         | 
| 195 | 
            -
                    result = case File.extname(file_path)
         | 
| 196 | 
            -
                             when '.yaml', '.yml'
         | 
| 197 | 
            -
                               yaml_load(file_path)
         | 
| 198 | 
            -
                             when '.json'
         | 
| 199 | 
            -
                               json_load(file_path)
         | 
| 200 | 
            -
                             when '.xml'
         | 
| 201 | 
            -
                               xml_load(file_path)
         | 
| 202 | 
            -
                             when '.rb'
         | 
| 203 | 
            -
                               struct_load(file_path)
         | 
| 204 | 
            -
                             else
         | 
| 205 | 
            -
                               [:struct_load, :json_load, :yaml_load, :xml_load].each do |loader|
         | 
| 206 | 
            -
                                 begin
         | 
| 207 | 
            -
                                   result = send(loader, file_path)
         | 
| 208 | 
            -
                                   break
         | 
| 209 | 
            -
                                 rescue StandardError, ScriptError => e
         | 
| 210 | 
            -
                                   errors[loader] = e
         | 
| 211 | 
            -
                                   if(ENV['BOGO_DEBUG'])
         | 
| 212 | 
            -
                                     $stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
         | 
| 213 | 
            -
                                   end
         | 
| 214 | 
            -
                                 end
         | 
| 215 | 
            -
                               end
         | 
| 216 | 
            -
                               result
         | 
| 217 | 
            -
                             end
         | 
| 218 | 
            -
                  rescue => error
         | 
| 219 | 
            -
                  end
         | 
| 220 | 
            -
                  unless(result)
         | 
| 221 | 
            -
                    raise FileLoadError.new(
         | 
| 222 | 
            -
                      "Failed to load configuration from file (#{file_path})",
         | 
| 223 | 
            -
                      error || extract_error_for(file_path, errors)
         | 
| 224 | 
            -
                    )
         | 
| 225 | 
            -
                  end
         | 
| 226 | 
            -
                  result
         | 
| 227 | 
            -
                end
         | 
| 228 | 
            -
             | 
| 229 | 
            -
                # Read and parse YAML file
         | 
| 230 | 
            -
                #
         | 
| 231 | 
            -
                # @param file_path
         | 
| 232 | 
            -
                # @return [Smash]
         | 
| 233 | 
            -
                def yaml_load(file_path)
         | 
| 234 | 
            -
                  YAML.load(File.read(file_path)).to_smash
         | 
| 235 | 
            -
                end
         | 
| 236 | 
            -
             | 
| 237 | 
            -
                # Read and parse JSON file
         | 
| 238 | 
            -
                #
         | 
| 239 | 
            -
                # @param file_path
         | 
| 240 | 
            -
                # @return [Smash]
         | 
| 241 | 
            -
                def json_load(file_path)
         | 
| 242 | 
            -
                  MultiJson.load(File.read(file_path)).to_smash
         | 
| 243 | 
            -
                end
         | 
| 244 | 
            -
             | 
| 245 | 
            -
                # Read and parse XML file
         | 
| 246 | 
            -
                #
         | 
| 247 | 
            -
                # @param file_path
         | 
| 248 | 
            -
                # @return [Smash]
         | 
| 249 | 
            -
                # @note supar ENTERPRISE
         | 
| 250 | 
            -
                def xml_load(file_path)
         | 
| 251 | 
            -
                  result = MultiXml.parse(File.read(file_path)).to_smash[:configuration]
         | 
| 252 | 
            -
                  xml_format(result)
         | 
| 253 | 
            -
                end
         | 
| 254 | 
            -
             | 
| 255 | 
            -
                # Format XML types
         | 
| 256 | 
            -
                #
         | 
| 257 | 
            -
                # @param result [Smash]
         | 
| 258 | 
            -
                # @return [Smash]
         | 
| 259 | 
            -
                def xml_format(result)
         | 
| 260 | 
            -
                  Smash[result.map{|k,v| [k, xml_format_value(v)]}]
         | 
| 261 | 
            -
                end
         | 
| 262 | 
            -
             | 
| 263 | 
            -
                # Format XML value types
         | 
| 264 | 
            -
                #
         | 
| 265 | 
            -
                # @param value [Object]
         | 
| 266 | 
            -
                # @return [Object]
         | 
| 267 | 
            -
                def xml_format_value(value)
         | 
| 268 | 
            -
                  case value
         | 
| 269 | 
            -
                  when Hash
         | 
| 270 | 
            -
                    xml_format(value)
         | 
| 271 | 
            -
                  when Array
         | 
| 272 | 
            -
                    value.map{|v| xml_format_value(v)}
         | 
| 273 | 
            -
                  else
         | 
| 274 | 
            -
                    value.strip!
         | 
| 275 | 
            -
                    if(value == 'true')
         | 
| 276 | 
            -
                      true
         | 
| 277 | 
            -
                    elsif(value == 'false')
         | 
| 278 | 
            -
                      false
         | 
| 279 | 
            -
                    elsif(value.to_i.to_s == value)
         | 
| 280 | 
            -
                      value.to_i
         | 
| 281 | 
            -
                    elsif(value.to_f.to_s == value)
         | 
| 282 | 
            -
                      value.to_f
         | 
| 283 | 
            -
                    else
         | 
| 284 | 
            -
                      value
         | 
| 285 | 
            -
                    end
         | 
| 286 | 
            -
                  end
         | 
| 287 | 
            -
                end
         | 
| 288 | 
            -
             | 
| 289 | 
            -
                # Read and parse AttributeStruct file
         | 
| 290 | 
            -
                #
         | 
| 291 | 
            -
                # @param file_path
         | 
| 292 | 
            -
                # @return [Smash]
         | 
| 293 | 
            -
                def struct_load(file_path)
         | 
| 294 | 
            -
                  if(eval_disabled?)
         | 
| 295 | 
            -
                    raise 'Ruby based configuration evaluation is currently disabled!'
         | 
| 296 | 
            -
                  else
         | 
| 297 | 
            -
                    result = Module.new.instance_eval(
         | 
| 298 | 
            -
                      IO.read(file_path), file_path, 1
         | 
| 299 | 
            -
                    )
         | 
| 300 | 
            -
                    result._dump.to_smash
         | 
| 301 | 
            -
                  end
         | 
| 302 | 
            -
                end
         | 
| 303 | 
            -
             | 
| 304 | 
            -
                # @return [TrueClass, FalseClass]
         | 
| 305 | 
            -
                def eval_enabled?
         | 
| 306 | 
            -
                  ENV['BOGO_CONFIG_DISABLE_EVAL'].to_s.downcase != 'true'
         | 
| 307 | 
            -
                end
         | 
| 308 | 
            -
             | 
| 309 | 
            -
                # @return [TrueClass, FalseClass]
         | 
| 310 | 
            -
                def eval_disabled?
         | 
| 311 | 
            -
                  !eval_enabled?
         | 
| 312 | 
            -
                end
         | 
| 313 | 
            -
             | 
| 314 | 
            -
                # Extract appropriate execption based on path
         | 
| 315 | 
            -
                #
         | 
| 316 | 
            -
                # @param path [String]
         | 
| 317 | 
            -
                # @param errors [Hash]
         | 
| 318 | 
            -
                # @return [Exception, NilClass]
         | 
| 319 | 
            -
                def extract_error_for(path, errors)
         | 
| 320 | 
            -
                  content = File.read(path).strip
         | 
| 321 | 
            -
                  if(content.start_with?('{'))
         | 
| 322 | 
            -
                    errors[:json_load]
         | 
| 323 | 
            -
                  elsif(content.start_with?('<'))
         | 
| 324 | 
            -
                    errors[:xml_load]
         | 
| 325 | 
            -
                  elsif(content.match(/\.new\s*(do|\{)/))
         | 
| 326 | 
            -
                    errors[:struct_load]
         | 
| 327 | 
            -
                  else
         | 
| 328 | 
            -
                    errors[:yaml_load]
         | 
| 329 | 
            -
                  end
         | 
| 330 | 
            -
                end
         | 
| 331 | 
            -
             | 
| 332 | 
            -
              end
         | 
| 333 | 
            -
            end
         | 
| 1 | 
            +
            require 'bogo/config'
         | 
    
        data/lib/bogo-config/version.rb
    CHANGED
    
    
    
        data/lib/bogo-config.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: bogo-config
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.3.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Chris Roberts
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date:  | 
| 11 | 
            +
            date: 2022-12-21 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bogo
         | 
| @@ -86,20 +86,34 @@ dependencies: | |
| 86 86 | 
             
                - - ">="
         | 
| 87 87 | 
             
                  - !ruby/object:Gem::Version
         | 
| 88 88 | 
             
                    version: '0'
         | 
| 89 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 90 | 
            +
              name: rexml
         | 
| 91 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 92 | 
            +
                requirements:
         | 
| 93 | 
            +
                - - ">="
         | 
| 94 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 95 | 
            +
                    version: '0'
         | 
| 96 | 
            +
              type: :development
         | 
| 97 | 
            +
              prerelease: false
         | 
| 98 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 99 | 
            +
                requirements:
         | 
| 100 | 
            +
                - - ">="
         | 
| 101 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 102 | 
            +
                    version: '0'
         | 
| 89 103 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 90 104 | 
             
              name: rake
         | 
| 91 105 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 92 106 | 
             
                requirements:
         | 
| 93 | 
            -
                - - " | 
| 107 | 
            +
                - - ">="
         | 
| 94 108 | 
             
                  - !ruby/object:Gem::Version
         | 
| 95 | 
            -
                    version: ' | 
| 109 | 
            +
                    version: '0'
         | 
| 96 110 | 
             
              type: :development
         | 
| 97 111 | 
             
              prerelease: false
         | 
| 98 112 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 99 113 | 
             
                requirements:
         | 
| 100 | 
            -
                - - " | 
| 114 | 
            +
                - - ">="
         | 
| 101 115 | 
             
                  - !ruby/object:Gem::Version
         | 
| 102 | 
            -
                    version: ' | 
| 116 | 
            +
                    version: '0'
         | 
| 103 117 | 
             
            description: Configuration helper library
         | 
| 104 118 | 
             
            email: code@chrisroberts.org
         | 
| 105 119 | 
             
            executables: []
         | 
| @@ -115,6 +129,9 @@ files: | |
| 115 129 | 
             
            - lib/bogo-config/config.rb
         | 
| 116 130 | 
             
            - lib/bogo-config/configuration.rb
         | 
| 117 131 | 
             
            - lib/bogo-config/version.rb
         | 
| 132 | 
            +
            - lib/bogo/config.rb
         | 
| 133 | 
            +
            - lib/bogo/config/configuration.rb
         | 
| 134 | 
            +
            - lib/bogo/config/version.rb
         | 
| 118 135 | 
             
            homepage: https://github.com/spox/bogo-config
         | 
| 119 136 | 
             
            licenses:
         | 
| 120 137 | 
             
            - Apache 2.0
         | 
| @@ -134,10 +151,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 134 151 | 
             
                - !ruby/object:Gem::Version
         | 
| 135 152 | 
             
                  version: '0'
         | 
| 136 153 | 
             
            requirements: []
         | 
| 137 | 
            -
             | 
| 138 | 
            -
            rubygems_version: 2.4.8
         | 
| 154 | 
            +
            rubygems_version: 3.4.0.dev
         | 
| 139 155 | 
             
            signing_key: 
         | 
| 140 156 | 
             
            specification_version: 4
         | 
| 141 157 | 
             
            summary: Configuration helper library
         | 
| 142 158 | 
             
            test_files: []
         | 
| 143 | 
            -
            has_rdoc: 
         |