diff options
author | Michał Górny <mgorny@gentoo.org> | 2019-06-09 08:19:48 +0200 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2019-06-20 10:16:48 +0200 |
commit | a171a801ebaeb0020bfa183b7da1f202ea45ae5f (patch) | |
tree | dd531c55c65fb603995d935ef29562cb643fe4e5 /eclass/user.eclass | |
parent | user.eclass: Also permit using functions in pkg_*rm phases (diff) | |
download | gentoo-a171a801ebaeb0020bfa183b7da1f202ea45ae5f.tar.gz gentoo-a171a801ebaeb0020bfa183b7da1f202ea45ae5f.tar.bz2 gentoo-a171a801ebaeb0020bfa183b7da1f202ea45ae5f.zip |
user.eclass: Support getting & setting comment field
Signed-off-by: Michał Górny <mgorny@gentoo.org>
Diffstat (limited to 'eclass/user.eclass')
-rw-r--r-- | eclass/user.eclass | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/eclass/user.eclass b/eclass/user.eclass index fc883c965352..0e7aa43d8932 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 |