-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathenv_conflicts_windows.go
More file actions
62 lines (56 loc) · 1.24 KB
/
Copy pathenv_conflicts_windows.go
File metadata and controls
62 lines (56 loc) · 1.24 KB
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
//go:build windows
package main
import (
"sort"
"syscall"
"golang.org/x/sys/windows/registry"
)
const windowsUserEnvKey = `Environment`
func detectUserEnvConflicts() []envConflict {
key, err := registry.OpenKey(registry.CURRENT_USER, windowsUserEnvKey, registry.QUERY_VALUE)
if err != nil {
return nil
}
defer key.Close()
names, err := key.ReadValueNames(0)
if err != nil {
return nil
}
pairs := map[string]string{}
for _, name := range names {
value, _, err := key.GetStringValue(name)
if err != nil {
value = ""
}
pairs[name] = value
}
conflicts := detectedEnvConflictsFromPairs(pairs, "user")
sortEnvConflicts(conflicts)
return conflicts
}
func removeUserEnvValue(name string) (bool, error) {
key, err := registry.OpenKey(registry.CURRENT_USER, windowsUserEnvKey, registry.SET_VALUE|registry.QUERY_VALUE)
if err != nil {
return false, err
}
defer key.Close()
names, err := key.ReadValueNames(0)
if err != nil {
return false, err
}
sort.Strings(names)
found := false
for _, existing := range names {
if existing == name {
found = true
break
}
}
if !found {
return false, nil
}
if err := key.DeleteValue(name); err != nil && err != syscall.ERROR_FILE_NOT_FOUND {
return false, err
}
return true, nil
}