Class: VMC::Cli::Command::Base

Inherits:
Object
  • Object
show all
Includes:
Interactive
Defined in:
lib/cli/commands/base.rb

Direct Known Subclasses

Admin, Apps, Manifest, Micro, Misc, Services, User

Constant Summary collapse

MANIFEST =
"manifest.yml"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cli/commands/base.rb', line 16

def initialize(options={})
  @options = options.dup
  @no_prompt = @options[:noprompts]
  @prompt_ok = !no_prompt

  # Suppress colorize on Windows systems for now.
  if WINDOWS
    VMC::Cli::Config.colorize = false
  end

  @path = @options[:path] || '.'

  load_manifest manifest_file if manifest_file
end

Instance Attribute Details

#no_promptObject (readonly)

Returns the value of attribute no_prompt.



12
13
14
# File 'lib/cli/commands/base.rb', line 12

def no_prompt
  @no_prompt
end

#prompt_okObject (readonly)

Returns the value of attribute prompt_ok.



12
13
14
# File 'lib/cli/commands/base.rb', line 12

def prompt_ok
  @prompt_ok
end

Instance Method Details

#auth_tokenObject



199
200
201
# File 'lib/cli/commands/base.rb', line 199

def auth_token
  @auth_token = VMC::Cli::Config.auth_token(@options[:token_file])
end

#client(cli = nil) ⇒ Object

Inject a client to help in testing.



186
187
188
189
190
191
192
193
# File 'lib/cli/commands/base.rb', line 186

def client(cli=nil)
  @client ||= cli
  return @client if @client
  @client = VMC::Client.new(target_url, auth_token)
  @client.trace = VMC::Cli::Config.trace if VMC::Cli::Config.trace
  @client.proxy_for @options[:proxy] if @options[:proxy]
  @client
end

#client_infoObject



195
196
197
# File 'lib/cli/commands/base.rb', line 195

def client_info
  @client_info ||= client.info
end

#default_infraObject



226
227
228
# File 'lib/cli/commands/base.rb', line 226

def default_infra
  "aws"
end

#find_in_hash(hash, where) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/cli/commands/base.rb', line 165

def find_in_hash(hash, where)
  what = hash
  where.each do |x|
    return nil unless what.is_a?(Hash)
    what = what[x]
  end

  what
end

#find_symbol(sym, ctx) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/cli/commands/base.rb', line 145

def find_symbol(sym, ctx)
  ctx.each do |h|
    if val = resolve_in(h, sym)
      return val
    end
  end

  nil
end

#frameworks_infoObject



216
217
218
219
220
221
222
223
224
# File 'lib/cli/commands/base.rb', line 216

def frameworks_info
  return @frameworks if @frameworks
  info = client_info
  @frameworks = []
  if info[:frameworks]
    info[:frameworks].each_value { |f| @frameworks << [f[:name]] }
  end
  @frameworks
end

#load_manifest(file) ⇒ Object



81
82
83
84
# File 'lib/cli/commands/base.rb', line 81

def load_manifest(file)
  @manifest = load_manifest_structure(file)
  resolve_manifest(@manifest)
end

#load_manifest_structure(file) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cli/commands/base.rb', line 51

def load_manifest_structure(file)
  manifest = YAML.load_file file

  Array(manifest["inherit"]).each do |p|
    manifest = merge_parent(manifest, p)
  end

  if apps = manifest["applications"]
    apps.each do |k, v|
      client.infra = v['infra'] if v['infra']
      abs = File.expand_path(k, file)
      if Dir.pwd.start_with? abs
        manifest = merge_manifest(manifest, v)
      end
    end
  end

  manifest
end

#manifest(*where) ⇒ Object



161
162
163
# File 'lib/cli/commands/base.rb', line 161

def manifest(*where)
  resolve_in(@manifest, *where)
end

#manifest_fileObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cli/commands/base.rb', line 31

def manifest_file
  return @options[:manifest] if @options[:manifest]
  return @manifest_file if @manifest_file

  where = File.expand_path(@path)
  while true
    if File.exists?(File.join(where, MANIFEST))
      @manifest_file = File.join(where, MANIFEST)
      break
    elsif File.basename(where) == "/"
      @manifest_file = nil
      break
    else
      where = File.expand_path("../", where)
    end
  end

  @manifest_file
end

#merge_manifest(child, parent) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cli/commands/base.rb', line 91

def merge_manifest(child, parent)
  merge = proc do |_, old, new|
    if new.is_a?(Hash) and old.is_a?(Hash)
      old.merge(new, &merge)
    else
      new
    end
  end

  parent.merge(child, &merge)
end

#merge_parent(child, path) ⇒ Object



86
87
88
89
# File 'lib/cli/commands/base.rb', line 86

def merge_parent(child, path)
  file = File.expand_path("../" + path, manifest_file)
  merge_manifest(child, load_manifest_structure(file))
end

#resolve_in(hash, *where) ⇒ Object



155
156
157
158
159
# File 'lib/cli/commands/base.rb', line 155

def resolve_in(hash, *where)
  find_in_hash(hash, ["properties"] + where) ||
    find_in_hash(hash, ["applications", @application] + where) ||
    find_in_hash(hash, where)
end

#resolve_lexically(val, ctx = [@manifest]) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cli/commands/base.rb', line 103

def resolve_lexically(val, ctx = [@manifest])
  case val
  when Hash
    val.each_value do |v|
      resolve_lexically(v, [val] + ctx)
    end
  when Array
    val.each do |v|
      resolve_lexically(v, ctx)
    end
  when String
    val.gsub!(/\$\{([[:alnum:]\-]+)\}/) do
      resolve_symbol($1, ctx)
    end
  end

  nil
end

#resolve_manifest(manifest) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/cli/commands/base.rb', line 71

def resolve_manifest(manifest)
  if apps = manifest["applications"]
    apps.each_value do |v|
      resolve_lexically(v, [manifest])
    end
  end

  resolve_lexically(manifest, [manifest])
end

#resolve_symbol(sym, ctx) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cli/commands/base.rb', line 122

def resolve_symbol(sym, ctx)
  case sym
  when "target-base"
    target_base(ctx)

  when "target-url"
    target_url(ctx)

  when "random-word"
    "%04x" % [rand(0x0100000)]

  else
    found = find_symbol(sym, ctx)

    if found
      resolve_lexically(found, ctx)
      found
    else
      err(sym, "Unknown symbol in manifest: ")
    end
  end
end

#runtimes_infoObject



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cli/commands/base.rb', line 203

def runtimes_info
  return @runtimes if @runtimes
  info = client_info
  @runtimes = {}
  if info[:frameworks]
    info[:frameworks].each_value do |f|
      next unless f[:runtimes]
      f[:runtimes].each { |r| @runtimes[r[:name]] = r}
    end
  end
  @runtimes
end

#target_base(ctx = []) ⇒ Object



181
182
183
# File 'lib/cli/commands/base.rb', line 181

def target_base(ctx = [])
  VMC::Cli::Config.base_of(find_symbol("target", ctx) || "api.#{client.suggest_url}")
end

#target_url(ctx = []) ⇒ Object



175
176
177
178
179
# File 'lib/cli/commands/base.rb', line 175

def target_url(ctx = [])
  find_symbol("target", ctx) ||
    (@client && @client.target) ||
    VMC::Cli::Config.target_url
end