Class: Blufin::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/core/config.rb

Constant Summary collapse

@@data =
{}

Class Method Summary collapse

Class Method Details

.display_errors(errors, gem_name, setup_command = 'x') ⇒ Object

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

Returns:

  • void



75
76
77
78
79
80
81
# 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) ⇒ Object

Opens the config file in VIM.

Returns:

  • void



49
50
51
# File 'lib/core/config.rb', line 49

def self.edit_config(config_file)
    system("vim #{config_file}")
end

.getObject

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.

Returns:

  • Hash



35
36
37
# File 'lib/core/config.rb', line 35

def self.get
    @@data
end

.get_path(*args) ⇒ Object

Use for path. Basically wraps them in File.expand_path.

Returns:

  • String



41
42
43
44
45
# 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') ⇒ Object

Validate and initialize the configuration file.

Returns:

  • void

Raises:

  • (RuntimeError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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') ⇒ Object

Standardized way of outputting invalid configuration.

Returns:

  • void



85
86
87
# 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) ⇒ Object

This validates the configuration file against the defined schema.

Returns:

  • Array



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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