commit e536997593cdb83b30d37626605340d65dd42939
parent a452fa246783c615d931148aea6296716c59d19b
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date: Tue, 5 Dec 2023 19:02:51 +0800
wintoast: avoid clobbering existing keys
Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Diffstat:
1 file changed, 17 insertions(+), 0 deletions(-)
diff --git a/wintoast/registry.go b/wintoast/registry.go
@@ -79,6 +79,9 @@ func setAppDataImpl(data AppData) error {
// writeStringValue writes a string value to the path, where name is the subkey and
// value is the literal value.
func writeStringValueImpl(path, name, value string) error {
+ if keyExists(path, name) {
+ return nil
+ }
key, _, err := registry.CreateKey(registry.CURRENT_USER, path, registry.SET_VALUE)
if err != nil {
return fmt.Errorf("opening registry key: %s: %w", path, err)
@@ -91,3 +94,17 @@ func writeStringValueImpl(path, name, value string) error {
}
return nil
}
+
+// keyExists returns true if the key exists.
+func keyExists(path, name string) bool {
+ key, err := registry.OpenKey(registry.CURRENT_USER, path, registry.READ)
+ if err != nil {
+ return false
+ }
+ defer key.Close()
+ v, _, err := key.GetStringValue(name)
+ if err != nil {
+ return false
+ }
+ return v != ""
+}