module PoiseApplication::Utils

Utility methods for PoiseApplication.

@api public @since 5.0.0

Public Instance Methods

primary_group_for(user) click to toggle source

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

@param user [String, Integer] User to check, if given as an integer this

is used as a UID, otherwise it is the username.

@return [String] @example

attribute(:group, kind_of: [String, Integer], default: lazy { PoiseApplication::Utils.primary_group_for(user) })
# 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