class KuberKit::Shell::LocalShell

Constants

MAX_LINES_TO_PRINT

Public Instance Methods

delete(file_path) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 79
def delete(file_path)
  exec!("rm #{file_path}")
end
dir_exists?(dir_path) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 87
def dir_exists?(dir_path)
  exec!("test -d #{dir_path} && echo 'true' || echo 'false'", log_command: false) == 'true'
end
exec!(command, log_command: true) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 12
def exec!(command, log_command: true)
  command_number = command_counter.get_number.to_s.rjust(2, "0")
  
  if log_command
    ui.print_debug("LocalShell", "Execute: [#{command_number}]: #{command.to_s.cyan}")
  end

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

  if result && result != "" && log_command
    print_result = result
    print_result_lines = print_result.split("\n")

    if print_result_lines.count >= MAX_LINES_TO_PRINT
      print_result = [
        "[Result is too long, showing only first and last items]".yellow, 
        print_result_lines.first, 
        "[#{print_result_lines.count - 2} lines not showing]".yellow, 
        print_result_lines.last
    ].join("\n")
    end

    ui.print_debug("LocalShell", "Finished [#{command_number}] with result: \n  ----\n#{print_result.grey}\n  ----")
  end

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

  result
end
file_exists?(file_path) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 83
def file_exists?(file_path)
  exec!("test -f #{file_path} && echo 'true' || echo 'false'", log_command: false) == 'true'
end
interactive!(command, log_command: true) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 47
def interactive!(command, log_command: true)
  command_number = command_counter.get_number.to_s.rjust(2, "0")
  
  if log_command
    ui.print_debug("LocalShell", "Interactive: [#{command_number}]: #{command.to_s.cyan}")
  end

  result = system(wrap_command_with_pid(command))

  if !$?.success?
    raise ShellError, "Shell command failed: #{command}\r\n#{result}"
  end
end
list_dirs(path) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 103
def list_dirs(path)
  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
read(file_path) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 65
def read(file_path)
  File.read(file_path)
end
recursive_list_files(path, name: nil) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 91
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
sync(local_path, remote_path, exclude: nil, delete: true) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 61
def sync(local_path, remote_path, exclude: nil, delete: true)
  rsync_commands.rsync(self, local_path, remote_path, exclude: exclude, delete: delete)
end
wrap_command_with_pid(command) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 115
def wrap_command_with_pid(command)
  "KIT=#{Process.pid} #{command}"
end
write(file_path, content) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 69
def write(file_path, content)
  ensure_directory_exists(file_path)

  File.write(file_path, content)

  ui.print_debug("LocalShell", "Created file #{file_path.to_s.cyan}\r\n  ----\r\n#{content.grey}\r\n  ----")

  true
end

Private Instance Methods

ensure_directory_exists(file_path) click to toggle source
# File lib/kuber_kit/shell/local_shell.rb, line 120
def ensure_directory_exists(file_path)
  dir_path = File.dirname(file_path)

  unless Dir.exists?(dir_path)
    FileUtils.mkdir_p(dir_path)
  end
end