Class: VMC::Cli::Command::Micro

Inherits:
Base show all
Defined in:
lib/cli/commands/micro.rb

Constant Summary

Constants inherited from Base

Base::MANIFEST

Instance Attribute Summary

Attributes inherited from Base

#no_prompt, #prompt_ok

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#auth_token, #client, #client_info, #find_in_hash, #find_symbol, #frameworks_info, #load_manifest, #load_manifest_structure, #manifest, #manifest_file, #merge_manifest, #merge_parent, #resolve_in, #resolve_lexically, #resolve_manifest, #resolve_symbol, #runtimes_info, #target_base, #target_url

Constructor Details

#initialize(args) ⇒ Micro

Returns a new instance of Micro.



4
5
6
# File 'lib/cli/commands/micro.rb', line 4

def initialize(args)
  super(args)
end

Class Method Details

.platformObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cli/commands/micro.rb', line 101

def self.platform
  case RUBY_PLATFORM
  when /darwin/  # x86_64-darwin11.2.0
    :darwin
  when /linux/   # x86_64-linux
    :linux
  when /mingw|mswin32|cygwin/ # i386-mingw32
    :windows
  else
    RUBY_PLATFORM
  end
end

Instance Method Details

#build_configObject

Returns the configuration needed to run the micro related subcommands. First loads saved config from file (if there is any), then overrides loaded values with command line arguments, and finally tries to guess in case neither was used:

vmx       location of micro.vmx file
vmrun     location of vmrun command
password  password for vcap user (in the guest vm)
platform  current platform


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

def build_config
  conf = VMC::Cli::Config.micro # returns {} if there isn't a saved config

  override(conf, 'vmx', true) do
    locate_vmx(Micro.platform)
  end

  override(conf, 'vmrun', true) do
    VMC::Micro::VMrun.locate(Micro.platform)
  end

  override(conf, 'password') do
    @password = ask("Please enter your Micro Cloud Foundry VM password (vcap user) password", :echo => "*")
  end

  conf['platform'] = Micro.platform

  conf
end

#command(cmd) ⇒ Object



20
21
22
23
24
# File 'lib/cli/commands/micro.rb', line 20

def command(cmd)
  config = build_config
  switcher(config).send(cmd)
  store_config(config)
end

#locate_vmx(platform) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/cli/commands/micro.rb', line 93

def locate_vmx(platform)
  paths = YAML.load_file(VMC::Micro.config_file('paths.yml'))
  vmx_paths = paths[platform.to_s]['vmx']
  vmx = VMC::Micro.locate_file('micro.vmx', 'micro', vmx_paths)
  err "Unable to locate micro.vmx, please supply --vmx option" unless vmx
  vmx
end

#offline(mode) ⇒ Object



8
9
10
# File 'lib/cli/commands/micro.rb', line 8

def offline(mode)
  command('offline')
end

#online(mode) ⇒ Object



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

def online(mode)
  command('online')
end

#override(config, option, escape = false, &blk) ⇒ Object

override with command line arguments and yield the block in case the option isn’t set



84
85
86
87
88
89
90
91
# File 'lib/cli/commands/micro.rb', line 84

def override(config, option, escape=false, &blk)
  # override if given on the command line
  if opt = @options[option.to_sym]
    opt = VMC::Micro.escape_path(opt) if escape
    config[option] = opt
  end
  config[option] = yield unless config[option]
end

#status(mode) ⇒ Object



16
17
18
# File 'lib/cli/commands/micro.rb', line 16

def status(mode)
  command('status')
end

#store_config(config) ⇒ Object

Save the cleartext password if –save is supplied. Note: it is due to vix we have to use a cleartext password :( Only if –password is used and not –save is the password deleted from the config file before it is stored to disk.



73
74
75
76
77
78
79
80
81
# File 'lib/cli/commands/micro.rb', line 73

def store_config(config)
  if @options[:save]
    warn("cleartext password saved in: #{VMC::Cli::Config::MICRO_FILE}")
  elsif @options[:password] || @password
    config.delete('password')
  end

  VMC::Cli::Config.store_micro(config)
end

#switcher(config) ⇒ Object



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

def switcher(config)
  case Micro.platform
  when :darwin
    switcher = VMC::Micro::Switcher::Darwin.new(config)
  when :linux
    switcher = VMC::Micro::Switcher::Linux.new(config)
  when :windows
    switcher = VMC::Micro::Switcher::Windows.new(config)
  when :dummy # for testing only
    switcher = VMC::Micro::Switcher::Dummy.new(config)
  else
    err "unsupported platform: #{Micro.platform}"
  end
end