Module: Vos::Box::Shell

Included in:
Vos::Box
Defined in:
lib/vos/box/shell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#env(command_or_env_variables = nil, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vos/box/shell.rb', line 33

def env command_or_env_variables = nil, &block
  @env ||= default_env

  if block
    before = env.clone
    begin
      if variables = command_or_env_variables
        raise 'invalid arguments' unless variables.is_a? Hash
        @env.merge! variables
      end
      block.call
    ensure
      @env = before
    end
  else
    if command_or_env_variables == nil
      @env
    elsif command_or_env_variables.is_a? String
      cmd = command_or_env_variables
      env_str = env.to_a.collect{|k, v| "#{k}=#{v}"}.join(' ')
      wrap_cmd env_str, cmd
    elsif command_or_env_variables.is_a? Hash
      variables = command_or_env_variables
      @env.merge! variables
    else
      raise 'invalid arguments'
    end
  end
end

Instance Method Details

#bash(cmd, *args) ⇒ Object



4
5
6
# File 'lib/vos/box/shell.rb', line 4

def bash cmd, *args
  self['/'].bash cmd, *args
end

#bash_without_path(cmd, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vos/box/shell.rb', line 8

def bash_without_path cmd, *args
  check = args.shift if args.first.is_a?(Regexp)
  options = args.last || {}

  cmd = env cmd
  code, stdout_and_stderr = open{driver.bash cmd}

  unless code == 0
    puts stdout_and_stderr
    raise "can't execute '#{cmd}'!"
  end

  if check and (stdout_and_stderr !~ check)
    puts stdout_and_stderr
    raise "output not match with #{check.inspect}!"
  end

  stdout_and_stderr
end

#default_envObject



62
63
64
# File 'lib/vos/box/shell.rb', line 62

def default_env
  {}
end

#exec(cmd) ⇒ Object



28
29
30
# File 'lib/vos/box/shell.rb', line 28

def exec cmd
  open{driver.exec(env(cmd))}
end

#home(path = nil) ⇒ Object



70
71
72
73
# File 'lib/vos/box/shell.rb', line 70

def home path = nil
  open{@home = bash('cd ~; pwd').gsub("\n", '')} unless @home
  path ? self[@home][path] : self[@home]
end

#wrap_cmd(env_str, cmd) ⇒ Object



65
66
67
# File 'lib/vos/box/shell.rb', line 65

def wrap_cmd env_str, cmd
  %(#{env_str}#{' && ' unless env_str.empty?}#{cmd})
end