oh-my-posh/environment_windows.go

54 lines
1.3 KiB
Go
Raw Normal View History

2020-11-04 23:56:12 -08:00
// +build windows
2019-03-13 04:14:30 -07:00
package main
import (
"os"
2019-03-13 04:14:30 -07:00
"golang.org/x/sys/windows"
)
func (env *environment) isRunningAsRoot() bool {
var sid *windows.SID
2020-10-03 10:43:38 -07:00
2019-03-13 04:14:30 -07:00
// Although this looks scary, it is directly copied from the
// official windows documentation. The Go API for this is a
// direct wrap around the official C++ API.
// See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-checktokenmembership
err := windows.AllocateAndInitializeSid(
&windows.SECURITY_NT_AUTHORITY,
2,
windows.SECURITY_BUILTIN_DOMAIN_RID,
windows.DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&sid)
if err != nil {
return false
}
2020-10-03 10:43:38 -07:00
defer windows.FreeSid(sid)
2019-03-13 04:14:30 -07:00
// This appears to cast a null pointer so I'm not sure why this
// works, but this guy says it does and it Works for Me™:
// https://github.com/golang/go/issues/28804#issuecomment-438838144
token := windows.Token(0)
2020-10-03 10:43:38 -07:00
2019-03-13 04:14:30 -07:00
member, err := token.IsMember(sid)
if err != nil {
return false
}
2020-10-03 10:43:38 -07:00
return member
2019-03-13 04:14:30 -07:00
}
func (env *environment) homeDir() string {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
2020-11-04 23:56:12 -08:00
func (env *environment) getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
return getWindowTitle(imageName, windowTitleRegex)
}