github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/installation/context_windows.go (about) 1 package installation 2 3 import ( 4 "fmt" 5 "os/user" 6 "strconv" 7 8 "github.com/ActiveState/cli/internal/errs" 9 "github.com/ActiveState/cli/internal/multilog" 10 "github.com/ActiveState/cli/internal/osutils" 11 ) 12 13 const ( 14 // adminInstallRegistry is the registry key name used to determine if the State Tool was installed as administrator 15 adminInstallRegistry = "Installed As Admin" 16 17 installRegistryKeyPath = `SOFTWARE\ActiveState\install` 18 ) 19 20 func GetContext() (*Context, error) { 21 key, err := osutils.OpenUserKey(installRegistryKeyPath) 22 if osutils.IsNotExistError(err) { 23 multilog.Error("Installation registry key does not exist, assuming user level install of State Tool") 24 return &Context{InstalledAsAdmin: false}, nil 25 } 26 if err != nil { 27 return nil, errs.Wrap(err, "Could not get key value") 28 } 29 defer key.Close() 30 31 v, _, err := key.GetStringValue(adminInstallRegistry) 32 if err != nil { 33 return nil, errs.Wrap(err, "Could not get string value") 34 } 35 36 installedAsAdmin, err := strconv.ParseBool(v) 37 if err != nil { 38 return nil, errs.Wrap(err, "Could not parse bool from string value: %s", v) 39 } 40 41 return &Context{InstalledAsAdmin: installedAsAdmin}, nil 42 } 43 44 func SaveContext(context *Context) error { 45 user, err := user.Current() 46 if err != nil { 47 return errs.Wrap(err, "Could not get current user") 48 } 49 50 key, _, err := osutils.CreateUserKey(fmt.Sprintf(`%s\%s`, user.Uid, installRegistryKeyPath)) 51 if err != nil { 52 return errs.Wrap(err, "Could not create registry key") 53 } 54 defer key.Close() 55 56 err = key.SetStringValue(adminInstallRegistry, strconv.FormatBool(context.InstalledAsAdmin)) 57 if err != nil { 58 return errs.Wrap(err, "Could not set registry key value") 59 } 60 61 return nil 62 }