6
6
7
7
package user
8
8
9
- import "errors"
9
+ import (
10
+ "errors"
11
+ "fmt"
12
+ "os"
13
+ "runtime"
14
+ "strconv"
15
+ )
10
16
11
17
func init () {
12
18
userImplemented = false
13
19
groupImplemented = false
14
20
}
15
21
16
22
func current () (* User , error ) {
17
- return nil , errors .New ("user: Current requires cgo" )
23
+ u := & User {
24
+ Uid : currentUID (),
25
+ Gid : currentGID (),
26
+ Username : os .Getenv ("USER" ),
27
+ Name : "" , // ignored
28
+ HomeDir : os .Getenv ("HOME" ),
29
+ }
30
+ if runtime .GOOS == "nacl" {
31
+ if u .Uid == "" {
32
+ u .Uid = "1"
33
+ }
34
+ if u .Username == "" {
35
+ u .Username = "nacl"
36
+ }
37
+ if u .HomeDir == "" {
38
+ u .HomeDir = "/home/nacl"
39
+ }
40
+ }
41
+ // cgo isn't available, but if we found the minimum information
42
+ // without it, use it:
43
+ if u .Uid != "" && u .Username != "" && u .HomeDir != "" {
44
+ return u , nil
45
+ }
46
+ return nil , fmt .Errorf ("user: Current not implemented on %s/%s" , runtime .GOOS , runtime .GOARCH )
18
47
}
19
48
20
49
func lookupUser (username string ) (* User , error ) {
@@ -36,3 +65,20 @@ func lookupGroupId(string) (*Group, error) {
36
65
func listGroups (* User ) ([]string , error ) {
37
66
return nil , errors .New ("user: GroupIds requires cgo" )
38
67
}
68
+
69
+ func currentUID () string {
70
+ if id := os .Getuid (); id >= 0 {
71
+ return strconv .Itoa (id )
72
+ }
73
+ // Note: Windows returns -1, but this file isn't used on
74
+ // Windows anyway, so this empty return path shouldn't be
75
+ // used.
76
+ return ""
77
+ }
78
+
79
+ func currentGID () string {
80
+ if id := os .Getgid (); id >= 0 {
81
+ return strconv .Itoa (id )
82
+ }
83
+ return ""
84
+ }
0 commit comments