From e37a80cd9b601adc16894d3b6fb526ae8f4c846b Mon Sep 17 00:00:00 2001 From: Joseph Richey Date: Wed, 30 Aug 2017 04:49:39 -0700 Subject: [PATCH] util: Added parsing and effective user functions --- util/util.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/util/util.go b/util/util.go index c02ea0e..3de4a1a 100644 --- a/util/util.go +++ b/util/util.go @@ -27,6 +27,8 @@ import ( "bufio" "math" "os" + "os/user" + "strconv" "unsafe" ) @@ -105,3 +107,23 @@ func ReadLine() (string, error) { scanner.Scan() return scanner.Text(), scanner.Err() } + +// AtoiOrPanic converts a string to an int or it panics. Should only be used in +// situations where the input MUST be a decimal number. +func AtoiOrPanic(input string) int { + i, err := strconv.Atoi(input) + if err != nil { + panic(err) + } + return i +} + +// EffectiveUser returns the user entry corresponding to the effective user. +func EffectiveUser() (*user.User, error) { + return user.LookupId(strconv.Itoa(os.Geteuid())) +} + +// IsUserRoot checks if the effective user is root. +func IsUserRoot() bool { + return os.Geteuid() == 0 +} -- 2.39.5