Class: CF::UAA::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/uaa/cli/config.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.contextObject

Returns the value of attribute context.



21
22
23
# File 'lib/uaa/cli/config.rb', line 21

def context
  @context
end

.targetObject

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

Raises:

  • (ArgumentError)


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

.configObject



23
# File 'lib/uaa/cli/config.rb', line 23

def self.config; @config ? @config.dup : {} 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

Raises:

  • (ArgumentError)


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

.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

Returns:

  • (Boolean)


24
# File 'lib/uaa/cli/config.rb', line 24

def self.loaded?; !!@config end

.saveObject



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

.target?(tgt) ⇒ Boolean

Returns:

  • (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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

.yamlObject



25
# File 'lib/uaa/cli/config.rb', line 25

def self.yaml; YAML.dump(Util.hash_keys(@config, :str)) end