Class: RubyYacht::Runner::Checkout

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

Overview

This class provides a command for checking out a new branch for an app.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

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

Instance Attribute Details

#app_nameObject

The name of the app.



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

def app_name
  @app_name
end

#branchObject

The branch that we are checking out.



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

def branch
  @branch
end

#project_nameObject

The name of the project that the app is in.



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

def project_name
  @project_name
end

Class Method Details

.commandObject

The name of the command.



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

def self.command; 'checkout'; end

.descriptionObject

The short description for the command.



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

def self.description
  "Check out a new branch for an app"
end

Instance Method Details

#option_parserObject

This OptionParser for parsing command-line flags.

This command accepts the project name as an optional flag.



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

def option_parser
  OptionParser.new do |options|
    options.banner = "Usage: #{Command.short_script_name} checkout [options] [APP] [BRANCH]"
    options.separator "Options:"

    options.on('-p', '--project PROJECT', "The project with the app we are checking out the branch 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 positional arguments from the command line.

This will take the app name and branch from the command line.



38
39
40
41
# File 'lib/ruby_yacht/runner/checkout.rb', line 38

def parse_positional_arguments(arguments)
  self.app_name = arguments.shift
  self.branch = arguments.shift
end

#runObject

This method runs the logic for the command.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_yacht/runner/checkout.rb', line 44

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

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

  app = project.apps.find { |a| a.name == app_name.to_sym }
  container_name = app.container_name
  
  docker "exec #{container_name} bash -c 'cd /var/code; git fetch; git checkout .; git checkout #{branch}; git pull'"
  docker "exec #{container_name} /var/docker/before_startup.bash"
  docker "restart #{container_name}"
  
  true
end