lib/wright/util/user.rb in wright-0.3.2 vs lib/wright/util/user.rb in wright-0.4.0
- old
+ new
@@ -44,11 +44,11 @@
return object.to_i unless object.is_a?(String)
type == :user ? Etc.getpwnam(object).uid : Etc.getgrnam(object).gid
end
private_class_method :to_id
- # Returns the the next free uid in a range.
+ # Returns the next free uid in a range.
#
# @param uid_range [Range] the uid range
#
# @example
# Wright::Util::User.next_free_uid(1...500)
@@ -58,11 +58,11 @@
# @raise [RuntimeError] if there are no free uids in the range
def self.next_free_uid(uid_range)
next_free_id(uid_range, :uid)
end
- # Returns the the next free gid in a range.
+ # Returns the next free gid in a range.
#
# @param gid_range [Range] the gid range
#
# @example
# Wright::Util::User.next_free_gid(1...500)
@@ -90,8 +90,40 @@
def self.id_iterator(id_type)
fail ArgumentError unless [:uid, :gid].include?(id_type)
id_type == :uid ? Etc.method(:passwd) : Etc.method(:group)
end
private_class_method :id_iterator
+
+ # Returns a user entry.
+ #
+ # @param username [String] the name of the user
+ #
+ # @example
+ # Wright::Util::User.safe_getpwnam('this_user_does_not_exist')
+ # # => nil
+ #
+ # @return [Struct::Passwd, nil] the user entry or nil if the
+ # user does not exist
+ def self.safe_getpwnam(username)
+ Etc.getpwnam(username)
+ rescue ArgumentError
+ nil
+ end
+
+ # Returns a group entry.
+ #
+ # @param groupname [String] the name of the group
+ #
+ # @example
+ # Wright::Util::User.safe_getgrnam('this_group_does_not_exist')
+ # # => nil
+ #
+ # @return [Struct::Group, nil] the group entry or nil if the
+ # group does not exist
+ def self.safe_getgrnam(groupname)
+ Etc.getgrnam(groupname)
+ rescue ArgumentError
+ nil
+ end
end
end
end