Skip to content

Commit

Permalink
Use a better function name and remove diagnostic printfs.
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Promislow <epromislow@suse.com>
  • Loading branch information
ericpromislow committed Jun 20, 2023
1 parent 507c182 commit 8c36a07
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/go/rdctl/pkg/reg/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,17 @@ func escape(s string) string {
// an array of lines representing the current value
// boolean: true if the current entry is empty
// error
func reflecto(pathParts []string, v reflect.Value, jsonTag string) ([]string, bool, error) {
func convertToRegFormat(pathParts []string, v reflect.Value, jsonTag string) ([]string, bool, error) {
kind := v.Kind()
fmt.Printf("type name: %v, kind: %v, tag: %s\n", pathParts, kind, jsonTag)
switch kind {
case reflect.Struct:
retLines := []string{"", fmt.Sprintf("[%s]", strings.Join(pathParts, "\\"))}
fmt.Printf("it's a struct...\n")
numFields := v.NumField()
fmt.Printf("It has %d fields\n", numFields)
structIsEmpty := true
for i := 0; i < numFields; i++ {
// name := v.Type().Field(i).Name
fieldTag := v.Type().Field(i).Tag.Get("json")
fieldName := strings.Replace(fieldTag, ",omitempty", "", 1)
newRetLines, isEmpty, err := reflecto(append(pathParts, fieldName), v.Field(i), fieldName)
newRetLines, isEmpty, err := convertToRegFormat(append(pathParts, fieldName), v.Field(i), fieldName)
if err != nil {
return nil, true, err
}
Expand All @@ -52,11 +48,9 @@ func reflecto(pathParts []string, v reflect.Value, jsonTag string) ([]string, bo
return retLines, structIsEmpty, nil
case reflect.Ptr:
if v.IsNil() {
fmt.Printf("... is nil\n")
return []string{}, true, nil
} else {
fmt.Printf("... walk the pointer...\n")
return reflecto(pathParts, v.Elem(), jsonTag)
return convertToRegFormat(pathParts, v.Elem(), jsonTag)
}
case reflect.Slice, reflect.Array:
// Currently, all arrays in the options are arrays of strings
Expand All @@ -77,7 +71,7 @@ func reflecto(pathParts []string, v reflect.Value, jsonTag string) ([]string, bo
retLines := []string{"", fmt.Sprintf("[%s]", strings.Join(pathParts, "\\"))}
for _, key := range v.MapKeys() {
keyAsString := key.String()
innerLines, isEmpty, err := reflecto(append(pathParts, keyAsString), v.MapIndex(key), keyAsString)
innerLines, isEmpty, err := convertToRegFormat(append(pathParts, keyAsString), v.MapIndex(key), keyAsString)
if err != nil {
return nil, isEmpty, err
} else if !isEmpty {
Expand All @@ -89,7 +83,7 @@ func reflecto(pathParts []string, v reflect.Value, jsonTag string) ([]string, bo
if v.IsNil() {
return nil, true, nil
}
return reflecto(pathParts, v.Elem(), jsonTag)
return convertToRegFormat(pathParts, v.Elem(), jsonTag)
case reflect.Bool:
boolValue := map[bool]int{true: 1, false: 0}[v.Bool()]
return []string{fmt.Sprintf(`"%s"=dword:%d`, jsonTag, boolValue)}, false, nil
Expand All @@ -104,7 +98,7 @@ func reflecto(pathParts []string, v reflect.Value, jsonTag string) ([]string, bo
case reflect.String:
return []string{fmt.Sprintf(`"%s"="%s"`, jsonTag, escape(v.String()))}, false, nil
}
return nil, true, fmt.Errorf("reflecto: don't know how to process kind: %s, (%T), value: %v for var %s\n", kind, v, v, jsonTag)
return nil, true, fmt.Errorf("convertToRegFormat: don't know how to process kind: %s, (%T), value: %v for var %s\n", kind, v, v, jsonTag)
}

// Encode multi-stringSZ settings in comma-separated ucs2 little-endian bytes
Expand Down Expand Up @@ -132,7 +126,6 @@ func stringToMultiStringHexBytes(values []string) string {
*/
func JsonToReg(hiveType string, profileType string, settingsBodyAsJSON string) ([]string, error) {
var settings options.ServerSettingsForJSON
// body := `{"version":8,"application":{"adminAccess":false,"debug":true,"extensions":{"allowed":{"enabled":false,"list":[]}},"pathManagementStrategy":"manual","telemetry":{"enabled":true},"updater":{"enabled":false},"autoStart":false,"startInBackground":false,"hideNotificationIcon":false,"window":{"quitOnClose":false}},"containerEngine":{"allowedImages":{"enabled":false,"patterns":[]},"name":"moby"},"WSL":{"integrations":{}},"kubernetes":{"version":"1.25.9","port":6443,"enabled":true,"options":{"traefik":true,"flannel":true},"ingress":{"localhostOnly":false}}}`

fullHiveType, ok := map[string]string{"hklm": "HKEY_LOCAL_MACHINE", "hkcu": "HKEY_CURRENT_USER"}[hiveType]
if !ok {
Expand All @@ -146,15 +139,13 @@ func JsonToReg(hiveType string, profileType string, settingsBodyAsJSON string) (
return nil, fmt.Errorf("error in json: %s\n", err)
}
headerLines := []string{"Windows Registry Editor Version 5.00"}
bodyLines, _, err := reflecto([]string{fullHiveType, "SOFTWARE", "Profiles", "Rancher Desktop", profileType}, reflect.ValueOf(settings), "")
bodyLines, _, err := convertToRegFormat([]string{fullHiveType, "SOFTWARE", "Profiles", "Rancher Desktop", profileType}, reflect.ValueOf(settings), "")
if err != nil {
return nil, err
}
if len(bodyLines) > 0 {
headerLines = append(headerLines, "", fmt.Sprintf("[%s\\%s\\%s]", fullHiveType, "SOFTWARE", "Profiles"))
headerLines = append(headerLines, "", fmt.Sprintf("[%s\\%s\\%s\\%s]", fullHiveType, "SOFTWARE", "Profiles", "Rancher Desktop"))
//headerLines = append(headerLines, "", fmt.Sprintf("%s\\%s\\%s\\%s\\%s", fullHiveType, "SOFTWARE", "Profiles", "Rancher Desktop", profileType))
}
return append(headerLines, bodyLines...), nil
// fmt.Printf("struct thing: %v\n", settings)
}

0 comments on commit 8c36a07

Please sign in to comment.