Class: Carioca::Services::Toolbox

Inherits:
Object
  • Object
show all
Defined in:
lib/carioca/services/toolbox.rb

Overview

Exiter namespace

Class Method Summary collapse

Class Method Details

.check_unicode_term(description: 'Check if terminal support unicode', describe: false) ⇒ Boolean

check if unicode must be used with term ENV

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'lib/carioca/services/toolbox.rb', line 95

def self.check_unicode_term(description: 'Check if terminal support unicode', describe: false)
  return description if describe
  return false unless ENV.include? 'TERM'

  (ENV.values_at('LC_ALL', 'LC_CTYPE', 'LANG').compact.include?('UTF-8') and ENV.values_at('TERM').include? 'xterm')
end

.describeObject



8
9
10
11
12
13
14
15
16
# File 'lib/carioca/services/toolbox.rb', line 8

def self.describe
  result = {}
  methods(false).each do |meth|
    next if meth == :describe

    result[meth] = send meth, describe: true
  end
  result
end

.get_processes(patterns: [], pattern: nil, full: false, description: 'Get the list of running processus', describe: false) ⇒ String|Array

facility for retreiving PID from process query

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [Array] (Hash)

    a customizable set of options

  • [Bool] (Hash)

    a customizable set of options

Returns:

  • (String|Array)

    PID or data structure



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/carioca/services/toolbox.rb', line 39

def self.get_processes(patterns: [], pattern: nil, full: false, description: 'Get the list of running processus', describe: false)
  return description if describe

  patterns << pattern if pattern
  res = ::PS.get_all_processes
  patterns.each do |item|
    res = res.find_processes item
  end
  if full
    res
  else
    res.pick_attr('PID')
  end
end

.group_root(description: 'Get the local system root groupname ', describe: false) ⇒ String

return the ‘root’ group name : root or wheel

Returns:

  • (String)

    name



28
29
30
31
32
# File 'lib/carioca/services/toolbox.rb', line 28

def self.group_root(description: 'Get the local system root groupname ', describe: false)
  return description if describe

  Etc.getgrgid(0).name
end

.root?(description: 'Verify if active current processus is running as root', describe: false) ⇒ Bool

facility to verifying if the active process run as root

Returns:

  • (Bool)

    status



87
88
89
90
91
# File 'lib/carioca/services/toolbox.rb', line 87

def self.root?(description: 'Verify if active current processus is running as root', describe: false)
  return description if describe

  Process.uid.zero?
end

.search_file_in_gem(gem: nil, file: nil, description: 'Retrieve absolute path of a file in a specific gem', describe: false) ⇒ String, False

facility to find a file in gem path

Parameters:

  • _gem (String)

    a Gem name

  • _file (String)

    a file relative path in the gem

Returns:

  • (String)

    the path of the file, if found.

  • (False)

    if not found



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/carioca/services/toolbox.rb', line 59

def self.search_file_in_gem(gem: nil, file: nil, description: 'Retrieve absolute path of a file in a specific gem', describe: false)
  return description if describe

  if Gem::Specification.respond_to?(:find_by_name)
    begin
      spec = Gem::Specification.find_by_name(gem)
    rescue LoadError
      spec = nil
    end
  else
    spec = Gem.searcher.find(gem)
  end
  if spec
    res = if Gem::Specification.respond_to?(:find_by_name)
            spec.lib_dirs_glob.split('/')
          else
            Gem.searcher.lib_dirs_for(spec).split('/')
          end
    res.pop
    services_path = res.join('/').concat("/#{file}")
    return services_path if File.exist?(services_path)

  end
  false
end

.user_root(description: 'Get the local system root username ', describe: false) ⇒ String

return the ‘root’ name

Returns:

  • (String)

    name



20
21
22
23
24
# File 'lib/carioca/services/toolbox.rb', line 20

def self.user_root(description: 'Get the local system root username ', describe: false)
  return description if describe

  Etc.getpwuid(0).name
end