-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
117 lines (87 loc) · 3.05 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"path/filepath"
"strings"
)
const WkScdaemonSocketName = "S.scdaemon"
func GetWindowsScdaemonSocketFn() (string, error) {
winSocketDir, err := gpgconfGetDirectory("gpgconf.exe", "socketdir")
if err != nil {
return "", fmt.Errorf("Failed to get win socketdir: %w", err)
}
socketDir, err := GetWslPath(strings.TrimSpace(strings.Replace(winSocketDir, "%3a", ":", 1)))
if err != nil {
return "", fmt.Errorf("Failed to convert win socketdir to WSL path: %w", err)
}
return filepath.Join(socketDir, WkScdaemonSocketName), nil
}
func GetWindowsScdaemonBinaryFn() (string, error) {
binary, err := gpgconfGetBinary("gpgconf.exe", "scdaemon")
if err != nil {
return "", fmt.Errorf("Failed to get win scdaemon binary: %w", err)
}
return GetWslPath(strings.TrimSpace(strings.Replace(binary, "%3a", ":", 1)))
}
func GetLinuxScdaemonSocketFn() (string, error) {
socketDir, err := gpgconfGetDirectory("gpgconf", "socketdir")
if err != nil {
return "", fmt.Errorf("Failed to get socketdir: %w", err)
}
return filepath.Join(socketDir, WkScdaemonSocketName), nil
}
func GetLinuxGnupgHomePath() (string, error) {
homeDir, err := gpgconfGetDirectory("gpgconf", "homedir")
if err != nil {
return "", fmt.Errorf("Failed to get homedir: %w", err)
}
return homeDir, nil
}
// GetWslPath converts a windows path into a WSL path.
func GetWslPath(path string) (string, error) {
res, err := exec.Command("wslpath", "-a", path).Output()
if err != nil {
return "", fmt.Errorf("Error while running wslpath cmd: %w", err)
}
return strings.TrimSpace(string(res)), err
}
// gpgconfGetDirectory gets the specified key from the gpgconf --list-dirs command output.
func gpgconfGetDirectory(gpgconf, key string) (string, error) {
return gpgconfGetKey(gpgconf, key, "--list-dirs")
}
// gpgconfGetBinary gets the binary with the specified key from gpgconf.
func gpgconfGetBinary(gpgconf, key string) (string, error) {
return gpgconfGetKey(gpgconf, key)
}
// gpgconfGetKey runs gpgconf with the provided args and returns the value of the key provided.
func gpgconfGetKey(gpgconf string, key string, args ...string) (string, error) {
cmd := exec.Command(string(gpgconf), args...)
res, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Error running gpgconf: %w", err)
}
scanner := bufio.NewScanner(bytes.NewReader(res))
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
tokens := strings.SplitN(scanner.Text(), ":", 3)
if tokens[0] == key {
return tokens[len(tokens)-1], nil
}
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("Error scanning gpgconf output: %w", err)
}
return "", fmt.Errorf("Key was not found")
}
// GetResolvAddr gets the Windows host IP address (to support WSL2) in a very hacky way...
func GetResolvAddr() (string, error) {
cmd := exec.Command("awk", "/nameserver / {print $2; exit}", "/etc/resolv.conf")
res, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Unable to get windows host addr: %w", err)
}
return strings.TrimSpace(string(res)), nil
}