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



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

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.



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

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



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

def client_info
  @client_info ||= client.info
end

#find_in_hash(hash, where) ⇒ Object



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

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



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

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

  nil
end

#frameworks_infoObject



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#target_url(ctx = []) ⇒ Object



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

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