-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
52 lines (45 loc) · 874 Bytes
/
common.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"errors"
"net"
"os"
"os/user"
"path/filepath"
)
//
// Default socket location:
//
// - $XDG_RUNTIME_DIR/eternal
// - $TMPDIR/$USER-eternal
// - /tmp/$USER-eternal
//
func socketName() string {
filename := "eternal"
dir := os.Getenv("XDG_RUNTIME_DIR")
if dir != "" {
return filepath.Join(dir, filename)
}
username := os.Getenv("USER")
user, err := user.Current()
if err == nil {
username = user.Username
}
if username != "" {
filename = username + "-" + filename
}
dir = os.Getenv("TMPDIR")
if dir == "" {
dir = "/tmp"
}
return filepath.Join(dir, filename)
}
func connect() (net.Conn, error) {
return net.Dial("unix", socketName())
}
func getSession() (string, error) {
session := os.Getenv("ETERNAL_SESSION")
if session == "" {
return "", errors.New("no ETERNAL_SESSION in environment")
}
return session, nil
}