pkg/utils: Add function to get the name of the sudoers group

https://github.com/containers/toolbox/pull/318
This commit is contained in:
Harry Míchal 2020-04-30 21:29:01 +02:00 committed by Debarshi Ray
parent 188c92c76e
commit 31f1b741c0

View file

@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"sort"
"syscall"
@ -127,6 +128,24 @@ func GetEnvOptionsForPreservedVariables() []string {
return envOptions
}
// GetGroupForSudo returns the name of the sudoers group.
//
// Some distros call it 'sudo' (eg. Ubuntu) and some call it 'wheel' (eg. Fedora).
func GetGroupForSudo() (string, error) {
logrus.Debug("Looking up group for sudo")
groups := []string{"sudo", "wheel"}
for _, group := range groups {
if _, err := user.LookupGroup(group); err == nil {
logrus.Debugf("Group for sudo is %s", group)
return group, nil
}
}
return "", errors.New("group for sudo not found")
}
// ShortID shortens provided id to first 12 characters.
func ShortID(id string) string {
if len(id) > idTruncLength {