feat(windows): update registry on upgrade

This commit is contained in:
Jan De Dobbeleer 2024-07-12 17:22:58 +02:00 committed by Jan De Dobbeleer
parent c865759e2f
commit 58fd8e5f82
4 changed files with 51 additions and 1 deletions

View file

@ -137,7 +137,7 @@ func (term *Terminal) CachePath() string {
func (term *Terminal) WindowsRegistryKeyValue(path string) (*WindowsRegistryValue, error) { func (term *Terminal) WindowsRegistryKeyValue(path string) (*WindowsRegistryValue, error) {
term.Trace(time.Now(), path) term.Trace(time.Now(), path)
// Format:sudo -u postgres psql // Format:
// "HKLM\Software\Microsoft\Windows NT\CurrentVersion\EditionID" // "HKLM\Software\Microsoft\Windows NT\CurrentVersion\EditionID"
// 1 | 2 | 3 // 1 | 2 | 3
// //
@ -187,6 +187,7 @@ func (term *Terminal) WindowsRegistryKeyValue(path string) (*WindowsRegistryValu
term.Error(err) term.Error(err)
return nil, err return nil, err
} }
_, valType, err := k.GetValue(regKey, nil) _, valType, err := k.GetValue(regKey, nil)
if err != nil { if err != nil {
term.Error(err) term.Error(err)
@ -214,6 +215,7 @@ func (term *Terminal) WindowsRegistryKeyValue(path string) (*WindowsRegistryValu
errorLogMsg := fmt.Sprintf("Error, no formatter for type: %d", valType) errorLogMsg := fmt.Sprintf("Error, no formatter for type: %d", valType)
return nil, errors.New(errorLogMsg) return nil, errors.New(errorLogMsg)
} }
term.Debug(fmt.Sprintf("%s(%s): %s", regKey, regValue.ValueType, regValue.String)) term.Debug(fmt.Sprintf("%s(%s): %s", regKey, regValue.ValueType, regValue.String))
return regValue, nil return regValue, nil
} }

View file

@ -68,5 +68,7 @@ func install(tag string) error {
_ = hideFile(oldPath) _ = hideFile(oldPath)
} }
updateRegistry(tag)
return nil return nil
} }

View file

@ -5,3 +5,5 @@ package upgrade
func hideFile(_ string) error { func hideFile(_ string) error {
return nil return nil
} }
func updateRegistry(_ string) {}

View file

@ -1,8 +1,12 @@
package upgrade package upgrade
import ( import (
"strconv"
"strings"
"syscall" "syscall"
"unsafe" "unsafe"
"golang.org/x/sys/windows/registry"
) )
func hideFile(path string) error { func hideFile(path string) error {
@ -22,3 +26,43 @@ func hideFile(path string) error {
return nil return nil
} }
func updateRegistry(version string) {
key, err := getRegistryKey()
if err != nil {
return
}
version = strings.TrimLeft(version, "v")
_ = key.SetStringValue("DisplayVersion", version)
_ = key.SetStringValue("DisplayName", "Oh My Posh")
splitted := strings.Split(version, ".")
if len(splitted) < 3 {
return
}
if u64, err := strconv.ParseUint(splitted[0], 10, 32); err == nil {
major := uint32(u64)
_ = key.SetDWordValue("MajorVersion", major)
_ = key.SetDWordValue("VersionMajor", major)
}
if u64, err := strconv.ParseUint(splitted[1], 10, 32); err == nil {
minor := uint32(u64)
_ = key.SetDWordValue("MinorVersion", minor)
_ = key.SetDWordValue("VersionMinor", minor)
}
}
func getRegistryKey() (registry.Key, error) {
path := `Software\Microsoft\Windows\CurrentVersion\Uninstall\Oh My Posh_is1`
key, err := registry.OpenKey(registry.CURRENT_USER, path, registry.WRITE)
if err == nil {
return key, nil
}
return registry.OpenKey(registry.LOCAL_MACHINE, path, registry.WRITE)
}