class CF::UAA::Config
Attributes
context[R]
target[R]
Public Class Methods
[](attr)
click to toggle source
# File lib/uaa/cli/config.rb, line 126 def self.[](attr) value(attr) end
add_opts(hash)
click to toggle source
# File lib/uaa/cli/config.rb, line 114 def self.add_opts(hash) raise ArgumentError, "target and context not set" unless @target && @context return unless hash and !hash.empty? @config[@target][:contexts][@context].merge! Util.hash_keys(hash, :sym) save end
config()
click to toggle source
# File lib/uaa/cli/config.rb, line 23 def self.config; @config ? @config.dup : {} end
context=(ctx)
click to toggle source
# File lib/uaa/cli/config.rb, line 82 def self.context=(ctx) raise ArgumentError, "target not set" unless @target unless c = set_current_subhash(@config[@target][:contexts] ||= {}, ctx, @context) raise ArgumentError, "invalid context, #{ctx}" end save @context = c end
delete(tgt = nil, ctx = nil)
click to toggle source
# File lib/uaa/cli/config.rb, line 98 def self.delete(tgt = nil, ctx = nil) if tgt && ctx unless @config[tgt][:contexts].nil? ctx = ctx.downcase.to_sym @config[tgt][:contexts].delete(ctx) end @context = nil if tgt == @target && ctx == @context elsif tgt @config.delete(tgt) @target = @context = nil if tgt == @target else @target, @context, @config = nil, nil, {} end save end
delete_attr(attr)
click to toggle source
# File lib/uaa/cli/config.rb, line 128 def self.delete_attr(attr) raise ArgumentError, "target and context not set" unless @target && @context @config[@target][:contexts][@context].delete(attr) end
load(config = nil)
click to toggle source
if a yaml string is provided, config is loaded from the string, otherwise config is assumed to be a file name to read and store config. config can be retrieved in yaml form from Config.yaml
# File lib/uaa/cli/config.rb, line 31 def self.load(config = nil) @config = {} return unless config if config =~ /^---/ || config == "" @config = config == "" ? {} : YAML.load(config) @config_file = nil elsif File.exist?(@config_file = config) if (@config = YAML.load_file(@config_file)) && @config.is_a?(Hash) @config.each { |k, v| break @config = nil if k.to_s =~ / / } end unless @config && @config.is_a?(Hash) STDERR.puts "", "Invalid config file #{@config_file}.", "If it's from an old version of uaac, please remove it.", "Note that the uaac command structure has changed.", "Please review the new commands with 'uaac help'", "" exit 1 end else # file doesn't exist, make sure we can write it now self.write_file(@config_file, "--- {}\n\n") end Util.hash_keys!(@config, :sym) @context = current_subhash(@config[@target][:contexts]) if @target = current_subhash(@config) end
loaded?()
click to toggle source
# File lib/uaa/cli/config.rb, line 24 def self.loaded?; !!@config end
save()
click to toggle source
# File lib/uaa/cli/config.rb, line 55 def self.save self.write_file(@config_file, YAML.dump(Util.hash_keys(@config, :str))) if @config_file true end
target=(tgt)
click to toggle source
# File lib/uaa/cli/config.rb, line 60 def self.target=(tgt) unless t = set_current_subhash(@config, tgt, @target) raise ArgumentError, "invalid target, #{tgt}" end @context = current_subhash(@config[t][:contexts]) save @target = t end
target?(tgt)
click to toggle source
# File lib/uaa/cli/config.rb, line 26 def self.target?(tgt) tgt if @config[tgt = subhash_key(@config, tgt)] end
target_opts(hash)
click to toggle source
# File lib/uaa/cli/config.rb, line 69 def self.target_opts(hash) raise ArgumentError, "target not set" unless @target return unless hash and !hash.empty? raise ArgumentError, "'contexts' is a reserved key" if hash.key?(:contexts) @config[@target].merge! Util.hash_keys(hash, :sym) save end
target_value(attr)
click to toggle source
# File lib/uaa/cli/config.rb, line 77 def self.target_value(attr) raise ArgumentError, "target not set" unless @target @config[@target][attr] end
valid_context(ctx)
click to toggle source
# File lib/uaa/cli/config.rb, line 91 def self.valid_context(ctx) raise ArgumentError, "target not set" unless @target k = existing_key(@config[@target][:contexts] ||= {}, ctx) raise ArgumentError, "unknown context #{ctx}" unless k k end
value(attr)
click to toggle source
# File lib/uaa/cli/config.rb, line 121 def self.value(attr) raise ArgumentError, "target and context not set" unless @target && @context @config[@target][:contexts][@context][attr] end
yaml()
click to toggle source
# File lib/uaa/cli/config.rb, line 25 def self.yaml; YAML.dump(Util.hash_keys(@config, :str)) end
Private Class Methods
current_subhash(hash)
click to toggle source
# File lib/uaa/cli/config.rb, line 142 def self.current_subhash(hash) return unless hash key = nil hash.each { |k, v| key ? v.delete(:current) : (key = k if v[:current]) } key end
existing_key(hash, key)
click to toggle source
# File lib/uaa/cli/config.rb, line 159 def self.existing_key(hash, key) k = subhash_key(hash, key) k if hash[k] end
set_current_subhash(hash, newcurrent, oldcurrent)
click to toggle source
# File lib/uaa/cli/config.rb, line 164 def self.set_current_subhash(hash, newcurrent, oldcurrent) return unless k = subhash_key(hash, newcurrent) hash[oldcurrent].delete(:current) if oldcurrent (hash[k] ||= {}).merge!(current: true) k end
subhash_key(hash, key)
click to toggle source
key can be an integer index of the desired subhash or the key symbol or string
# File lib/uaa/cli/config.rb, line 150 def self.subhash_key(hash, key) case key when Integer then hash.each_with_index { |(k, v), i| return k if i == key }; nil when String then key.downcase.to_sym when Symbol then key.to_s.downcase.to_sym else nil end end
write_file(filename, content)
click to toggle source
these are all class methods and so can’t really be private, but the methods below here are not intended to be part of the public interface
# File lib/uaa/cli/config.rb, line 137 def self.write_file(filename, content) File.open(filename, 'w') { |f| f.write content } File.chmod(0600, filename) end