class RubyYacht::Runner::Command
This class is the base class for the commands that users can run for managing their system.
Public Class Methods
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
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
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
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
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
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
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
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
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
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
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
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
This method gets the projects that have been configured.
# File lib/ruby_yacht/runner/command.rb, line 74 def projects RubyYacht.configuration.projects end
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
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