generated from brevdev/seed
/
basic.go
60 lines (54 loc) · 1.64 KB
/
basic.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
53
54
55
56
57
58
59
60
package store
import (
"fmt"
"os"
otherpath "path"
"runtime"
"strings"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
)
type BasicStore struct {
envGetter func(string) string
}
func NewBasicStore() *BasicStore {
return &BasicStore{
envGetter: os.Getenv,
}
}
func (b *BasicStore) WithEnvGetter(f func(string) string) *BasicStore {
b.envGetter = f
return b
}
// look in path on wsl to find out which user is the user on windows that is running wsl
// PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Users/15854/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/15854/AppData/Local/Programs/Microsoft VS Code/bin:/snap/bin
func (b BasicStore) GetWSLHostHomeDir() (string, error) {
if runtime.GOOS == "windows" {
return "", breverrors.New("not supported on windows")
}
if runtime.GOOS == "linux" {
path := ""
if b.envGetter == nil {
path = os.Getenv("PATH")
} else {
path = b.envGetter("PATH")
}
if path == "" {
return "", breverrors.New("PATH is empty")
}
username := ""
pathSplit := strings.Split(path, ":")
for _, p := range pathSplit {
if strings.Contains(p, "/mnt/c/Users/") {
username = strings.Split(p, "/")[4]
break
}
}
if username == "" {
return "", breverrors.New("could not find username")
}
windowsDir := "/mnt/c/Users/"
windowsUserDir := otherpath.Join(windowsDir, username)
return windowsUserDir, nil
}
return "", fmt.Errorf("not supported on %s", runtime.GOOS)
}