Skip to content

Commit 75510c3

Browse files
committed
human readable
Signed-off-by: Harper, Jason M <[email protected]>
1 parent c166820 commit 75510c3

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

cmd/config/restore.go

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ const restoreCmdName = "restore"
2626

2727
// flagValue represents a single flag name and value pair, preserving order
2828
type flagValue struct {
29-
flagName string
30-
value string
29+
fieldName string // the human-readable field name from config file (e.g., "Cores per Socket")
30+
flagName string // the command-line flag name (e.g., "cores")
31+
value string
3132
}
3233

3334
var restoreExamples = []string{
@@ -130,8 +131,15 @@ func runRestoreCmd(cmd *cobra.Command, args []string) error {
130131

131132
// show what will be restored
132133
fmt.Printf("Configuration settings to restore from %s:\n", configFilePath)
134+
// find the longest field name for alignment
135+
maxLen := 0
133136
for _, fv := range flagValues {
134-
fmt.Printf(" --%s %s\n", fv.flagName, fv.value)
137+
if len(fv.fieldName) > maxLen {
138+
maxLen = len(fv.fieldName)
139+
}
140+
}
141+
for _, fv := range flagValues {
142+
fmt.Printf(" %-*s: %s\n", maxLen, fv.fieldName, fv.value)
135143
}
136144
fmt.Println()
137145

@@ -267,7 +275,8 @@ func parseConfigFile(filePath string) ([]flagValue, error) {
267275
line := scanner.Text()
268276
matches := flagLineRegex.FindStringSubmatch(line)
269277
if len(matches) == 4 {
270-
// matches[1] = field name (not used)
278+
// matches[1] = field name (e.g., "Cores per Socket")
279+
fieldName := strings.TrimSpace(matches[1])
271280
rawValue := strings.TrimSpace(matches[2])
272281
flagStr := matches[3]
273282

@@ -282,8 +291,9 @@ func parseConfigFile(filePath string) ([]flagValue, error) {
282291
}
283292

284293
flagValues = append(flagValues, flagValue{
285-
flagName: flagName,
286-
value: convertedValue,
294+
fieldName: fieldName,
295+
flagName: flagName,
296+
value: convertedValue,
287297
})
288298
}
289299
}
@@ -420,14 +430,14 @@ func parseAndPresentResults(stderrOutput string, flagValues []flagValue) {
420430
return
421431
}
422432

423-
// Parse stderr for success and error messages
424-
// Looking for patterns like:
425-
// - "set <flag> to <value>"
426-
// - "failed to set <flag> to <value>"
427-
// - "error: ..." messages related to flags
433+
// flagResult stores the success/failure status and value for each flag
434+
type flagResult struct {
435+
success bool
436+
value string
437+
}
428438

429439
// Build a map of flag names to their results
430-
flagResults := make(map[string]string)
440+
flagResults := make(map[string]flagResult)
431441

432442
// Regex patterns to match success and error messages
433443
// Flag names can contain hyphens, so use [\w-]+ instead of \S+
@@ -443,7 +453,7 @@ func parseAndPresentResults(stderrOutput string, flagValues []flagValue) {
443453
if len(matches) >= 3 {
444454
flagName := matches[1]
445455
value := strings.TrimSpace(matches[2])
446-
flagResults[flagName] = fmt.Sprintf("✓ Set %s to %s", flagName, value)
456+
flagResults[flagName] = flagResult{success: true, value: value}
447457
}
448458
}
449459

@@ -453,20 +463,31 @@ func parseAndPresentResults(stderrOutput string, flagValues []flagValue) {
453463
if len(matches) >= 3 {
454464
flagName := matches[1]
455465
value := strings.TrimSpace(matches[2])
456-
flagResults[flagName] = fmt.Sprintf("✗ Failed to set %s to %s", flagName, value)
466+
flagResults[flagName] = flagResult{success: false, value: value}
457467
}
458468
}
459469
}
460470

461471
// Present results in the order of flagValues
462472
if len(flagValues) > 0 {
463473
fmt.Println("\nConfiguration Results:")
474+
// find the longest field name for alignment
475+
maxLen := 0
476+
for _, fv := range flagValues {
477+
if len(fv.fieldName) > maxLen {
478+
maxLen = len(fv.fieldName)
479+
}
480+
}
464481
for _, fv := range flagValues {
465482
if result, found := flagResults[fv.flagName]; found {
466-
fmt.Printf(" %s\n", result)
483+
if result.success {
484+
fmt.Printf(" ✓ %-*s: %s\n", maxLen, fv.fieldName, result.value)
485+
} else {
486+
fmt.Printf(" ✗ %-*s: %s\n", maxLen, fv.fieldName, result.value)
487+
}
467488
} else {
468489
// If no explicit success or error was found, show unknown status
469-
fmt.Printf(" ? %s: status unknown\n", fv.flagName)
490+
fmt.Printf(" ? %-*s: status unknown\n", maxLen, fv.fieldName)
470491
}
471492
}
472493
fmt.Println()

0 commit comments

Comments
 (0)