class Brightbox::BBConfig
Attributes
Public Class Methods
Source
# File lib/brightbox-cli/config.rb, line 38 def initialize(options = {}) @options = options prepare_dir prepare_ini @client_name = determine_client(options[:client_name]) @account = determine_account(options[:account]) @dirty = false end
@params [Hash] options Options to control loading and settings @option options [String] :directory A path to the directory where config and
cached and located. Otherwise a default of +$HOME/.brightbox+ is used
@option options [Boolean] :force_default_config @option options [String] :client_name The name of the client whose config
section to use
@option options [String] :account The scoped account to use overriding the
saved configuration
Public Instance Methods
Source
# File lib/brightbox-cli/config.rb, line 77 def config @config_file end
The loads the configuration from disk or creates the directory if missing and caches the details.
@return [Ini] The raw settings from the ini configuration file
Source
# File lib/brightbox-cli/config.rb, line 50 def config_directory return @dir if @dir path = @options[:directory] || default_config_dir @dir = File.expand_path(path) end
The String path to the configuration directory
@return [String]
Source
# File lib/brightbox-cli/config.rb, line 60 def config_directory_exists? File.exist?(config_directory) && File.directory?(config_directory) end
Returns true
if the config_directory
exists on disk
@return [Boolean]
Source
# File lib/brightbox-cli/config.rb, line 67 def config_filename return @config_filename if @config_filename @config_filename = File.join(config_directory, "config") end
The String path to the configuration file itself (in .ini format)
@return [String]
Source
# File lib/brightbox-cli/config.rb, line 109 def core_setting(setting) config["core"][setting] end
Returns the core
Source
# File lib/brightbox-cli/config.rb, line 91 def debug_tokens return unless ENV["DEBUG"] if client_name debug "Client: #{client_name}" debug "Access token: #{access_token} (#{cached_access_token})" if using_application? debug "Refresh token: #{refresh_token} (#{cached_refresh_token}))" else debug "Refresh token: <NOT EXPECTED FOR CLIENT>" end else debug "No client selected" end end
Outputs to debug the current values of the config/client’s tokens
Source
# File lib/brightbox-cli/config.rb, line 82 def save return unless dirty? && config.respond_to?(:write) config.write clean_up end
Write out the configuration file to disk
Source
# File lib/brightbox-cli/config.rb, line 113 def set_core_setting(key, value) dirty! unless value == core_setting(key) config["core"][key] = value end
Private Instance Methods
Source
# File lib/brightbox-cli/config.rb, line 125 def configured? if client_name.nil? || config[client_name].nil? raise BBConfigError, "client id or alias #{client_name.inspect} not defined in config" end true end
Source
# File lib/brightbox-cli/config.rb, line 136 def create_directory return if File.exist? config_directory begin FileUtils.mkdir config_directory rescue Errno::EEXIST end end
Attempts to create the directory the config is expecting to find it’s file in
Source
# File lib/brightbox-cli/config.rb, line 120 def default_config_dir config_dir = ENV.fetch("HOME", nil) || Dir.pwd File.join(config_dir, ".brightbox") end
Source
# File lib/brightbox-cli/config.rb, line 145 def prepare_dir create_directory unless config_directory_exists? end
Source
# File lib/brightbox-cli/config.rb, line 149 def prepare_ini return @config_file if @config_file return {} if @config_file == false @config_file ||= Ini.new(config_filename) @config_file rescue Ini::Error => e raise BBConfigError, "Config problem in #{config_filename}: #{e}" end