Class: Lexicon::Common::ShellExecutor

Inherits:
Object
  • Object
show all
Includes:
Mixin::Finalizable
Defined in:
lib/lexicon/common/shell_executor.rb

Instance Method Summary collapse

Methods included from Mixin::Finalizable

included

Constructor Details

#initializeShellExecutor

Returns a new instance of ShellExecutor.



8
9
10
# File 'lib/lexicon/common/shell_executor.rb', line 8

def initialize
  @command_dir = Dir.mktmpdir
end

Instance Method Details

#execute(command) ⇒ String

Parameters:

  • command (String)

Returns:

  • (String)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lexicon/common/shell_executor.rb', line 14

def execute(command)
  cmd = Tempfile.new('command-', @command_dir)
  cmd.write <<~BASH
    #!/usr/bin/env bash
    set -e

    #{command}
  BASH
  cmd.close

  `bash #{cmd.path}`
ensure
  cmd.close
  cmd.unlink
end

#finalizeObject



30
31
32
33
34
# File 'lib/lexicon/common/shell_executor.rb', line 30

def finalize
  if !@command_dir.nil?
    FileUtils.rm_rf(@command_dir)
  end
end