commit:     a171a801ebaeb0020bfa183b7da1f202ea45ae5f
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sun Jun  9 06:19:48 2019 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Jun 20 08:16:48 2019 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=a171a801

user.eclass: Support getting & setting comment field

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 eclass/user.eclass | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/eclass/user.eclass b/eclass/user.eclass
index fc883c96535..0e7aa43d893 100644
--- a/eclass/user.eclass
+++ b/eclass/user.eclass
@@ -413,6 +413,27 @@ egetshell() {
        egetent passwd "$1" | cut -d: -f${pos}
 }
 
+# @FUNCTION: egetcomment
+# @USAGE: <user>
+# @DESCRIPTION:
+# Gets the comment field for the specified user.
+egetcomment() {
+       local pos
+
+       [[ $# -eq 1 ]] || die "usage: egetshell <user>"
+
+       case ${CHOST} in
+       *-freebsd*|*-dragonfly*)
+               pos=8
+               ;;
+       *)      # Linux, NetBSD, OpenBSD, etc...
+               pos=5
+               ;;
+       esac
+
+       egetent passwd "$1" | cut -d: -f${pos}
+}
+
 # @FUNCTION: esethome
 # @USAGE: <user> <homedir>
 # @DESCRIPTION:
@@ -546,4 +567,60 @@ esetshell() {
        esac
 }
 
+# @FUNCTION: esetcomment
+# @USAGE: <user> <comment>
+# @DESCRIPTION:
+# Update the comment field in a platform-agnostic way.
+# Required parameters is the username and the new comment.
+esetcomment() {
+       _assert_pkg_ebuild_phase ${FUNCNAME}
+
+       # get the username
+       local euser=$1; shift
+       if [[ -z ${euser} ]] ; then
+               eerror "No username specified !"
+               die "Cannot call esetcomment without a username"
+       fi
+
+       # lets see if the username already exists
+       if [[ -z $(egetent passwd "${euser}") ]] ; then
+               ewarn "User does not exist, cannot set comment -- skipping."
+               return 1
+       fi
+
+       # handle comment
+       local ecomment=$1; shift
+       if [[ -z ${ecomment} ]] ; then
+               eerror "No comment specified !"
+               die "Cannot call esetcomment without a comment"
+       fi
+
+       # exit with no message if comment is up to date
+       if [[ $(egetcomment "${euser}") == ${ecomment} ]]; then
+               return 0
+       fi
+
+       einfo "Updating comment for user '${euser}' ..."
+       einfo " - Comment: ${ecomment}"
+
+       # update the comment
+       case ${CHOST} in
+       *-freebsd*|*-dragonfly*)
+               pw usermod "${euser}" -c "${ecomment}" && return 0
+               [[ $? == 8 ]] && eerror "${euser} is in use, cannot update 
comment"
+               eerror "There was an error when attempting to update the 
comment for ${euser}"
+               eerror "Please update it manually on your system:"
+               eerror "\t pw usermod \"${euser}\" -c \"${ecomment}\""
+               ;;
+
+       *)
+               usermod -c "${ecomment}" "${euser}" && return 0
+               [[ $? == 8 ]] && eerror "${euser} is in use, cannot update 
comment"
+               eerror "There was an error when attempting to update the 
comment for ${euser}"
+               eerror "Please update it manually on your system (as root):"
+               eerror "\t usermod -c \"${ecomment}\" \"${euser}\""
+               ;;
+       esac
+}
+
 fi

Reply via email to