lib/convox_installer/config.rb in convox_installer-1.0.9 vs lib/convox_installer/config.rb in convox_installer-2.0.0

- old
+ new

@@ -1,50 +1,50 @@ # frozen_string_literal: true -require "highline" -require "fileutils" -require "json" -require "securerandom" +require 'highline' +require 'fileutils' +require 'json' +require 'securerandom' module ConvoxInstaller class Config - CONFIG_FILE = File.expand_path("./.installer_config.json").freeze + CONFIG_FILE = File.expand_path('./.installer_config.json').freeze attr_accessor :logger, :config, :prompts, :highline DEFAULT_PROMPTS = [ { key: :stack_name, - title: "Convox Stack Name", - prompt: "Please enter a name for your Convox installation", - default: "convox", + title: 'Convox Stack Name', + prompt: 'Please enter a name for your Convox installation', + default: 'convox' }, { key: :aws_region, - title: "AWS Region", - default: "us-east-1", + title: 'AWS Region', + default: 'us-east-1' }, { key: :instance_type, - title: "EC2 Instance Type", - default: "t3.medium", + title: 'EC2 Instance Type', + default: 't3.medium' }, { - section: "Admin AWS Credentials", + section: 'Admin AWS Credentials' }, { key: :aws_access_key_id, - title: "AWS Access Key ID", + title: 'AWS Access Key ID' }, { key: :aws_secret_access_key, - title: "AWS Secret Access Key", - }, + title: 'AWS Secret Access Key' + } ].freeze def initialize(options = {}) - @logger = Logger.new(STDOUT) + @logger = Logger.new($stdout) logger.level = options[:log_level] || Logger::INFO self.prompts = options[:prompts] || DEFAULT_PROMPTS self.config = {} load_config_from_file @@ -72,26 +72,27 @@ show_config_summary @completed_prompt = true - highline.say "Please double check all of these configuration details." + highline.say 'Please double check all of these configuration details.' agree = highline.agree( - "Would you like to start the Convox installation?" \ + 'Would you like to start the Convox installation?' \ " (press 'n' to correct any settings)" ) break if agree + highline.say "\n" end config end def show_config_summary highline.say "\n============================================" - highline.say " SUMMARY" + highline.say ' SUMMARY' highline.say "============================================\n\n" config_titles = prompts.map do |prompt| prompt[:title] || prompt[:key] end.compact @@ -104,14 +105,22 @@ title = prompt[:title] || prompt[:key] padded_key = "#{title}:".ljust(max + 3) highline.say " #{padded_key} #{value}" end highline.say "\nWe've saved your configuration to: #{CONFIG_FILE}" - highline.say "If anything goes wrong during the installation, " \ + highline.say 'If anything goes wrong during the installation, ' \ "you can restart the script to reload the config and continue.\n\n" end + def self.config_file_exists? + File.exist?(CONFIG_FILE) + end + + def self.read_config_file + File.read(CONFIG_FILE) + end + private def ask_prompt(prompt) key = prompt[:key] title = prompt[:title] || key @@ -128,14 +137,14 @@ if prompt[:value] return if config[key] default = prompt[:value] config[key] = if default.is_a?(Proc) - default.arity == 0 ? default.call : default.call(config) - else - default - end + default.arity.zero? ? default.call : default.call(config) + else + default + end save_config_to_file return end prompt_string = prompt[:prompt] || "Please enter your #{title}: " @@ -155,11 +164,11 @@ def load_config_from_file return unless Config.config_file_exists? logger.debug "Loading saved config from #{CONFIG_FILE}..." - loaded_config = JSON.parse(Config.read_config_file)["config"].symbolize_keys + loaded_config = JSON.parse(Config.read_config_file)['config'].symbolize_keys self.config = config.merge(loaded_config) end def load_config_from_env config_keys.each do |key| @@ -175,16 +184,8 @@ def save_config_to_file # FileUtils.mkdir_p File.expand_path("~/.convox") File.open(CONFIG_FILE, 'w') do |f| f.puts(JSON.pretty_generate(config: config)) end - end - - def self.config_file_exists? - File.exist?(CONFIG_FILE) - end - - def self.read_config_file - File.read(CONFIG_FILE) end end end