Sha256: ebca8c263c2f62273b3b94ff31457ff3a6647eb75679afc4379d1459502f033f

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'yaml'

module VagrantPlugins
  module Fractal
    class Config < Vagrant.plugin(2, :config)
      attr_accessor :file
      attr_reader :active

      DEFAULT_SETTINGS = {
        file: 'fractal.yaml'
      }.freeze

      def initialize
        @file = UNSET_VALUE
        @filter = UNSET_VALUE
        @raw = nil
      end

      def file=(path)
        @file = path unless path.empty?
      end

      def active
        validate(nil)
        @filter == UNSET_VALUE ? raw.keys : @filter
      end

      def data
        validate(nil)
        raw.select { |key,_| active.include? key }
      end

      def validate(_)
        finalize!
        errors = _detected_errors

        if File.file?(@file)
          begin
            unless @filter == UNSET_VALUE
              @filter.each do |box|
                errors.push("There is no #{box} box in #{@file}") unless raw.has_key?(box)
              end
            end
          rescue Exception
            errors.push("file #{@file} have wrong format")
          end
        else
          errors.push("file #{@file} does not exists") 
        end

        { 'fractal' => errors }
      end

      def finalize! 
        @file = DEFAULT_SETTINGS[:file] if @file == UNSET_VALUE
        @filter = docker_boxes.split(',').map{|box| box.strip } unless docker_boxes == UNSET_VALUE
      end

      private

      def raw
        @raw ||= YAML.load_file(@file)
      end

      def docker_boxes
        ENV[VagrantPlugins::Fractal::ENVIRONMENT_VARIABLE].nil? ? UNSET_VALUE : ENV[VagrantPlugins::Fractal::ENVIRONMENT_VARIABLE];
      end

    end #Config
  end #Envrionments
end # VagrantPlugins

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-fractal-0.1.7 lib/vagrant-fractal/config.rb