class Blufin::Config

Public Class Methods

display_errors(errors, gem_name, setup_command = 'x') click to toggle source

This shows the schema validation errors in a consistent manner across all the gems. @return void

# File lib/core/config.rb, line 75
def self.display_errors(errors, gem_name, setup_command = 'x')
    errors_output = []
    errors.each { |e|
        errors_output << "[#{e.path}] #{e.message}"
    }
    invalid_configuration(gem_name, errors, setup_command)
end
edit_config(config_file) click to toggle source

Opens the config file in VIM. @return void

# File lib/core/config.rb, line 49
def self.edit_config(config_file)
    system("vim #{config_file}")
end
get() click to toggle source

Straight-up returns the data object so you can get keys using string literals. Hard-coding keys is OK because configuration rarely changes and it's validated, so you can be assured the data is there. @return Hash

# File lib/core/config.rb, line 35
def self.get
    @@data
end
get_path(*args) click to toggle source

Use for path. Basically wraps them in File.expand_path. @return String

# File lib/core/config.rb, line 41
def self.get_path(*args)
    val = get
    args.each { |arg| val = val[arg] }
    File.expand_path(val)
end
init(schema_file, template_file, config_file, gem_name, setup_command = 'x') click to toggle source

Validate and initialize the configuration file. @return void

# File lib/core/config.rb, line 12
def self.init(schema_file, template_file, config_file, gem_name, setup_command = 'x')
    schema_file = File.expand_path(schema_file)
    template_file = File.expand_path(template_file)
    config_file = File.expand_path(config_file)
    raise RuntimeError, "File not found: #{schema_file}" unless Blufin::Files::file_exists(schema_file)
    raise RuntimeError, "File not found: #{template_file}" unless Blufin::Files::file_exists(template_file)
    if Blufin::Files::file_exists(config_file)
        # Validate the user created config file.
        load_config(config_file, schema_file, gem_name)
    else
        system('clear')
        if Blufin::Terminal::prompt_yes_no(" Missing Configuration File: #{Blufin::Terminal::format_directory(config_file)}", 'This script can automatically create it.', 'Create configuration file')
            make_config(template_file, config_file)
            edit_config(config_file)
            Blufin::Terminal::custom('File Created', 56, "Now try running: #{Blufin::Terminal::format_command(gem_name)}", nil, false)
        end
        exit
    end
end
invalid_configuration(gem_name, errors = nil, setup_command = 'x') click to toggle source

Standardized way of outputting invalid configuration. @return void

# File lib/core/config.rb, line 85
def self.invalid_configuration(gem_name, errors = nil, setup_command = 'x')
    Blufin::Terminal::error("Your configuration file is invalid. To fix it run: #{Blufin::Terminal::format_command("#{gem_name} #{setup_command}")}", errors, true)
end
validate_file(config_file, schema_file) click to toggle source

This validates the configuration file against the defined schema. @return Array

# File lib/core/config.rb, line 55
def self.validate_file(config_file, schema_file)
    begin
        schema_file_parsed = YAML.load_file(schema_file)
    rescue => e
        Blufin::Terminal::error("Failed to parse schema file: #{Blufin::Terminal::format_directory(schema_file)}", e.message)
    end
    validator = Kwalify::Validator.new(schema_file_parsed)
    begin
        document = YAML.load_file(config_file)
        # This is here because the IDE is incorrectly resolving to Convoy::validate().
        # noinspection RubyArgCount
        errors = validator.validate(document)
    rescue => e
        Blufin::Terminal::error("Failed to parse config file: #{Blufin::Terminal::format_directory(config_file)}", e.message)
    end
    return document, errors
end

Private Class Methods

load_config(config_file, schema_file, gem_name, setup_command = 'x') click to toggle source

Loads the config file into global Hash. @return void

# File lib/core/config.rb, line 93
def self.load_config(config_file, schema_file, gem_name, setup_command = 'x')
    document, errors = validate_file(config_file, schema_file)
    if errors && !errors.empty?
        display_errors(errors, gem_name, setup_command)
    else
        @@data = document
    end
end
make_config(template_file, config_file) click to toggle source

Creates a the config file from the given template. @return

# File lib/core/config.rb, line 104
def self.make_config(template_file, config_file)
    `cp #{template_file} #{config_file}`
end