Class: VMC::Cli::Config
Constant Summary collapse
- DEFAULT_TARGET =
'api.vcap.me'- TARGET_FILE =
'~/.vmc_target'- UHURU_TARGET_FILE =
'~/.vmc_uhuru_target'- TOKEN_FILE =
'~/.vmc_token'- INSTANCES_FILE =
'~/.vmc_instances'- ALIASES_FILE =
'~/.vmc_aliases'- CLIENTS_FILE =
'~/.vmc_clients'- MICRO_FILE =
'~/.vmc_micro'- STOCK_CLIENTS =
File.("../../../config/clients.yml", __FILE__)
Class Attribute Summary collapse
-
.colorize ⇒ Object
Returns the value of attribute colorize.
-
.nozip ⇒ Object
Returns the value of attribute nozip.
-
.output ⇒ Object
Returns the value of attribute output.
-
.trace ⇒ Object
Returns the value of attribute trace.
Class Method Summary collapse
- .aliases ⇒ Object
- .all_tokens(token_file_path = nil) ⇒ Object (also: targets)
- .all_uhuru_targets ⇒ Object
- .auth_token(token_file_path = nil) ⇒ Object
- .base_of(url) ⇒ Object
- .clients ⇒ Object
- .deep_merge(a, b) ⇒ Object
- .instances ⇒ Object
- .lock_and_read(file) ⇒ Object
- .lock_and_write(file, contents) ⇒ Object
- .micro ⇒ Object
- .remove_token_file ⇒ Object
- .store_aliases(aliases) ⇒ Object
- .store_instances(instances) ⇒ Object
- .store_micro(micro) ⇒ Object
- .store_target(target_host) ⇒ Object
- .store_token(token, token_file_path = nil) ⇒ Object
- .store_uhuru_target(host, uhuru_target) ⇒ Object
- .suggest_url ⇒ Object
- .target_url ⇒ Object
- .uhuru_target ⇒ Object
Instance Method Summary collapse
-
#initialize(work_dir = Dir.pwd) ⇒ Config
constructor
A new instance of Config.
Constructor Details
#initialize(work_dir = Dir.pwd) ⇒ Config
Returns a new instance of Config.
193 194 195 |
# File 'lib/cli/config.rb', line 193 def initialize(work_dir = Dir.pwd) @work_dir = work_dir end |
Class Attribute Details
.colorize ⇒ Object
Returns the value of attribute colorize.
23 24 25 |
# File 'lib/cli/config.rb', line 23 def colorize @colorize end |
.nozip ⇒ Object
Returns the value of attribute nozip.
26 27 28 |
# File 'lib/cli/config.rb', line 26 def nozip @nozip end |
.output ⇒ Object
Returns the value of attribute output.
24 25 26 |
# File 'lib/cli/config.rb', line 24 def output @output end |
.trace ⇒ Object
Returns the value of attribute trace.
25 26 27 |
# File 'lib/cli/config.rb', line 25 def trace @trace end |
Class Method Details
.aliases ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/cli/config.rb', line 116 def aliases aliases_file = File.(ALIASES_FILE) # bacward compatible unless File.exists? aliases_file old_aliases_file = File.('~/.vmc-aliases') FileUtils.mv(old_aliases_file, aliases_file) if File.exists? old_aliases_file end aliases = YAML.load_file(aliases_file) rescue {} end |
.all_tokens(token_file_path = nil) ⇒ Object Also known as: targets
78 79 80 81 82 83 |
# File 'lib/cli/config.rb', line 78 def all_tokens(token_file_path=nil) token_file = File.(token_file_path || TOKEN_FILE) return nil unless File.exists? token_file contents = lock_and_read(token_file).strip JSON.parse(contents) end |
.all_uhuru_targets ⇒ Object
45 46 47 48 49 50 |
# File 'lib/cli/config.rb', line 45 def all_uhuru_targets uhuru_target_file = File.(UHURU_TARGET_FILE) return nil unless File.exists? uhuru_target_file contents = lock_and_read(uhuru_target_file).strip JSON.parse(contents, :symbolize_names => true) end |
.auth_token(token_file_path = nil) ⇒ Object
87 88 89 90 91 |
# File 'lib/cli/config.rb', line 87 def auth_token(token_file_path=nil) return @token if @token tokens = all_tokens(token_file_path) @token = tokens[target_url] if tokens end |
.base_of(url) ⇒ Object
52 53 54 |
# File 'lib/cli/config.rb', line 52 def base_of(url) url.sub(/^[^\.]+\./, "") end |
.clients ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cli/config.rb', line 155 def clients return @clients if @clients stock = YAML.load_file(STOCK_CLIENTS) clients = File. CLIENTS_FILE if File.exists? clients user = YAML.load_file(clients) @clients = deep_merge(stock, user) else @clients = stock end end |
.deep_merge(a, b) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/cli/config.rb', line 143 def deep_merge(a, b) merge = proc do |_, old, new| if new.is_a?(Hash) and old.is_a?(Hash) old.merge(new, &merge) else new end end a.merge(b, &merge) end |
.instances ⇒ Object
104 105 106 107 108 109 |
# File 'lib/cli/config.rb', line 104 def instances instances_file = File.(INSTANCES_FILE) return nil unless File.exists? instances_file contents = lock_and_read(instances_file).strip JSON.parse(contents) end |
.lock_and_read(file) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cli/config.rb', line 168 def lock_and_read(file) File.open(file, File::RDONLY) {|f| if defined? JRUBY_VERSION f.flock(File::LOCK_SH) else f.flock(File::LOCK_EX) end contents = f.read f.flock(File::LOCK_UN) contents } end |
.lock_and_write(file, contents) ⇒ Object
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cli/config.rb', line 181 def lock_and_write(file, contents) File.open(file, File::RDWR | File::CREAT, 0600) {|f| f.flock(File::LOCK_EX) f.rewind f.puts contents f.flush f.truncate(f.pos) f.flock(File::LOCK_UN) } end |
.micro ⇒ Object
131 132 133 134 135 136 |
# File 'lib/cli/config.rb', line 131 def micro micro_file = File.(MICRO_FILE) return {} unless File.exists? micro_file contents = lock_and_read(micro_file).strip JSON.parse(contents) end |
.remove_token_file ⇒ Object
93 94 95 |
# File 'lib/cli/config.rb', line 93 def remove_token_file FileUtils.rm_f(File.(TOKEN_FILE)) end |
.store_aliases(aliases) ⇒ Object
126 127 128 129 |
# File 'lib/cli/config.rb', line 126 def store_aliases(aliases) aliases_file = File.(ALIASES_FILE) File.open(aliases_file, 'wb') {|f| f.write(aliases.to_yaml)} end |
.store_instances(instances) ⇒ Object
111 112 113 114 |
# File 'lib/cli/config.rb', line 111 def store_instances(instances) instances_file = File.(INSTANCES_FILE) lock_and_write(instances_file, instances.to_json) end |
.store_micro(micro) ⇒ Object
138 139 140 141 |
# File 'lib/cli/config.rb', line 138 def store_micro(micro) micro_file = File.(MICRO_FILE) lock_and_write(micro_file, micro.to_json) end |
.store_target(target_host) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/cli/config.rb', line 60 def store_target(target_host) target_host = "http://#{target_host}" unless /^https?/ =~ target_host target_host = target_host.gsub(/\/+$/, '') target_file = File.(TARGET_FILE) lock_and_write(target_file, target_host) end |
.store_token(token, token_file_path = nil) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/cli/config.rb', line 97 def store_token(token, token_file_path=nil) tokens = all_tokens(token_file_path) || {} tokens[target_url] = token token_file = File.(token_file_path || TOKEN_FILE) lock_and_write(token_file, tokens.to_json) end |
.store_uhuru_target(host, uhuru_target) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/cli/config.rb', line 68 def store_uhuru_target(host, uhuru_target) host = "http://#{host}" unless /^https?/ =~ host host = host.gsub(/\/+$/, '') uhuru_target_urls = all_uhuru_targets || {} uhuru_target_urls[host.to_sym] = uhuru_target uhuru_target_file = File.(UHURU_TARGET_FILE) lock_and_write(uhuru_target_file, JSON.pretty_generate(uhuru_target_urls)) end |
.suggest_url ⇒ Object
56 57 58 |
# File 'lib/cli/config.rb', line 56 def suggest_url @suggest_url ||= base_of(target_url) end |
.target_url ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cli/config.rb', line 28 def target_url return @target_url if @target_url target_file = File.(TARGET_FILE) if File.exists? target_file @target_url = lock_and_read(target_file).strip else @target_url = DEFAULT_TARGET end @target_url = "http://#{@target_url}" unless /^https?/ =~ @target_url @target_url = @target_url.gsub(/\/+$/, '') @target_url end |
.uhuru_target ⇒ Object
41 42 43 |
# File 'lib/cli/config.rb', line 41 def uhuru_target (all_uhuru_targets || {})[target_url.to_sym] end |