Module: Sgpg::Helper

Defined in:
lib/sgpg/helper.rb

Overview

Reusable functions for the program

Class Method Summary collapse

Class Method Details

.auth?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
# File 'lib/sgpg/helper.rb', line 8

def self.auth?
  return :root if Process.uid == '0'
  return :doas if File.exist?('/bin/doas') || File.exist?('/sbin/doas')
  return :sudo if File.exist?('/bin/sudo') || File.exist?('/sbin/sudo')
end

.chmod(perm, file_path) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sgpg/helper.rb', line 34

def self.chmod(perm, file_path)
  case auth?
  when :root then FileUtils.chmod perm, file_path, verbose: true
  when :sudo then system('sudo', 'chmod', perm, file_path)
  when :doas then system('doas', 'chmod', perm, file_path)
  end
end

.mkdir(path_dir) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/sgpg/helper.rb', line 22

def self.mkdir(path_dir)
  case auth?
  when :root then FileUtils.mkdir(path_dir)
  when :sudo then system('sudo', 'mkdir', '-p', path_dir)
  when :doas then system('doas', 'mkdir', '-p', path_dir)
  end
end

.mv(src, dest) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/sgpg/helper.rb', line 14

def self.mv(src, dest)
  case auth?
  when :root then FileUtils.mv(src, dest)
  when :sudo then system('sudo', 'mv', src, dest)
  when :doas then system('doas', 'mv', src, dest)
  end
end

.search_destObject



30
31
32
# File 'lib/sgpg/helper.rb', line 30

def self.search_dest
  Dir.exist?("#{Sgpg::KEYDIR}/Persistent") ? "#{Sgpg::KEYDIR}/Persistent" : Sgpg::KEYDIR
end