class RubyYacht::Runner::Command

This class is the base class for the commands that users can run for managing their system.

Public Class Methods

command() click to toggle source

The name of the command that users run to trigger this command. This implementation raises an error. Subclasses must implement it to provide the command name.

# File lib/ruby_yacht/runner/command.rb, line 12
def self.command
  raise "Command not defined for #{self}"
end
description() click to toggle source

This class gets a short description of the command. This implementation raises an error. Subclasses must implement it to provide the command description.

# File lib/ruby_yacht/runner/command.rb, line 19
def self.description
  raise "Description not defined for #{self}"
end
short_script_name() click to toggle source

This method gets the name of the script that was invoked to run the commands.

This will just be the filename of the script.

# File lib/ruby_yacht/runner/command.rb, line 69
def self.short_script_name
  return File.basename($0)
end

Public Instance Methods

backtick(command) click to toggle source

This method runs a command using the backtick operator.

This allows us to stub and and test the system interaction.

### Parameters

  • **command: String** The command to execute.

### Returns This returns the output from the command.

# File lib/ruby_yacht/runner/command.rb, line 169
def backtick(command)
  `#{command}`
end
default_project() click to toggle source

The project that we will use if they do not provide one from the command line.

# File lib/ruby_yacht/runner/command.rb, line 80
def default_project
  projects.first
end
docker(command) click to toggle source

This method sends a command to docker.

This should start with exec, run, build, etc.

This allows us to stub out and test the docker commands that get sent.

### Parameters

  • **command: String** The command to execute.

# File lib/ruby_yacht/runner/command.rb, line 145
def docker(command)
  system "docker #{command}"
end
docker_machine() click to toggle source

This method gets the path to the docker-machine binary.

If docker-machine is not installed or is disabled, this will be blank.

### Returns String

# File lib/ruby_yacht/runner/command.rb, line 124
def docker_machine
  return '' if RubyYacht.configuration.disable_docker_machine
  backtick('which docker-machine').strip
end
get_machine_info(property) click to toggle source

This method gets a property from the default docker machine.

### Parameters

  • **property: String** The docker machine property to fetch.

### Returns The response from docker-machine.

# File lib/ruby_yacht/runner/command.rb, line 113
def get_machine_info(property)
  return '' if docker_machine == ''
  backtick("#{docker_machine} inspect default -f {{#{property}}}").strip
end
log(message) click to toggle source

This method logs a message to standard out. It allows us to stub out and test the logging behavior of the commands.

# File lib/ruby_yacht/runner/command.rb, line 133
def log(message)
  $stdout.puts message
end
option_parser() click to toggle source

This method gets the command-line options for invoking this script.

This must return an OptionParser.

The default implementation returns an OptionParser with the command name and description. Subclasses should implement this if they want to accept other command-line options.

# File lib/ruby_yacht/runner/command.rb, line 30
def option_parser
  OptionParser.new do |options|
    options.banner = "Usage: #{Command.short_script_name} #{self.class.command}\n\n#{self.class.description}"
  end
end
parse_positional_arguments(arguments) click to toggle source

This method extracts the positional arguments from the command line.

This will be called after all the flags have been consumed by the OptionParser.

The default implementation does nothing. Subclasses should implement this if they want to extract arguments from the command line.

### Parameters

  • **arguments: Array** The remaining arguments from the command line.

# File lib/ruby_yacht/runner/command.rb, line 47
def parse_positional_arguments(arguments)
end
project_named(name) click to toggle source

This method finds a project by name.

If the name is nil, this will use the default project. If the name is provided, but does not match any project, this will log an error and return nil.

### Parameters

  • **name: Symbol** The name of the project.

# File lib/ruby_yacht/runner/command.rb, line 93
def project_named(name)
  if self.project_name
    project = projects.find { |p| p.name == name }
    unless project
      log "There is no project named #{name}"
    end
    project
  else
    self.default_project
  end
end
projects() click to toggle source

This method gets the projects that have been configured.

# File lib/ruby_yacht/runner/command.rb, line 74
def projects
  RubyYacht.configuration.projects
end
run() click to toggle source

This method runs the logic for the command.

The default implementation raises an exception. Subclasses must implement this with their command-specific logic.

### Returns

This must return a Boolean indicating whether the command succeeded or not.

# File lib/ruby_yacht/runner/command.rb, line 59
def run
  raise "Run method not defined for #{self}"
end
system(command) click to toggle source

This method sends a command to the system.

This is a proxy for Kernel.system that allows us to stub out and test the system interaction.

### Parameters

  • **command: String** The command to execute.

# File lib/ruby_yacht/runner/command.rb, line 156
def system(command)
  Kernel.system(command)
end