Skip to content

Commit

Permalink
Implement --reg-hive and --section options for list-settings --output…
Browse files Browse the repository at this point in the history
…=reg.

Signed-off-by: Eric Promislow <epromislow@suse.com>
  • Loading branch information
ericpromislow committed Jun 29, 2023
1 parent 7b6e7ce commit 8b652c4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 98 deletions.
74 changes: 32 additions & 42 deletions bats/tests/preferences/list-settings-output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,61 @@ RD_USE_IMAGE_ALLOW_LIST=true
}

@test 'report parameters for json' {
run rdctl list-settings '--output=json,fish'
run rdctl list-settings '--output=json' '--reg-hive=fish'
assert_failure
assert_output --partial 'the json output format takes no sub-formats, got "json,fish"'
assert_output --partial $'registry hive and profile can\'t be specified with json'
}

@test 'report unrecognized output-options' {
run rdctl list-settings '--output=bickley,ruff'
assert_failure
assert_output --partial $'expecting an output format of \'json\' or \'reg\', got "bickley,ruff"'
assert_output --partial $'invalid output format of "bickley,ruff"'
}

@test 'report unrecognized reg sub-options' {
run rdctl list-settings '--output=reg,hklm,ruff'
run rdctl list-settings --output=reg --reg-hive=hklm --section=ruff
assert_failure
assert_output --partial 'expecting a reg output-format parameter, got "ruff" in "reg,hklm,ruff"'
assert_output --partial "invalid registry section of 'ruff' specified"
}

@test 'report duplicate reg hives' {
for x in hklm hkcu; do
for y in hklm hkcu; do
option="reg,${x},locked,${y}"
run rdctl list-settings "--output=${option}"
assert_failure
assert_output --partial $"already specified registry hive \"${x}\" in \"${option}\", can't respecify"
done
done
}
@test 'generates registry output for hklm/defaults' {
run rdctl list-settings --output reg
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application]'

@test 'report duplicate registry sections' {
for x in defaults locked; do
for y in defaults locked; do
option="reg,${x},hkcu,${y}"
run rdctl list-settings "--output=${option}"
assert_failure
assert_output --partial $"already specified registry section \"${x}\" in \"${option}\", can't respecify"
done
done
}
run rdctl list-settings --output reg --reg-hive=hklm
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application]'

@test 'generates registry output for hklm/defaults' {
for option in reg reg,hklm reg,hklm,defaults reg,defaults; do
run rdctl list-settings --output "$option"
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application]'
done
run rdctl list-settings --output reg --reg-hive=hklm --section=defaults
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application]'

run rdctl list-settings --output reg --section=defaults
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\defaults\application]'
}

@test 'generates registry output for hklm/locked' {
for option in reg,hklm,locked reg,locked; do
run rdctl list-settings --output "$option"
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\locked\application]'
done
run rdctl list-settings --output reg --reg-hive=hklm --section=locked
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\locked\application]'
run rdctl list-settings --output reg --section=locked
assert_success
assert_output --partial '[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Rancher Desktop\locked\application]'
}

@test 'generates registry output for hkcu/defaults' {
for option in reg,hkcu,defaults reg,hkcu; do
run rdctl list-settings --output "$option"
assert_success
assert_output --partial '[HKEY_CURRENT_USER\SOFTWARE\Policies\Rancher Desktop\defaults\application]'
done
run rdctl list-settings --output reg --reg-hive=hkcu
assert_success
assert_output --partial '[HKEY_CURRENT_USER\SOFTWARE\Policies\Rancher Desktop\defaults\application]'
run rdctl list-settings --output reg --reg-hive=hkcu --section=defaults
assert_success
assert_output --partial '[HKEY_CURRENT_USER\SOFTWARE\Policies\Rancher Desktop\defaults\application]'
}

@test 'generates registry output for hkcu/locked' {
run rdctl list-settings --output reg,hkcu,locked
run rdctl list-settings --output reg --reg-hive=hkcu --section=locked
assert_success
assert_output --partial '[HKEY_CURRENT_USER\SOFTWARE\Policies\Rancher Desktop\locked\application]'
}
Expand Down
71 changes: 20 additions & 51 deletions src/go/rdctl/cmd/listSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,71 +69,40 @@ and Y is either "defaults" or "locked", depending on which deployment profile yo

func init() {
rootCmd.AddCommand(listSettingsCmd)
listSettingsCmd.Flags().StringVarP(&outputSettings, "output", "", "", fmt.Sprintf("output format: %s|%s[,hive][,type], default %s", jsonFormat, regFormat, jsonFormat))
listSettingsCmd.Flags().StringVarP(&outputSettingsFlags.Format, "output", "", jsonFormat, fmt.Sprintf("output format: %s|%s, default %s", jsonFormat, regFormat, jsonFormat))
listSettingsCmd.Flags().StringVarP(&outputSettingsFlags.RegistryHive, "reg-hive", "", "", fmt.Sprintf("registry hive: %s|%s, default %s", reg.HklmRegistryHive, reg.HkcuRegistryHive, reg.HklmRegistryHive))
listSettingsCmd.Flags().StringVarP(&outputSettingsFlags.RegistryProfileType, "section", "", "", fmt.Sprintf("registry section: %s|%s, default %s", defaultsRegistrySection, lockedRegistrySection, defaultsRegistrySection))
}

func calcOutputFormatFlags() error {
if outputSettings == "" || outputSettings == jsonFormat {
outputSettingsFlags.Format = jsonFormat
return nil
func validateOutputFormatFlags() error {
if outputSettingsFlags.Format != jsonFormat && outputSettingsFlags.Format != regFormat {
return fmt.Errorf(`invalid output format of "%s"`, outputSettingsFlags.Format)
}
parts := strings.Split(outputSettings, ",")
if parts[0] == jsonFormat {
if len(parts) > 1 {
return fmt.Errorf(`the json output format takes no sub-formats, got "%s"`, outputSettings)
if outputSettingsFlags.Format == jsonFormat {
if outputSettingsFlags.RegistryHive != "" || outputSettingsFlags.RegistryProfileType != "" {
return fmt.Errorf("registry hive and profile can't be specified with json")
}
outputSettingsFlags.Format = jsonFormat
return nil
}
if parts[0] != regFormat {
return fmt.Errorf(`expecting an output format of '%s' or '%s', got "%s"`, jsonFormat, regFormat, outputSettings)
}
outputSettingsFlags.Format = regFormat
for _, part := range parts[1:] {
switch part {
case reg.HklmRegistryHive:
if outputSettingsFlags.RegistryHive != "" {
return fmt.Errorf(`already specified registry hive "%s" in "%s", can't respecify`, outputSettingsFlags.RegistryHive, outputSettings)
}
outputSettingsFlags.RegistryHive = part
break

case reg.HkcuRegistryHive:
if outputSettingsFlags.RegistryHive != "" {
return fmt.Errorf(`already specified registry hive "%s" in "%s", can't respecify`, outputSettingsFlags.RegistryHive, outputSettings)
}
outputSettingsFlags.RegistryHive = part
break

case defaultsRegistrySection:
if outputSettingsFlags.RegistryProfileType != "" {
return fmt.Errorf(`already specified registry section "%s" in "%s", can't respecify`, outputSettingsFlags.RegistryProfileType, outputSettings)
}
outputSettingsFlags.RegistryProfileType = part
break

case lockedRegistrySection:
if outputSettingsFlags.RegistryProfileType != "" {
return fmt.Errorf(`already specified registry section "%s" in "%s", can't respecify`, outputSettingsFlags.RegistryProfileType, outputSettings)
}
outputSettingsFlags.RegistryProfileType = part
break

default:
return fmt.Errorf(`expecting a reg output-format parameter, got "%s" in "%s"`, part, outputSettings)
}
}
if outputSettingsFlags.RegistryHive == "" {
switch outputSettingsFlags.RegistryHive {
case reg.HklmRegistryHive, reg.HkcuRegistryHive:
case "":
outputSettingsFlags.RegistryHive = reg.HklmRegistryHive
default:
return fmt.Errorf("invalid registry hive of '%s' specified", outputSettingsFlags.RegistryHive)
}
if outputSettingsFlags.RegistryProfileType == "" {
switch outputSettingsFlags.RegistryProfileType {
case defaultsRegistrySection, lockedRegistrySection:
case "":
outputSettingsFlags.RegistryProfileType = defaultsRegistrySection
default:
return fmt.Errorf("invalid registry section of '%s' specified", outputSettingsFlags.RegistryProfileType)
}
return nil
}

func getListSettings() ([]byte, error) {
err := calcOutputFormatFlags()
err := validateOutputFormatFlags()
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions src/go/rdctl/pkg/reg/reg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ func TestJsonToRegFormat(t *testing.T) {
"allowed": {
"enabled": false,
"list": ["found", "fully", "bawdy", "tarot"]
}
},
"installed": {
"extcheck1": "a",
"extcheck2": "b"
}
},
"pathManagementStrategy": "manual",
"telemetry": {
Expand Down Expand Up @@ -256,10 +260,6 @@ func TestJsonToRegFormat(t *testing.T) {
"check2": false
}
},
"extensions": {
"extcheck1": true,
"extcheck2": false
},
"experimental": {
"virtualMachine": {
"type": "qemu",
Expand Down

0 comments on commit 8b652c4

Please sign in to comment.