mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-10 04:54:03 -08:00
fix(winreg): fallback value on error or empty
This commit is contained in:
parent
30c82c2f56
commit
4cbb3e6cf3
|
@ -34,7 +34,8 @@ Supported registry key types:
|
|||
## Properties
|
||||
|
||||
- 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
|
||||
with [sprig][sprig] utilizing the properties below.
|
||||
|
||||
|
|
|
@ -12,6 +12,8 @@ const (
|
|||
RegistryPath Property = "path"
|
||||
// key within full reg path formed from two above
|
||||
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) {
|
||||
|
@ -26,10 +28,15 @@ func (wr *winreg) enabled() bool {
|
|||
|
||||
registryPath := wr.props.getString(RegistryPath, "")
|
||||
registryKey := wr.props.getString(RegistryKey, "")
|
||||
fallback := wr.props.getString(Fallback, "")
|
||||
|
||||
var err error
|
||||
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 {
|
||||
|
|
|
@ -12,7 +12,9 @@ func TestRegQueryEnabled(t *testing.T) {
|
|||
CaseDescription string
|
||||
Path string
|
||||
Key string
|
||||
Fallback string
|
||||
ExpectedSuccess bool
|
||||
ExpectedValue string
|
||||
Output string
|
||||
Err error
|
||||
}{
|
||||
|
@ -20,15 +22,43 @@ func TestRegQueryEnabled(t *testing.T) {
|
|||
CaseDescription: "Error",
|
||||
Path: "HKLLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
|
||||
Key: "ProductName",
|
||||
ExpectedSuccess: false,
|
||||
Err: errors.New("No match"),
|
||||
ExpectedSuccess: false,
|
||||
},
|
||||
{
|
||||
CaseDescription: "Value",
|
||||
Path: "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
|
||||
Key: "InstallTime",
|
||||
Output: "xbox",
|
||||
ExpectedSuccess: true,
|
||||
ExpectedValue: "xbox",
|
||||
},
|
||||
{
|
||||
CaseDescription: "Fallback value",
|
||||
Path: "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
|
||||
Key: "InstallTime",
|
||||
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{}{
|
||||
RegistryPath: tc.Path,
|
||||
RegistryKey: tc.Key,
|
||||
Fallback: tc.Fallback,
|
||||
},
|
||||
}
|
||||
r := &winreg{
|
||||
|
@ -47,6 +78,7 @@ func TestRegQueryEnabled(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue