Skip to content

Commit 57a5103

Browse files
committed
👔 up: sys - add new func: IsAdmin, enhance func: UserDir, UserConfigDir
1 parent cd830b9 commit 57a5103

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

sysutil/user.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/gookit/goutil/internal/comfunc"
88
)
99

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

19-
// LoginUser must get current user
19+
// LoginUser must get current user, will panic if error
2020
func LoginUser() *user.User {
2121
return CurrentUser()
2222
}
2323

24-
// CurrentUser must get current user
24+
// CurrentUser must get current user, will panic if error
2525
func CurrentUser() *user.User {
26-
// check $HOME/.terminfo
2726
u, err := user.Current()
2827
if err != nil {
2928
panic(err)
3029
}
3130
return u
3231
}
3332

34-
// UHomeDir get user home dir path.
33+
// UHomeDir get user home dir path, ignore error. (by user.Current)
3534
func UHomeDir() string {
36-
// check $HOME/.terminfo
3735
u, err := user.Current()
3836
if err != nil {
3937
return ""
@@ -42,37 +40,32 @@ func UHomeDir() string {
4240
}
4341

4442
// homeDir cache
45-
var homeDir string
43+
var _homeDir string
4644

47-
// UserHomeDir is alias of os.UserHomeDir, but ignore error
45+
// UserHomeDir is alias of os.UserHomeDir, but ignore error.(by os.UserHomeDir)
4846
func UserHomeDir() string {
49-
if homeDir == "" {
50-
homeDir, _ = os.UserHomeDir()
47+
if _homeDir == "" {
48+
_homeDir, _ = os.UserHomeDir()
5149
}
52-
return homeDir
50+
return _homeDir
5351
}
5452

5553
// HomeDir get user home dir path.
56-
func HomeDir() string {
57-
return UserHomeDir()
58-
}
54+
func HomeDir() string { return UserHomeDir() }
5955

60-
// UserDir will prepend user home dir to subPath
61-
func UserDir(subPath string) string {
62-
dir := UserHomeDir()
63-
return dir + "/" + subPath
56+
// UserDir will prepend user home dir to subPaths
57+
func UserDir(subPaths ...string) string {
58+
return comfunc.JoinPaths2(UserHomeDir(), subPaths)
6459
}
6560

66-
// UserCacheDir will prepend user `$HOME/.cache` to subPath
67-
func UserCacheDir(subPath string) string {
68-
dir := UserHomeDir()
69-
return dir + "/.cache/" + subPath
61+
// UserCacheDir will prepend user `$HOME/.cache` to subPaths
62+
func UserCacheDir(subPaths ...string) string {
63+
return comfunc.JoinPaths3(UserHomeDir(), ".cache", subPaths)
7064
}
7165

7266
// UserConfigDir will prepend user `$HOME/.config` to subPath
73-
func UserConfigDir(subPath string) string {
74-
dir := UserHomeDir()
75-
return dir + "/.config/" + subPath
67+
func UserConfigDir(subPaths ...string) string {
68+
return comfunc.JoinPaths3(UserHomeDir(), ".cache", subPaths)
7669
}
7770

7871
// ExpandPath will parse `~` as user home dir path.

sysutil/user_nonwin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
package sysutil
44

55
import (
6+
"os"
67
"syscall"
78

89
"github.com/gookit/goutil/strutil"
910
)
1011

12+
// IsAdmin Determine whether the current user is an administrator(root)
13+
func IsAdmin() bool {
14+
return os.Getuid() == 0
15+
}
16+
1117
// ChangeUserByName change work user by new username.
1218
func ChangeUserByName(newUname string) error {
1319
u := MustFindUser(newUname)

sysutil/user_windows.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ func ChangeUserUidGid(newUid int, newGid int) (err error) {
1919
func ChangeUserUIDGid(newUid int, newGid int) (err error) {
2020
return nil
2121
}
22+
23+
// IsAdmin Determine whether the current user is an administrator
24+
func IsAdmin() bool {
25+
// 执行 net session 判断
26+
_, err := ExecCmd("net", []string{"session"})
27+
return err == nil
28+
}

0 commit comments

Comments
 (0)