Skip to content

Commit

Permalink
👔 up: sys - add new func: IsAdmin, enhance func: UserDir, UserConfigDir
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 3, 2024
1 parent cd830b9 commit 57a5103
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
43 changes: 18 additions & 25 deletions sysutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gookit/goutil/internal/comfunc"
)

// MustFindUser must find an system user by name
// MustFindUser must find a system user by name
func MustFindUser(uname string) *user.User {
u, err := user.Lookup(uname)
if err != nil {
Expand All @@ -16,24 +16,22 @@ func MustFindUser(uname string) *user.User {
return u
}

// LoginUser must get current user
// LoginUser must get current user, will panic if error
func LoginUser() *user.User {
return CurrentUser()
}

// CurrentUser must get current user
// CurrentUser must get current user, will panic if error
func CurrentUser() *user.User {
// check $HOME/.terminfo
u, err := user.Current()
if err != nil {
panic(err)
}
return u
}

// UHomeDir get user home dir path.
// UHomeDir get user home dir path, ignore error. (by user.Current)
func UHomeDir() string {
// check $HOME/.terminfo
u, err := user.Current()
if err != nil {
return ""
Expand All @@ -42,37 +40,32 @@ func UHomeDir() string {
}

// homeDir cache
var homeDir string
var _homeDir string

// UserHomeDir is alias of os.UserHomeDir, but ignore error
// UserHomeDir is alias of os.UserHomeDir, but ignore error.(by os.UserHomeDir)
func UserHomeDir() string {
if homeDir == "" {
homeDir, _ = os.UserHomeDir()
if _homeDir == "" {
_homeDir, _ = os.UserHomeDir()
}
return homeDir
return _homeDir
}

// HomeDir get user home dir path.
func HomeDir() string {
return UserHomeDir()
}
func HomeDir() string { return UserHomeDir() }

// UserDir will prepend user home dir to subPath
func UserDir(subPath string) string {
dir := UserHomeDir()
return dir + "/" + subPath
// UserDir will prepend user home dir to subPaths
func UserDir(subPaths ...string) string {
return comfunc.JoinPaths2(UserHomeDir(), subPaths)
}

// UserCacheDir will prepend user `$HOME/.cache` to subPath
func UserCacheDir(subPath string) string {
dir := UserHomeDir()
return dir + "/.cache/" + subPath
// UserCacheDir will prepend user `$HOME/.cache` to subPaths
func UserCacheDir(subPaths ...string) string {
return comfunc.JoinPaths3(UserHomeDir(), ".cache", subPaths)
}

// UserConfigDir will prepend user `$HOME/.config` to subPath
func UserConfigDir(subPath string) string {
dir := UserHomeDir()
return dir + "/.config/" + subPath
func UserConfigDir(subPaths ...string) string {
return comfunc.JoinPaths3(UserHomeDir(), ".cache", subPaths)
}

// ExpandPath will parse `~` as user home dir path.
Expand Down
6 changes: 6 additions & 0 deletions sysutil/user_nonwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
package sysutil

import (
"os"
"syscall"

"github.com/gookit/goutil/strutil"
)

// IsAdmin Determine whether the current user is an administrator(root)
func IsAdmin() bool {
return os.Getuid() == 0
}

// ChangeUserByName change work user by new username.
func ChangeUserByName(newUname string) error {
u := MustFindUser(newUname)
Expand Down
7 changes: 7 additions & 0 deletions sysutil/user_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ func ChangeUserUidGid(newUid int, newGid int) (err error) {
func ChangeUserUIDGid(newUid int, newGid int) (err error) {
return nil
}

// IsAdmin Determine whether the current user is an administrator
func IsAdmin() bool {
// 执行 net session 判断
_, err := ExecCmd("net", []string{"session"})
return err == nil
}

0 comments on commit 57a5103

Please sign in to comment.