From 56e73a7c2641dddf035a8d404b93bbf102e2fd39 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Tue, 2 Jun 2020 07:10:11 +0200 Subject: [PATCH] Add 'usergroups' state Create `usergroups` state to collect all the groups the user running modulecmd.tcl process is member of. Add an initStateUsergroups procedure to initialize this state, which calls `id -G -n` to fetch these group names. This external command is available on every Unices and its `-G -n` arguments operate the same way. An error will be obtained on Windows platform as `id` command is not known there. initStateUsergroups procedure is recorded in modulecmd.tcl as __initStateUsergroups and renamed if initStateUsergroups procedure is not found from Modules Tcl extension library. A more efficient version of initStateUsergroups will be provided in the library later on (not to depend on an external command call). --- modulecmd.tcl.in | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/modulecmd.tcl.in b/modulecmd.tcl.in index f7832521..db825a16 100644 --- a/modulecmd.tcl.in +++ b/modulecmd.tcl.in @@ -60,6 +60,7 @@ array set g_state_defs [list\ tcl_ext_lib_loaded {0}\ tcl_version [list [info patchlevel]]\ term_columns { initStateTermColumns}\ + usergroups { initStateUsergroups}\ username { initStateUsername}\ ] @@ -352,6 +353,18 @@ proc initStateTermColumns {} { return [expr {![info exists cols] || $cols eq {0} ? 80 : $cols}] } +# Get all groups of user running modulecmd.tcl process +proc __initStateUsergroups {} { + # ensure groups including space in their name (found on Cygwin/MSYS + # platforms) are correctly set as list element + if {[catch { + return [split [string range [runCommand id -G -n -z] 0 end-1] \0] + } errMsg]} { + # fallback if '-z' option is not supported + return [runCommand id -G -n] + } +} + # Get name of user running modulecmd.tcl process proc __initStateUsername {} { return [runCommand id -u -n] @@ -11710,6 +11723,7 @@ if {[catch { if {[info commands readFile] eq {}} { rename ::__readFile ::readFile rename ::__getFilesInDirectory ::getFilesInDirectory + rename ::__initStateUsergroups ::initStateUsergroups rename ::__initStateUsername ::initStateUsername }