module Helpers

Public Class Methods

add_file(path, content = '') click to toggle source
# File lib/getch/helpers.rb, line 29
def self.add_file(path, content = '')
  File.write path, content if ! File.exist? path
end
cp(src, dest) click to toggle source
# File lib/getch/helpers.rb, line 41
def self.cp(src, dest)
  raise "Src file #{src} no found" unless File.exist? src
  FileUtils.cp(src, dest)
end
create_dir(path, perm = 0755) click to toggle source
# File lib/getch/helpers.rb, line 25
def self.create_dir(path, perm = 0755)
  FileUtils.mkdir_p path, mode: perm if ! Dir.exist?(path)
end
efi?() click to toggle source
# File lib/getch/helpers.rb, line 6
def self.efi?
  Dir.exist? '/sys/firmware/efi/efivars'
end
exec_or_die(cmd) click to toggle source
# File lib/getch/helpers.rb, line 18
def self.exec_or_die(cmd)
  _, stderr, status = Open3.capture3(cmd)
  unless status.success?
    raise "Problem running #{cmd}, stderr was:\n#{stderr}"
  end
end
get_file_online(url, dest) click to toggle source
# File lib/getch/helpers.rb, line 10
def self.get_file_online(url, dest)
  URI.open(url) do |l|
    File.open(dest, "wb") do |f|
      f.write(l.read)
    end
  end
end
grep?(file, regex) click to toggle source
# File lib/getch/helpers.rb, line 46
def self.grep?(file, regex)
  is_found = false
  return is_found if ! File.exist? file
  File.open(file) do |f|
    f.each do |line|
      is_found = true if line.match(regex)
    end
  end
  is_found
end
mkdir(dir) click to toggle source
# File lib/getch/helpers.rb, line 33
def self.mkdir(dir)
  FileUtils.mkdir_p dir if ! Dir.exist? dir
end
partuuid(dev) click to toggle source
# File lib/getch/helpers.rb, line 64
def self.partuuid(dev)
  `lsblk -o PARTUUID #{dev}`.match(/[\w]+-[\w]+-[\w]+-[\w]+-[\w]+/)
end
pool_id(dev) click to toggle source

Used with ZFS for the pool name

# File lib/getch/helpers.rb, line 77
def self.pool_id(dev)
  if dev.match(/[0-9]/)
    sleep 1
    `lsblk -o PARTUUID #{dev}`.delete("\n").delete("PARTUUID").match(/[\w]{5}/)
  else
    puts "Please, enter a pool name"
    while true
      print "\n> "
      value = gets
      if value.match(/[a-z]{4,20}/)
        return value
      end
      puts "Bad name, you enter: #{value}"
      puts "Valid pool name use character only, between 4-20."
    end
  end
end
sys(cmd) click to toggle source
# File lib/getch/helpers.rb, line 57
def self.sys(cmd)
  system(cmd)
  unless $?.success?
    raise "Error with #{cmd}"
  end
end
touch(file) click to toggle source
# File lib/getch/helpers.rb, line 37
def self.touch(file)
  File.write file, '' if ! File.exist? file
end
uuid(dev) click to toggle source
# File lib/getch/helpers.rb, line 68
def self.uuid(dev)
  Dir.glob("/dev/disk/by-uuid/*").each { |f|
    if File.readlink(f).match(/#{dev}/)
      return f.delete_prefix("/dev/disk/by-uuid/")
    end
  }
end