Sha256: 13eee480df09a72760e885934ecc715dcda54b7afaeb68564d3447c5509e2984

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

module MotherBrain
  module Chef
    # Handles loading configuration values from a Chef config file
    class Config < Hash
      DEFAULT_PATHS = %w[
        ./.chef/knife.rb
        ~/.chef/knife.rb
        /etc/chef/solo.rb
        /etc/chef/client.rb
      ]

      # @param [String] path
      def initialize(path = nil)
        @path = path
      end

      # Parse the file for the path and store symbolicated keys for knife
      # configuration options.
      #
      # @return [Knife] self
      def parse
        parse_file
        self
      end

      private

        def parse_file
          lines.each { |line| parse_line line }
        end

        def parse_line(line)
          eval line, binding
        rescue
        end

        def method_missing(key, value = nil)
          store key.to_sym, value
        end

        def lines
          file_contents.lines.to_a
        end

        def file_contents
          File.read(file_path)
        rescue
          String.new
        end

        def file_path
          File.expand_path(path)
        end

        def path
          @path ||= DEFAULT_PATHS.find { |path|
            File.exist?(File.expand_path(path))
          }
        end

        # Because it's common to set the local variable current_dir in a knife.rb
        # and then interpolate that into strings, set it here because that's hard
        # to parse.
        def current_dir
          File.dirname(file_path)
        end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
motherbrain-1.5.0 lib/mb/chef/config.rb
motherbrain-1.4.0 lib/mb/chef/config.rb
motherbrain-1.3.0 lib/mb/chef/config.rb
motherbrain-1.2.1 lib/mb/chef/config.rb
motherbrain-1.2.0 lib/mb/chef/config.rb
motherbrain-1.1.3 lib/mb/chef/config.rb
motherbrain-1.1.2 lib/mb/chef/config.rb
motherbrain-1.1.1 lib/mb/chef/config.rb
motherbrain-1.1.0 lib/mb/chef/config.rb
motherbrain-1.0.0 lib/mb/chef/config.rb
motherbrain-0.14.5 lib/mb/chef/config.rb
motherbrain-0.14.4 lib/mb/chef/config.rb
motherbrain-0.14.3 lib/mb/chef/config.rb
motherbrain-0.14.2 lib/mb/chef/config.rb
motherbrain-0.13.1 lib/mb/chef/config.rb