Class: VMC::Cli::Config
Constant Summary collapse
- DEFAULT_TARGET =
'api.vcap.me'
- DEFAULT_SUGGEST =
'vcap.me'
- TARGET_FILE =
'~/.vmc_target'
- TOKEN_FILE =
'~/.vmc_token'
- INSTANCES_FILE =
'~/.vmc_instances'
- ALIASES_FILE =
'~/.vmc_aliases'
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 ⇒ Object (also: targets)
- .auth_token ⇒ Object
- .instances ⇒ Object
- .lock_and_read(file) ⇒ Object
- .lock_and_write(file, contents) ⇒ Object
- .remove_token_file ⇒ Object
- .store_aliases(aliases) ⇒ Object
- .store_instances(instances) ⇒ Object
- .store_target(target_host) ⇒ Object
- .store_token(token) ⇒ Object
- .suggest_url ⇒ Object
- .target_url ⇒ 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.
126 127 128 |
# File 'lib/cli/config.rb', line 126 def initialize(work_dir = Dir.pwd) @work_dir = work_dir end |
Class Attribute Details
.colorize ⇒ Object
Returns the value of attribute colorize.
19 20 21 |
# File 'lib/cli/config.rb', line 19 def colorize @colorize end |
.nozip ⇒ Object
Returns the value of attribute nozip.
22 23 24 |
# File 'lib/cli/config.rb', line 22 def nozip @nozip end |
.output ⇒ Object
Returns the value of attribute output.
20 21 22 |
# File 'lib/cli/config.rb', line 20 def output @output end |
.trace ⇒ Object
Returns the value of attribute trace.
21 22 23 |
# File 'lib/cli/config.rb', line 21 def trace @trace end |
Class Method Details
.aliases ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/cli/config.rb', line 90 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 ⇒ Object Also known as: targets
52 53 54 55 56 57 |
# File 'lib/cli/config.rb', line 52 def all_tokens token_file = File.(TOKEN_FILE) return nil unless File.exists? token_file contents = lock_and_read(token_file).strip JSON.parse(contents) end |
.auth_token ⇒ Object
61 62 63 64 65 |
# File 'lib/cli/config.rb', line 61 def auth_token return @token if @token tokens = all_tokens @token = tokens[target_url] if tokens end |
.instances ⇒ Object
78 79 80 81 82 83 |
# File 'lib/cli/config.rb', line 78 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
105 106 107 108 109 110 111 112 |
# File 'lib/cli/config.rb', line 105 def lock_and_read(file) File.open(file, "r") {|f| f.flock(File::LOCK_EX) contents = f.read f.flock(File::LOCK_UN) contents } end |
.lock_and_write(file, contents) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cli/config.rb', line 114 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 |
.remove_token_file ⇒ Object
67 68 69 |
# File 'lib/cli/config.rb', line 67 def remove_token_file FileUtils.rm_f(File.(TOKEN_FILE)) end |
.store_aliases(aliases) ⇒ Object
100 101 102 103 |
# File 'lib/cli/config.rb', line 100 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
85 86 87 88 |
# File 'lib/cli/config.rb', line 85 def store_instances(instances) instances_file = File.(INSTANCES_FILE) lock_and_write(instances_file, instances.to_json) end |
.store_target(target_host) ⇒ Object
47 48 49 50 |
# File 'lib/cli/config.rb', line 47 def store_target(target_host) target_file = File.(TARGET_FILE) lock_and_write(target_file, target_host) end |
.store_token(token) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/cli/config.rb', line 71 def store_token(token) tokens = all_tokens || {} tokens[target_url] = token token_file = File.(TOKEN_FILE) lock_and_write(token_file, tokens.to_json) end |
.suggest_url ⇒ Object
42 43 44 45 |
# File 'lib/cli/config.rb', line 42 def suggest_url target_url @suggest_url end |
.target_url ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cli/config.rb', line 24 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! ha = @target_url.split('.') ha.shift @suggest_url = ha.join('.') @suggest_url = DEFAULT_SUGGEST if @suggest_url.empty? else @target_url = DEFAULT_TARGET @suggest_url = DEFAULT_SUGGEST end @target_url = "http://#{@target_url}" unless /^https?/ =~ @target_url @target_url = @target_url.gsub(/\/+$/, '') @target_url end |