Class: Minke::Helpers::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/minke/helpers/shell.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Shell

Returns a new instance of Shell.



4
5
6
# File 'lib/minke/helpers/shell.rb', line 4

def initialize logger
  @logger = logger
end

Instance Method Details

#execute(command, ignore_error = false) ⇒ Object

Executes a shell command and returns the return status



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/minke/helpers/shell.rb', line 10

def execute command, ignore_error=false
  @logger.debug command
  
  Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
    while line = stdout_err.gets
      @logger.debug line
    end
    
    exit_status = wait_thr.value
    unless exit_status.success? || ignore_error == true
      raise "Error executing command: #{command}"
    end
  end
end

#execute_and_return(command) ⇒ Object



25
26
27
28
# File 'lib/minke/helpers/shell.rb', line 25

def execute_and_return command
  log = `#{command}`
  return log.strip
end

#exist?(filename) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/minke/helpers/shell.rb', line 46

def exist? filename
  File.exist? filename
end

#mktmpdirObject



30
31
32
# File 'lib/minke/helpers/shell.rb', line 30

def mktmpdir
  Dir.mktmpdir
end

#read_file(filename) ⇒ Object



42
43
44
# File 'lib/minke/helpers/shell.rb', line 42

def read_file filename
  File.open(filename, 'rb') { |file| file.read }.strip
end

#remove_entry_secure(dir) ⇒ Object



34
35
36
# File 'lib/minke/helpers/shell.rb', line 34

def remove_entry_secure dir
  FileUtils.remove_entry_secure dir
end

#write_file(filename, data) ⇒ Object



38
39
40
# File 'lib/minke/helpers/shell.rb', line 38

def write_file filename, data
  File.open(filename, 'w') { |file| file.write(data) }
end