Add os specific stuff

This commit is contained in:
Stefan Goppelt 2022-03-17 11:11:17 +01:00
parent ff4b332abc
commit e9cbf8bf28
3 changed files with 69 additions and 0 deletions

22
os_darwin.go Normal file
View File

@ -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)
}

22
os_linux.go Normal file
View File

@ -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)
}

25
os_windows.go Normal file
View File

@ -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)
}