Class: App::Config

Inherits:
Object
  • Object
show all
Extended by:
ConfigUnique
Includes:
ConfigUnique
Defined in:
lib/core/config.rb

Constant Summary

Constants included from ConfigUnique

ConfigUnique::BETA, ConfigUnique::CONFIG_FILE, ConfigUnique::CRYPT_HEX, ConfigUnique::CRYPT_KEY, ConfigUnique::EC2_HOST, ConfigUnique::EC2_PASS, ConfigUnique::EC2_USER, ConfigUnique::GEM_NAME, ConfigUnique::GIT_USERNAME, ConfigUnique::LINUX, ConfigUnique::MAC, ConfigUnique::PREFERRED_TEXT_EDITOR, ConfigUnique::VM_IP, ConfigUnique::VM_MYSQL_PASSWORD, ConfigUnique::VM_MYSQL_USER, ConfigUnique::VM_USER, ConfigUnique::VM_USER_PASSWORD, ConfigUnique::WORKSTATION_OS, ConfigUnique::WORKSTATION_PATH_TO_BP_CODE, ConfigUnique::WORKSTATION_PATH_TO_BP_DB

Class Method Summary collapse

Methods included from ConfigUnique

config_file_create, param, required_config_keys

Class Method Details

.config_file_editObject



56
57
58
# File 'lib/core/config.rb', line 56

def self.config_file_edit
    system("nano #{File.expand_path(CONFIG_FILE)}")
end

.config_file_exists?Boolean



21
22
23
24
25
26
# File 'lib/core/config.rb', line 21

def self.config_file_exists?
    unless File.exists? ("#{File.expand_path(CONFIG_FILE)}")
        return false
    end
    true
end

.config_params_getObject



60
61
62
63
64
65
# File 'lib/core/config.rb', line 60

def self.config_params_get
    config = ParseConfig.new("#{File.expand_path(CONFIG_FILE)}")
    config.get_params.each do |param|
        @params[param] = config[param]
    end
end

.config_params_validate(show_success_message = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/core/config.rb', line 67

def self.config_params_validate(show_success_message = false)
    error_text = ''
    missing_keys = get_missing_config_keys
    if missing_keys.any?
        missing_keys.each do |key|
            error_text = "#{error_text}\x1B[38;5;196mKey/value must exist and cannot be null:\x1B[0m \x1B[38;5;240m#{key}\x1B[0m\n"
        end
        error_text = error_text[0..-2]
    end
    unless error_text == ''
        show_error_message(error_text)
    end
    if show_success_message
        App::Terminal::success('Configuration parameters are correct.', "You are now ready to start using this utility.\nStart by typing #{App::Terminal::format_command("#{GEM_NAME} --help")} (or #{App::Terminal::format_command("#{GEM_NAME} -h")}).")
        exit
    end
end

.first_journey_messageObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/core/config.rb', line 41

def self.first_journey_message
    puts
    puts "Thank you for installing the #{App::Terminal::format_highlight("#{GEM_NAME}-cli")} ruby gem."
    puts "CLI stands for 'Command Line Interface'."
    puts
    puts "The first thing you'll need to do is setup your configuration file."
    puts "The file is located at: #{App::Terminal::format_directory(ConfigUnique::CONFIG_FILE)}"
    puts
    puts "You probably won't have this file so the program will create it for you."
    puts "\n"
    unless App::Terminal::prompt_yes_no('Create configuration file?')
        App::Terminal::abort
    end
end

.get_custom_key(prefix, config_key) ⇒ Object

Get custom keys from the config file (IE: ssh_ec2=user|host|pem)



114
115
116
117
118
119
120
121
122
123
# File 'lib/core/config.rb', line 114

def self.get_custom_key(prefix, config_key)
    if config_key.nil?
        App::Terminal::error("The script requires a config parameter from your #{App::Terminal::format_directory(ConfigUnique::CONFIG_FILE)} file", "The key should have a prefix of: #{App::Terminal::format_highlight(prefix)}", true)
    end
    config_param = (config_key =~ /\A#{prefix}\S+\z/i) ? config_key : "#{prefix}#{config_key}"
    unless App::Config::param_exists(config_param)
        App::Terminal::error('Invalid config parameter', "Cannot find #{App::Terminal::format_highlight('key')} #{App::Terminal::format_invalid("\"#{config_param}\"")} in: #{App::Terminal::format_directory(ConfigUnique::CONFIG_FILE)}", true)
    end
    App::Config.param(config_param)
end

.get_missing_config_keysObject



90
91
92
93
94
95
96
97
98
# File 'lib/core/config.rb', line 90

def self.get_missing_config_keys
    missing_keys = required_config_keys.dup
    @params.each do |key, value|
        unless value == ''
            missing_keys.delete(key)
        end
    end
    missing_keys
end

.initializeObject



13
14
15
16
17
18
19
# File 'lib/core/config.rb', line 13

def self.initialize
    if config_file_exists?
        run_load_config
    else
        run_first_journey
    end
end

.param_exists(param_name) ⇒ Object

Checks if a config parameter exists.



102
103
104
# File 'lib/core/config.rb', line 102

def self.param_exists(param_name)
    @params.has_key?(param_name)
end

.paramsObject

Get ALL config parameters.



108
109
110
# File 'lib/core/config.rb', line 108

def self.params
    @params
end

.run_first_journeyObject



33
34
35
36
37
38
39
# File 'lib/core/config.rb', line 33

def self.run_first_journey
    first_journey_message
    config_file_create
    config_file_edit
    config_params_get
    config_params_validate(true)
end

.run_load_configObject



28
29
30
31
# File 'lib/core/config.rb', line 28

def self.run_load_config
    config_params_get
    config_params_validate
end

.show_error_message(error_text) ⇒ Object



85
86
87
88
# File 'lib/core/config.rb', line 85

def self.show_error_message(error_text)
    App::Terminal::error('Your configuration parameters are invalid.', "#{error_text}\n\nYou can fix this by running #{App::Terminal::format_command("#{GEM_NAME} setup")} (or #{App::Terminal::format_command("#{GEM_NAME} x")}).")
    exit
end