Class: RubyYacht::Runner::Shell

Inherits:
Command
  • Object
show all
Defined in:
lib/ruby_yacht/runner/shell.rb

Overview

This class provides a command for opening a shell in an app container.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#backtick, #default_project, #docker, #get_machine_info, #log, #project_named, #projects, short_script_name, #system

Instance Attribute Details

#app_nameObject

The name of the app that we are opening a shell in.


16
17
18
# File 'lib/ruby_yacht/runner/shell.rb', line 16

def app_name
  @app_name
end

#commandObject

The name of the command that we are invoking in the shell.


19
20
21
# File 'lib/ruby_yacht/runner/shell.rb', line 19

def command
  @command
end

#project_nameObject

The name of the project that the app is in.


13
14
15
# File 'lib/ruby_yacht/runner/shell.rb', line 13

def project_name
  @project_name
end

Class Method Details

.commandObject

The name of the command.


5
# File 'lib/ruby_yacht/runner/shell.rb', line 5

def self.command; 'shell'; end

.descriptionObject

The description of the command.


8
9
10
# File 'lib/ruby_yacht/runner/shell.rb', line 8

def self.description
  "Open a shell in a container"
end

Instance Method Details

#option_parserObject

This method gets the command-line options for the command.


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_yacht/runner/shell.rb', line 22

def option_parser
  OptionParser.new do |options|
    options.banner = "Usage: #{Command.short_script_name} shell [options] [APP] [command]"
    options.separator ""
    options.separator "The command is optional. If you omit it, this will open a bash shell"
    options.separator ""
    options.separator "Options:"

    options.on('-p', '--project PROJECT', "The project with the app we are opening the sell in. Default: #{default_project.name}") do |name|
      self.project_name = name.to_sym
    end
  end
end

#parse_positional_arguments(arguments) ⇒ Object

This method extracts the arguments from the command line.

Parameters

  • arguments: Array The command-line arguments.

41
42
43
44
45
46
47
# File 'lib/ruby_yacht/runner/shell.rb', line 41

def parse_positional_arguments(arguments)
  self.app_name = arguments.shift
  self.command = arguments.join(' ')
  if self.command == ''
    self.command = 'bash'
  end
end

#runObject

This method runs the logic for the command.


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_yacht/runner/shell.rb', line 50

def run
  if app_name.nil?
    log "You must provide an app name"
    log "Run #{Command.short_script_name} help shell for more information"
    return false
  end

  project = self.project_named(project_name)
  return false unless project

  app = project.apps.find { |a| a.name == app_name.to_sym }
  container = app.container_name(project)
  exec("docker exec -it #{container} bash -c 'cd /var/code; TERM=xterm #{command}'")
  true
end