module Kleiber::Commands

Public Instance Methods

apply_env_line(env_to_app) click to toggle source

Returns string of environment variable definitions @param [Hash] env_to_app final defined environment variables list @example

'FIRST_HOST=192.168.22.20 FIRST_PORT=3030 SECOND_HOST=192.168.22.21 SECOND_PORT=3030'

@return [String] environment definition string

# File lib/kleiber/commands.rb, line 148
def apply_env_line(env_to_app)
  env_to_app.map { |e| e.join('=') }.join(' ')
end
handle_halt(_params) click to toggle source

Returns command line for `vagrant halt` command. @param [Hash] _params this handler doesn't use params @return [String] line for vagrant halt execution

# File lib/kleiber/commands.rb, line 107
def handle_halt(_params)
  vagrant_(:halt)
end
handle_reload(params) click to toggle source

Returns command line for `vagrant reload` command. Evaluates tasks executions line by taken parameters @param [Hash] params params hash with values to execute with @return [String] line for vagrant reload execution

# File lib/kleiber/commands.rb, line 98
def handle_reload(params)
  line = [vagrant_(:reload)]
  line << handle_ssh(params) unless params[:tasks].empty?
  line.join(' && ')
end
handle_ssh(params) click to toggle source

Returns command line for `vagrant ssh` command. Evaluates tasks executions line by taken parameters @param [Hash] params params hash with values to execute with @return [String] line for vagrant ssh execution

# File lib/kleiber/commands.rb, line 88
def handle_ssh(params)
  line = [vagrant_(:ssh)]
  line << ssh_exec_line(params) unless params[:tasks].empty?
  line.join(' ')
end
handle_up(params) click to toggle source

Returns command line for `vagrant up` command. Evaluates tasks executions line by taken parameters @param [Hash] params params hash with values to execute with @return [String] line for vagrant up execution

# File lib/kleiber/commands.rb, line 78
def handle_up(params)
  line = [vagrant_(:up)]
  line << handle_ssh(params) unless params[:tasks].empty?
  line.join(' && ')
end
in_machine(params) click to toggle source

Returns commandline need to execute in vagrant machine. Actually, it's an environment settings and chain of tasks @example

'FIRST_HOST=192.168.22.20 cd /vagrant && bundle exec rails server'

@param [Hash] params params hash with values to execute with @return [String]

# File lib/kleiber/commands.rb, line 126
def in_machine(params)
  [apply_env_line(params[:env]), tasks_line(params[:tasks])].join(' ')
end
scriptify(command) click to toggle source

Returns text of bashscript file with command @param [String] command command line to execute @return [String] scriptlike string

# File lib/kleiber/commands.rb, line 70
def scriptify(command)
  ['#!/bin/bash', 'unset RUBYLIB', command].join("\n")
end
ssh_exec_line(params) click to toggle source

Returns option which executes tasks in vagrant @example

"-c 'FIRST_HOST=192.168.22.20 cd /vagrant && bundle exec rails server'"

@param [Hash] params params hash with values to execute with @return [String] option

# File lib/kleiber/commands.rb, line 116
def ssh_exec_line(params)
  Cocaine::CommandLine.new('', '-c :in_machine').command(in_machine: in_machine(params))
end
tasks_line(tasks_to_run) click to toggle source

Returns tasks chain command line @param [Hash] tasks_to_run final defined tasks hash @example

'cd /vagrant && bundle install --binstubs && bundle exec rackup'

@return [String] tasks commandline

# File lib/kleiber/commands.rb, line 135
def tasks_line(tasks_to_run)
  line = ['cd /vagrant']
  line += tasks_to_run.reduce({}) do |result, (key, value)|
    result.merge(key => tasks[key] || value)
  end.values
  line.join(' && ')
end
terminal_execute(command) click to toggle source

Executes command in new tab @param [String] command command line to run

# File lib/kleiber/commands.rb, line 49
def terminal_execute(command)
  with_tmpfile_script(command) do |file|
    Kleiber.terminal.execute(file)
  end
end
vagrant() click to toggle source
# File lib/kleiber/commands.rb, line 7
def vagrant
  Cocaine::CommandLine.new('/usr/bin/vagrant')
end
vagrant_(command) click to toggle source

Returns vagrant command @param [Symbol, String] command vagrant command @return [String] command line

# File lib/kleiber/commands.rb, line 155
def vagrant_(command)
  send("vagrant_#{command}".to_sym).command
end
vagrant_destroy() click to toggle source

Returns vagrant destroy command line @return [Cocaine::CommandLine] vagrant destroy command

# File lib/kleiber/commands.rb, line 37
def vagrant_destroy
  Cocaine::CommandLine.new(vagrant.command, 'destroy')
end
vagrant_halt() click to toggle source

Returns vagrant halt command line @return [Cocaine::CommandLine] vagrant halt command

# File lib/kleiber/commands.rb, line 31
def vagrant_halt
  Cocaine::CommandLine.new(vagrant.command, 'halt')
end
vagrant_provision() click to toggle source

Returns vagrant provision command line @return [Cocaine::CommandLine] vagrant provision command

# File lib/kleiber/commands.rb, line 43
def vagrant_provision
  Cocaine::CommandLine.new(vagrant.command, 'provision')
end
vagrant_reload() click to toggle source

Returns vagrant reload command line @return [Cocaine::CommandLine] vagrant reload command

# File lib/kleiber/commands.rb, line 25
def vagrant_reload
  Cocaine::CommandLine.new(vagrant.command, 'reload')
end
vagrant_ssh() click to toggle source

Returns vagrant ssh command line @return [Cocaine::CommandLine] vagrant ssh command

# File lib/kleiber/commands.rb, line 19
def vagrant_ssh
  Cocaine::CommandLine.new(vagrant.command, 'ssh')
end
vagrant_up() click to toggle source

Returns vagrant up command line @return [Cocaine::CommandLine] vagrant up command

# File lib/kleiber/commands.rb, line 13
def vagrant_up
  Cocaine::CommandLine.new(vagrant.command, 'up')
end
with_tmpfile_script(command) { |file| ... } click to toggle source

Provides command to execute within temporary bashscript file @param [String] command to execute @yieldparam [File] file bashcript to use in terminal instance

# File lib/kleiber/commands.rb, line 58
def with_tmpfile_script(command)
  Tempfile.create([name, '.sh'], '/tmp') do |file|
    file.chmod(0_755)
    file.write(scriptify(command))
    file.close
    yield file
  end
end