Class: RubyYacht::Runner::Command
- Inherits:
-
Object
- Object
- RubyYacht::Runner::Command
- Defined in:
- lib/ruby_yacht/runner/command.rb
Overview
This class is the base class for the commands that users can run for managing their system.
Direct Known Subclasses
Build, BuildImages, Checkout, CreateNewProject, Help, Implode, RunContainers, Services, Shell, UpdateHosts
Class Method Summary collapse
-
.command ⇒ Object
The name of the command that users run to trigger this command.
-
.description ⇒ Object
This class gets a short description of the command.
-
.short_script_name ⇒ Object
This method gets the name of the script that was invoked to run the commands.
Instance Method Summary collapse
-
#backtick(command) ⇒ Object
This method runs a command using the backtick operator.
-
#default_project ⇒ Object
The project that we will use if they do not provide one from the command line.
-
#docker(command) ⇒ Object
This method sends a command to docker.
-
#docker_machine ⇒ Object
This method gets the path to the docker-machine binary.
-
#get_machine_info(property) ⇒ Object
This method gets a property from the default docker machine.
-
#log(message) ⇒ Object
This method logs a message to standard out.
-
#option_parser ⇒ Object
This method gets the command-line options for invoking this script.
-
#parse_positional_arguments(arguments) ⇒ Object
This method extracts the positional arguments from the command line.
-
#project_named(name) ⇒ Object
This method finds a project by name.
-
#projects ⇒ Object
This method gets the projects that have been configured.
-
#run ⇒ Object
This method runs the logic for the command.
-
#system(command) ⇒ Object
This method sends a command to the system.
Class Method Details
.command ⇒ Object
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.
12 13 14 |
# File 'lib/ruby_yacht/runner/command.rb', line 12 def self.command raise "Command not defined for #{self}" end |
.description ⇒ Object
This class gets a short description of the command. This implementation raises an error. Subclasses must implement it to provide the command description.
19 20 21 |
# File 'lib/ruby_yacht/runner/command.rb', line 19 def self.description raise "Description not defined for #{self}" end |
.short_script_name ⇒ Object
This method gets the name of the script that was invoked to run the commands.
This will just be the filename of the script.
69 70 71 |
# File 'lib/ruby_yacht/runner/command.rb', line 69 def self.short_script_name return File.basename($0) end |
Instance Method Details
#backtick(command) ⇒ Object
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.
169 170 171 |
# File 'lib/ruby_yacht/runner/command.rb', line 169 def backtick(command) `#{command}` end |
#default_project ⇒ Object
The project that we will use if they do not provide one from the command line.
80 81 82 |
# File 'lib/ruby_yacht/runner/command.rb', line 80 def default_project projects.first end |
#docker(command) ⇒ Object
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.
145 146 147 |
# File 'lib/ruby_yacht/runner/command.rb', line 145 def docker(command) system "docker #{command}" end |
#docker_machine ⇒ Object
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
124 125 126 127 |
# 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) ⇒ Object
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.
113 114 115 116 |
# 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) ⇒ Object
This method logs a message to standard out. It allows us to stub out and test the logging behavior of the commands.
133 134 135 |
# File 'lib/ruby_yacht/runner/command.rb', line 133 def log() $stdout.puts end |
#option_parser ⇒ Object
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.
30 31 32 33 34 |
# File 'lib/ruby_yacht/runner/command.rb', line 30 def option_parser OptionParser.new do || . = "Usage: #{Command.short_script_name} #{self.class.command}\n\n#{self.class.description}" end end |
#parse_positional_arguments(arguments) ⇒ Object
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.
47 48 |
# File 'lib/ruby_yacht/runner/command.rb', line 47 def parse_positional_arguments(arguments) end |
#project_named(name) ⇒ Object
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.
93 94 95 96 97 98 99 100 101 102 103 |
# 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 ⇒ Object
This method gets the projects that have been configured.
74 75 76 |
# File 'lib/ruby_yacht/runner/command.rb', line 74 def projects RubyYacht.configuration.projects end |
#run ⇒ Object
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.
59 60 61 |
# File 'lib/ruby_yacht/runner/command.rb', line 59 def run raise "Run method not defined for #{self}" end |
#system(command) ⇒ Object
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.
156 157 158 |
# File 'lib/ruby_yacht/runner/command.rb', line 156 def system(command) Kernel.system(command) end |