Class: KuberKit::Shell::LocalShell

Inherits:
AbstractShell show all
Defined in:
lib/kuber_kit/shell/local_shell.rb

Constant Summary

Constants inherited from AbstractShell

AbstractShell::DirNotFoundError, AbstractShell::ShellError

Instance Method Summary collapse

Instance Method Details

#exec!(command) ⇒ Object



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

def exec!(command)
  command_number = command_counter.get_number.to_s.rjust(2, "0")
  
  logger.info("Executed command [#{command_number}]: #{command.to_s.cyan}")

  result = nil
  IO.popen(command, err: [:child, :out]) do |io|
    result = io.read.chomp.strip
  end

  if result && result != ""
    logger.info("Finished command [#{command_number}] with result: \n#{result.grey}")
  end

  if $?.exitstatus != 0
    raise ShellError, "Shell command failed: #{command}\r\n#{result}"
  end

  result
end

#read(file_path) ⇒ Object



30
31
32
# File 'lib/kuber_kit/shell/local_shell.rb', line 30

def read(file_path)
  File.read(file_path)
end

#recursive_list_files(path, name: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kuber_kit/shell/local_shell.rb', line 44

def recursive_list_files(path, name: nil)
  command = %Q{find -L #{path}  -type f}
  command += " -name #{name}" if name
  exec!(command).split(/[\r\n]+/)
rescue => e
  if e.message.include?("No such file or directory")
    raise DirNotFoundError.new("Dir not found: #{path}")
  else
    raise e
  end
end

#write(file_path, content) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/kuber_kit/shell/local_shell.rb', line 34

def write(file_path, content)
  ensure_directory_exists(file_path)

  File.write(file_path, content)

  logger.info("Created file #{file_path.to_s.cyan}\r\n#{content.grey}")

  true
end