Class: VMC::Cli::Config

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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_dir = Dir.pwd) ⇒ Config

Returns a new instance of Config.



105
106
107
# File 'lib/cli/config.rb', line 105

def initialize(work_dir = Dir.pwd)
  @work_dir = work_dir
end

Class Attribute Details

.colorizeObject

Returns the value of attribute colorize.



19
20
21
# File 'lib/cli/config.rb', line 19

def colorize
  @colorize
end

.nozipObject

Returns the value of attribute nozip.



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

def nozip
  @nozip
end

.outputObject

Returns the value of attribute output.



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

def output
  @output
end

.suggest_urlObject (readonly)

Returns the value of attribute suggest_url.



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

def suggest_url
  @suggest_url
end

.traceObject

Returns the value of attribute trace.



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

def trace
  @trace
end

Class Method Details

.aliasesObject



88
89
90
91
92
93
94
95
96
# File 'lib/cli/config.rb', line 88

def aliases
  aliases_file = File.expand_path(ALIASES_FILE)
  # bacward compatible
  unless File.exists? aliases_file
    old_aliases_file = File.expand_path('~/.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_tokensObject Also known as: targets



49
50
51
52
53
54
# File 'lib/cli/config.rb', line 49

def all_tokens
  token_file = File.expand_path(TOKEN_FILE)
  return nil unless File.exists? token_file
  contents = File.read(token_file).strip
  JSON.parse(contents)
end

.auth_tokenObject



58
59
60
61
62
# File 'lib/cli/config.rb', line 58

def auth_token
  return @token if @token
  tokens = all_tokens
  @token = tokens[target_url] if tokens
end

.instancesObject



76
77
78
79
80
81
# File 'lib/cli/config.rb', line 76

def instances
  instances_file = File.expand_path(INSTANCES_FILE)
  return nil unless File.exists? instances_file
  contents = File.read(instances_file).strip
  JSON.parse(contents)
end

.remove_token_fileObject



64
65
66
# File 'lib/cli/config.rb', line 64

def remove_token_file
  FileUtils.rm_f(File.expand_path(TOKEN_FILE))
end

.store_aliases(aliases) ⇒ Object



98
99
100
101
# File 'lib/cli/config.rb', line 98

def store_aliases(aliases)
  aliases_file = File.expand_path(ALIASES_FILE)
  File.open(aliases_file, 'wb') {|f| f.write(aliases.to_yaml)}
end

.store_instances(instances) ⇒ Object



83
84
85
86
# File 'lib/cli/config.rb', line 83

def store_instances(instances)
  instances_file = File.expand_path(INSTANCES_FILE)
  File.open(instances_file, 'w') { |f| f.write(instances.to_json) }
end

.store_target(target_host) ⇒ Object



43
44
45
46
47
# File 'lib/cli/config.rb', line 43

def store_target(target_host)
  target_file = File.expand_path(TARGET_FILE)
  File.open(target_file, 'w+') { |f| f.puts target_host }
  FileUtils.chmod 0600, target_file
end

.store_token(token) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/cli/config.rb', line 68

def store_token(token)
  tokens = all_tokens || {}
  tokens[target_url] = token
  token_file = File.expand_path(TOKEN_FILE)
  File.open(token_file, 'w+') { |f| f.write(tokens.to_json) }
  FileUtils.chmod 0600, token_file
end

.target_urlObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cli/config.rb', line 25

def target_url
  return @target_url if @target_url
  target_file = File.expand_path(TARGET_FILE)
  if File.exists? target_file
    @target_url = File.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