-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathusershell_other.go
More file actions
35 lines (31 loc) · 858 Bytes
/
usershell_other.go
File metadata and controls
35 lines (31 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//go:build !windows && !darwin
// +build !windows,!darwin
package usershell
import (
"os"
"strings"
"golang.org/x/xerrors"
)
// Get returns the /etc/passwd entry for the username provided.
// Deprecated: use SystemEnvInfo.UserShell instead.
func Get(username string) (string, error) {
contents, err := os.ReadFile("/etc/passwd")
if err != nil {
return "", xerrors.Errorf("read /etc/passwd: %w", err)
}
lines := strings.Split(string(contents), "\n")
for _, line := range lines {
if !strings.HasPrefix(line, username+":") {
continue
}
parts := strings.Split(line, ":")
if len(parts) < 7 {
return "", xerrors.Errorf("malformed user entry: %q", line)
}
return parts[6], nil
}
if s := os.Getenv("SHELL"); s != "" {
return s, nil
}
return "", xerrors.Errorf("shell for user %q not found in /etc/passwd or $SHELL", username)
}