Class: CF::UAA::Config
- Inherits:
-
Object
- Object
- CF::UAA::Config
- Defined in:
- lib/uaa/cli/config.rb
Class Attribute Summary collapse
-
.context ⇒ Object
Returns the value of attribute context.
-
.target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
- .[](attr) ⇒ Object
- .add_opts(hash) ⇒ Object
- .config ⇒ Object
- .current_subhash(hash) ⇒ Object
- .delete(tgt = nil, ctx = nil) ⇒ Object
- .delete_attr(attr) ⇒ Object
- .existing_key(hash, key) ⇒ Object
-
.load(config = nil) ⇒ Object
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.
- .loaded? ⇒ Boolean
- .save ⇒ Object
- .set_current_subhash(hash, newcurrent, oldcurrent) ⇒ Object
-
.subhash_key(hash, key) ⇒ Object
key can be an integer index of the desired subhash or the key symbol or string.
- .target?(tgt) ⇒ Boolean
- .target_opts(hash) ⇒ Object
- .target_value(attr) ⇒ Object
- .valid_context(ctx) ⇒ Object
- .value(attr) ⇒ Object
- .write_file(filename, content) ⇒ Object
- .yaml ⇒ Object
Class Attribute Details
.context ⇒ Object
Returns the value of attribute context.
21 22 23 |
# File 'lib/uaa/cli/config.rb', line 21 def context @context end |
.target ⇒ Object
Returns the value of attribute target.
21 22 23 |
# File 'lib/uaa/cli/config.rb', line 21 def target @target end |
Class Method Details
.[](attr) ⇒ Object
126 |
# File 'lib/uaa/cli/config.rb', line 126 def self.[](attr) value(attr) end |
.add_opts(hash) ⇒ Object
114 115 116 117 118 119 |
# 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 ⇒ Object
23 |
# File 'lib/uaa/cli/config.rb', line 23 def self.config; @config ? @config.dup : {} end |
.current_subhash(hash) ⇒ Object
142 143 144 145 146 147 |
# 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 |
.delete(tgt = nil, ctx = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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) ⇒ Object
128 129 130 131 |
# 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 |
.existing_key(hash, key) ⇒ Object
159 160 161 162 |
# File 'lib/uaa/cli/config.rb', line 159 def self.existing_key(hash, key) k = subhash_key(hash, key) k if hash[k] end |
.load(config = nil) ⇒ Object
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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# 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? ⇒ Boolean
24 |
# File 'lib/uaa/cli/config.rb', line 24 def self.loaded?; !!@config end |
.save ⇒ Object
55 56 57 58 |
# 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 |
.set_current_subhash(hash, newcurrent, oldcurrent) ⇒ Object
164 165 166 167 168 169 |
# 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) ⇒ Object
key can be an integer index of the desired subhash or the key symbol or string
150 151 152 153 154 155 156 157 |
# 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 |
.target?(tgt) ⇒ Boolean
26 |
# File 'lib/uaa/cli/config.rb', line 26 def self.target?(tgt) tgt if @config[tgt = subhash_key(@config, tgt)] end |
.target_opts(hash) ⇒ Object
69 70 71 72 73 74 75 |
# 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) ⇒ Object
77 78 79 80 |
# 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) ⇒ Object
91 92 93 94 95 96 |
# 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) ⇒ Object
121 122 123 124 |
# 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 |
.write_file(filename, content) ⇒ Object
137 138 139 140 |
# 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 |
.yaml ⇒ Object
25 |
# File 'lib/uaa/cli/config.rb', line 25 def self.yaml; YAML.dump(Util.hash_keys(@config, :str)) end |