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, 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.



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

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.



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

def no_prompt
  @no_prompt
end

#prompt_okObject (readonly)

Returns the value of attribute prompt_ok.



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

def prompt_ok
  @prompt_ok
end

Instance Method Details

#auth_tokenObject



201
202
203
# File 'lib/cli/commands/base.rb', line 201

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.



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

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



197
198
199
# File 'lib/cli/commands/base.rb', line 197

def client_info
  @client_info ||= client.info
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



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

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



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 52

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



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

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



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

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



179
180
181
182
183
184
185
# File 'lib/cli/commands/base.rb', line 179

def target_base(ctx = [])
  if tgt = find_symbol("target", ctx)
    VMC::Cli::Config.base_of(tgt)
  else
    VMC::Cli::Config.suggest_url
  end
end

#target_url(ctx = []) ⇒ Object



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

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