package format import ( "fmt" "strings" "edge-infra.dev/pkg/edge/api/graph/model" ) type actionType int const ( applyAction actionType = iota deleteAction ) // GenerateDeleteOutput creates a formatted string for displaying deletion // errors to the user func GenerateDeleteOutput(inputType string, errors []*model.OperatorInterventionErrorResponse) string { return generateOutput(deleteAction, inputType, errors) } // GenerateApplyOutput creates a formatted string for displaying errors that // occur during create and update mutations to the user func GenerateApplyOutput(inputType string, errors []*model.OperatorInterventionErrorResponse) string { return generateOutput(applyAction, inputType, errors) } func generateOutput(action actionType, inputType string, errors []*model.OperatorInterventionErrorResponse) string { var pastAction string var presentAction string switch action { case applyAction: pastAction = "applied" presentAction = "applying" case deleteAction: pastAction = "deleted" presentAction = "deleting" } if len(errors) == 0 { return fmt.Sprintf("\nOI %s %s\n", inputType, pastAction) } var sb strings.Builder sb.WriteString(fmt.Sprintf("\nErrors occurred when %s OI %s: \n", presentAction, inputType)) for _, e := range errors { sb.WriteString(formatError(e)) sb.WriteString("\n") } sb.WriteString(fmt.Sprintf("\nOI %s not %s.\n", inputType, pastAction)) return sb.String() } // generates an error message string corresponding to the particular error func formatError(e *model.OperatorInterventionErrorResponse) string { resp := fmt.Sprintf("\tError: %s", e.Type) var args []string if e.Role != nil { args = append(args, fmt.Sprintf("Role: %q", *e.Role)) } if e.Privilege != nil { args = append(args, fmt.Sprintf("Privilege: %q", *e.Privilege)) } if e.Command != nil { args = append(args, fmt.Sprintf("Command: %q", *e.Command)) } if len(args) != 0 { resp = fmt.Sprintf("%s. Details: ", resp) } resp = fmt.Sprintf("%s%s", resp, strings.Join(args, ", ")) return resp }