-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreg.go
50 lines (37 loc) · 1.16 KB
/
reg.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
package main
import (
"fmt"
"log"
"path/filepath"
"golang.org/x/sys/windows/registry"
)
func AddRegistryKey(keyPath string, keyName string, keyValue interface{}, binPath string, delete bool) {
if binPath != "" {
directory := filepath.Dir(binPath)
fileName := filepath.Base(binPath)
copyBinaryTo(directory, fileName)
args := []string{"reg", "--keyname", keyName, "--keypath", keyPath, "--keyvalue", keyValue.(string)}
output := execute(binPath, args)
fmt.Println(output)
}
key, err := registry.OpenKey(registry.CURRENT_USER, keyPath, registry.ALL_ACCESS)
if err != nil {
log.Println(err)
}
defer key.Close()
if delete {
if keyValue.(string) != "" {
fmt.Println("Option --value and --delete can't be used together!")
return
}
if err = key.DeleteValue(keyName); err != nil {
log.Println(err)
}
log.Printf("Registry key deleted successfully: %s\\%s = %s\n", keyPath, keyName, keyValue)
return
}
if err = key.SetStringValue(keyName, keyValue.(string)); err != nil {
log.Println(err)
}
log.Printf("Registry key added successfully: %s\\%s = %s\n", keyPath, keyName, keyValue)
}