Module: PoiseApplication::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/poise_application/utils.rb

Overview

Utility methods for PoiseApplication.

Since:

  • 5.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.primary_group_for(user) ⇒ String

Try to find the primary group name for a given user.

Examples:

attribute(:group, kind_of: [String, Integer], default: lazy { PoiseApplication::Utils.primary_group_for(user) })

Parameters:

  • user (String, Integer)

    User to check, if given as an integer this is used as a UID, otherwise it is the username.

Returns:

  • (String)

Since:

  • 5.0.0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/poise_application/utils.rb', line 36

def primary_group_for(user)
   # Force a reload in case any users were created earlier in the run.
  Etc.endpwent
  Etc.endgrent
  user = if user.is_a?(Integer)
    Etc.getpwuid(user)
  else
    Etc.getpwnam(user.to_s)
  end
  Etc.getgrgid(user.gid).name
rescue ArgumentError
  # One of the get* calls exploded. ¯\_(ツ)_/¯
  user.to_s
end

Instance Method Details

#primary_group_for(user) ⇒ String

Try to find the primary group name for a given user.

Examples:

attribute(:group, kind_of: [String, Integer], default: lazy { PoiseApplication::Utils.primary_group_for(user) })

Parameters:

  • user (String, Integer)

    User to check, if given as an integer this is used as a UID, otherwise it is the username.

Returns:

  • (String)

Since:

  • 5.0.0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/poise_application/utils.rb', line 36

def primary_group_for(user)
   # Force a reload in case any users were created earlier in the run.
  Etc.endpwent
  Etc.endgrent
  user = if user.is_a?(Integer)
    Etc.getpwuid(user)
  else
    Etc.getpwnam(user.to_s)
  end
  Etc.getgrgid(user.gid).name
rescue ArgumentError
  # One of the get* calls exploded. ¯\_(ツ)_/¯
  user.to_s
end