fix(winreg): fallback value on error or empty

This commit is contained in:
Jan De Dobbeleer 2021-11-25 09:15:10 +01:00 committed by Jan De Dobbeleer
parent 30c82c2f56
commit 4cbb3e6cf3
3 changed files with 44 additions and 4 deletions

View file

@ -34,7 +34,8 @@ Supported registry key types:
## Properties ## Properties
- path: `string` - registry path to the desired key using backslashes and with a valid root HKEY name. - path: `string` - registry path to the desired key using backslashes and with a valid root HKEY name.
- key: `string` - the key to read from the `path location. If `""`, will read the default value. - key: `string` - the key to read from the `path` location.
- fallback: `string` - the value to fall back to if no entry is found
- template: `string` - a go [text/template][go-text-template] template extended - template: `string` - a go [text/template][go-text-template] template extended
with [sprig][sprig] utilizing the properties below. with [sprig][sprig] utilizing the properties below.

View file

@ -12,6 +12,8 @@ const (
RegistryPath Property = "path" RegistryPath Property = "path"
// key within full reg path formed from two above // key within full reg path formed from two above
RegistryKey Property = "key" RegistryKey Property = "key"
// Fallback is the text to display if the key is not found
Fallback Property = "fallback"
) )
func (wr *winreg) init(props *properties, env environmentInfo) { func (wr *winreg) init(props *properties, env environmentInfo) {
@ -26,10 +28,15 @@ func (wr *winreg) enabled() bool {
registryPath := wr.props.getString(RegistryPath, "") registryPath := wr.props.getString(RegistryPath, "")
registryKey := wr.props.getString(RegistryKey, "") registryKey := wr.props.getString(RegistryKey, "")
fallback := wr.props.getString(Fallback, "")
var err error var err error
wr.Value, err = wr.env.getWindowsRegistryKeyValue(registryPath, registryKey) wr.Value, err = wr.env.getWindowsRegistryKeyValue(registryPath, registryKey)
return err == nil if len(fallback) > 0 && (err != nil || len(wr.Value) == 0) {
wr.Value = fallback
return true
}
return err == nil && len(wr.Value) > 0
} }
func (wr *winreg) string() string { func (wr *winreg) string() string {

View file

@ -12,7 +12,9 @@ func TestRegQueryEnabled(t *testing.T) {
CaseDescription string CaseDescription string
Path string Path string
Key string Key string
Fallback string
ExpectedSuccess bool ExpectedSuccess bool
ExpectedValue string
Output string Output string
Err error Err error
}{ }{
@ -20,15 +22,43 @@ func TestRegQueryEnabled(t *testing.T) {
CaseDescription: "Error", CaseDescription: "Error",
Path: "HKLLM\\Software\\Microsoft\\Windows NT\\CurrentVersion", Path: "HKLLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
Key: "ProductName", Key: "ProductName",
ExpectedSuccess: false,
Err: errors.New("No match"), Err: errors.New("No match"),
ExpectedSuccess: false,
}, },
{ {
CaseDescription: "Value", CaseDescription: "Value",
Path: "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion", Path: "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
Key: "InstallTime", Key: "InstallTime",
Output: "xbox",
ExpectedSuccess: true, ExpectedSuccess: true,
ExpectedValue: "xbox",
},
{
CaseDescription: "Fallback value",
Path: "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
Key: "InstallTime",
Output: "no formatter", Output: "no formatter",
Fallback: "cortana",
Err: errors.New("No match"),
ExpectedSuccess: true,
ExpectedValue: "cortana",
},
{
CaseDescription: "Fallback value on empty",
Path: "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
Key: "InstallTime",
Output: "",
Fallback: "anaconda",
ExpectedSuccess: true,
ExpectedValue: "anaconda",
},
{
CaseDescription: "Empty no fallback disabled",
Path: "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
Key: "InstallTime",
Output: "",
ExpectedSuccess: false,
ExpectedValue: "",
}, },
} }
@ -40,6 +70,7 @@ func TestRegQueryEnabled(t *testing.T) {
values: map[Property]interface{}{ values: map[Property]interface{}{
RegistryPath: tc.Path, RegistryPath: tc.Path,
RegistryKey: tc.Key, RegistryKey: tc.Key,
Fallback: tc.Fallback,
}, },
} }
r := &winreg{ r := &winreg{
@ -47,6 +78,7 @@ func TestRegQueryEnabled(t *testing.T) {
props: props, props: props,
} }
assert.Equal(t, r.enabled(), tc.ExpectedSuccess, tc.CaseDescription) assert.Equal(t, tc.ExpectedSuccess, r.enabled(), tc.CaseDescription)
assert.Equal(t, tc.ExpectedValue, r.string(), tc.CaseDescription)
} }
} }