diff --git a/os_darwin.go b/os_darwin.go new file mode 100644 index 0000000..13477e2 --- /dev/null +++ b/os_darwin.go @@ -0,0 +1,22 @@ +//go:build darwin +// +build darwin + +package util + +import ( + "os/user" + "path/filepath" +) + +// IsSuperUser returns true, if the current user is a super user +// A.K.A root, Administrator etc +func IsSuperUser() bool { + cuser, err := user.Current() + return err == nil && "0" == cuser.Uid +} + +// GetGlobalConfigurationDirectory returns OS specific location for putting +// global configuration files +func GetGlobalConfigurationDirectory(appname string) string { + return filepath.Join("/etc", appname) +} diff --git a/os_linux.go b/os_linux.go new file mode 100644 index 0000000..41c50b9 --- /dev/null +++ b/os_linux.go @@ -0,0 +1,22 @@ +//go:build linux +// +build linux + +package util + +import ( + "os/user" + "path/filepath" +) + +// IsSuperUser returns true, if the current user is a super user +// A.K.A root, Administrator etc +func IsSuperUser() bool { + cuser, err := user.Current() + return err == nil && "0" == cuser.Uid +} + +// GetGlobalConfigurationDirectory returns OS specific location for putting +// global configuration files +func GetGlobalConfigurationDirectory(appname string) string { + return filepath.Join("/etc", appname) +} diff --git a/os_windows.go b/os_windows.go new file mode 100644 index 0000000..102eee1 --- /dev/null +++ b/os_windows.go @@ -0,0 +1,25 @@ +//go:build windows +// +build windows + +package util + +import ( + "os" + "path/filepath" +) + +// IsSuperUser returns true, if the current user is a super user +// A.K.A root, Administrator etc +func IsSuperUser() bool { + _, err := os.Open("\\\\.\\PHYSICALDRIVE0") + if err != nil { + return false + } + return true +} + +// GetGlobalConfigurationDirectory returns OS specific location for putting +// global configuration files +func GetGlobalConfigurationDirectory(appname string) string { + return filepath.Join(os.Getenv("APPDATA"), appname) +}