Class: Veewee::Command::GroupBase

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, Helpers
Defined in:
lib/veewee/command/group_base.rb

Overview

A GroupBase is the superclass which should be used if you’re creating a CLI command which has subcommands such as ‘veewee box`, which has subcommands such as `add`, `remove`, `list`. If you’re creating a simple command which has no subcommands, such as ‘veewee up`, then use Base instead.

Unlike Base, where all public methods are executed, in a GroupBase, each public method defines a separate task which can be invoked. The best way to get examples of how to create a GroupBase command is to look at the built-in commands, such as BoxCommand.

# Defining a New Command

To define a new command with subcommands, create a new class which inherits from this class, then call GroupBase.register to register the command. That’s it! When the command is invoked, the method matching the subcommand is invoked. An example is shown below:

class SayCommand < Veewee::Command::GroupBase
  register "say", "Say hello or goodbye"

  desc "hello", "say hello"
  def hello
    env.ui.info "Hello"
  end

  desc "goodbye", "say goodbye"
  def goodbye
    env.ui.info "Goodbye"
  end
end

In this case, the above class is invokable via ‘veewee say hello` or `veewee say goodbye`. To give it a try yourself, just copy and paste the above into a Veeweefile somewhere, and run `veewee` from within that directory. You should see the new command!

Also notice that in the above, each task follows a ‘desc` call. This call is used to provide usage and description for each task, and is required.

## Defining Command-line Options

### Arguments

To define arguments to your commands, such as ‘veewee say hello mitchell`, then you simply define them as arguments to the method implementing the task. An example is shown below (only the method, to keep things brief):

def hello(name)
  env.ui.info "Hello, #{name}"
end

Then, if ‘veewee say hello mitchell` was called, then the output would be “Hello, mitchell”

### Switches or Other Options

TODO

Direct Known Subclasses

Fusion, Kvm, Parallels, Vbox

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#initialize_environment

Constructor Details

#initialize(*args) ⇒ GroupBase

Returns a new instance of GroupBase.



101
102
103
104
105
106
107
108
109
# File 'lib/veewee/command/group_base.rb', line 101

def initialize(*args)
  super
  # make provider class variables easily available to global task methods
  @command = self.class.class_variable_get(:@@command)
  @description = self.class.class_variable_get(:@@description)
  @provider = self.class.class_variable_get(:@@provider)
  initialize_environment(*args)
  @env.current_provider = @provider
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



76
77
78
# File 'lib/veewee/command/group_base.rb', line 76

def env
  @env
end

Class Method Details

.register(options = {}) ⇒ Object

Register the command with the main Veewee CLI under the given usage. The usage will be used for accessing it from the CLI, so if you give it a usage of ‘lamp [subcommand]`, then the command to invoke this will be `veewee lamp` (with a subcommand).

The description is used when a listing of the commands is given and is meant to be a brief (one sentence) description of what this command does.

Some additional options may be passed in as the last parameter:

  • ‘:alias` - If given as an array or string, these will be aliases

for the same command. For example, `veewee version` is also
`veewee --version` and `veewee -v`


93
94
95
96
97
98
99
# File 'lib/veewee/command/group_base.rb', line 93

def self.register(options = {})
  # self refers to the class object of the provider subclass
  self.send(:class_variable_set, :@@command,     options[:command]    )
  self.send(:class_variable_set, :@@description, options[:description])
  self.send(:class_variable_set, :@@provider,    options[:provider]   )
  CLI.register(self, options[:command], options[:command], options[:description], options[:opts])
end

Instance Method Details

#copy(box_name, src, dst) ⇒ Object



175
176
177
# File 'lib/veewee/command/group_base.rb', line 175

def copy(box_name, src, dst)
  env.get_box(box_name).copy_to_box(src,dst)
end

#define(definition_name, template_name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/veewee/command/group_base.rb', line 131

def define(definition_name, template_name)
  begin
    env.definitions.define(definition_name,template_name,options)
    env.ui.info "The basebox '#{definition_name}' has been successfully created from the template '#{template_name}'"
    env.ui.info "You can now edit the definition files stored in #{options[:cwd]}/definitions/#{definition_name} or build the box with:"
    env.ui.info "veewee #{@command} build '#{definition_name}' --workdir=#{options[:cwd]}"
  rescue Error => ex
    env.ui.error("#{ex}",:prefix => false)
    exit -1
  end
end

#destroy(box_name) ⇒ Object



153
154
155
# File 'lib/veewee/command/group_base.rb', line 153

def destroy(box_name)
  env.get_box(box_name).destroy(options)
end

#halt(box_name) ⇒ Object



159
160
161
# File 'lib/veewee/command/group_base.rb', line 159

def halt(box_name)
  env.get_box(box_name).halt(options)
end

#listObject



121
122
123
124
125
126
127
# File 'lib/veewee/command/group_base.rb', line 121

def list
  venv=env
  env.ui.info "The following definitions are available in #{venv.cwd}: ",:prefix => false
  venv.definitions.each do |name,definition|
    env.ui.info "- #{name}",:prefix => false
  end
end

#ostypesObject



192
193
194
195
196
# File 'lib/veewee/command/group_base.rb', line 192

def ostypes
  env.ostypes.each do |name|
    env.ui.info "- #{name}"
  end
end

#sendkeys(box_name, sequence) ⇒ Object



199
200
201
# File 'lib/veewee/command/group_base.rb', line 199

def sendkeys(box_name, sequence)
  env.get_box(box_name).console_type(sequence.split(","))
end

#ssh(box_name, command = nil) ⇒ Object



170
171
172
# File 'lib/veewee/command/group_base.rb', line 170

def ssh(box_name, command=nil)
  env.get_box(box_name).issh(command)
end

#templatesObject



113
114
115
116
117
118
# File 'lib/veewee/command/group_base.rb', line 113

def templates
  env.ui.info "The following templates are available:",:prefix => false
  env.templates.each do |name,template|
    env.ui.info "veewee #{@command} define '#{options[:box_name]}' '#{name}' --workdir=#{options[:cwd]}",:prefix => false
  end
end

#undefine(definition_name) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/veewee/command/group_base.rb', line 180

def undefine(definition_name)
  env.ui.info "Removing definition #{definition_name}" , :prefix => false
  begin
    env.definitions.undefine(definition_name,options)
    env.ui.info "Definition #{definition_name} successfully removed",:prefix => false
  rescue Error => ex
    env.ui.error "#{ex}" , :prefix => false
    exit -1
  end
end

#up(box_name) ⇒ Object



165
166
167
# File 'lib/veewee/command/group_base.rb', line 165

def up(box_name)
  env.get_box(box_name).up(options)
end

#winrm(box_name, command = nil) ⇒ Object



144
145
146
147
148
# File 'lib/veewee/command/group_base.rb', line 144

def winrm(box_name, command=nil)
  venv=Veewee::Environment.new(options)
  venv.ui=env.ui
  venv.providers["virtualbox"].get_box(box_name).winrm(command,{:exitcode => "*"})
end