From aa6059ef3ab58d3f35c2c9fe0ed2045ba16c127d Mon Sep 17 00:00:00 2001 From: Lorenzo Setale Date: Sun, 15 Nov 2020 17:33:32 +0100 Subject: [PATCH] format the plan diff Signed-off-by: Lorenzo Setale --- cmd/plan.go | 52 ++++++++++++++++++++----- cmd/utils.go | 14 ------- cmd/utils_plan.go | 63 +++++++++++++++++++++++++++++++ test_files/backpack/example.nomad | 8 ++-- 4 files changed, 109 insertions(+), 28 deletions(-) create mode 100644 cmd/utils_plan.go diff --git a/cmd/plan.go b/cmd/plan.go index 4fd0ff4..fe0c060 100644 --- a/cmd/plan.go +++ b/cmd/plan.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "log" "os" + "strings" "text/tabwriter" "github.com/spf13/cobra" @@ -75,6 +76,7 @@ func planRun(cmd *cobra.Command, args []string) { defer rt.Close() w := tabwriter.NewWriter(wt, 3, 0, 2, ' ', 0) + fmt.Fprintln(w, "Recap of Job Plans") fmt.Fprintln(w, "File Name\tCheck Index\tDiff Type\tPlan warnings") // For each job file perform the plan! 🚀 @@ -93,23 +95,53 @@ func planRun(cmd *cobra.Command, args []string) { fmt.Fprintf(w, "%s\t%d\t%s\t%s\t\n", name, p.JobModifyIndex, p.Diff.Type, p.Warnings) // Write in the output the diff from the previous output - log.Print("Plan for job", name) - fmt.Printf(" %s Job: %s\n", getDiffSimbol(p.Diff.Type), *job.ID) + fmt.Printf("Plan for job %s\n", name) + fmt.Printf("%s Job: \"%s\"\n", getDiffSimbol(p.Diff.Type), *job.ID) for _, field := range p.Diff.Fields { - if field.Type == "Edited" || verbosePlan { - fmt.Printf(" %s %s: %s -> %s\n", getDiffSimbol(field.Type), field.Name, field.Old, field.New) - } + printFieldsDiff(field, verbosePlan, 1) } for _, object := range p.Diff.Objects { - if object.Type == "Edited" || verbosePlan { - fmt.Printf(" %s %s\n", getDiffSimbol(object.Type), object.Name) - } + printObjectDiff(object, verbosePlan, 1) } + // Print the task groups changes for _, tg := range p.Diff.TaskGroups { - fmt.Printf(" %s Task Group: %s\n", getDiffSimbol(tg.Type), tg.Name) + // If no changes happed during the plan, continue to the next Task Group + if tg.Type == "None" { + continue + } + + // Build indentation level + indStr := strings.Repeat(indentationStyle, 1) + + fmt.Printf("%s%s Task Group: \"%s\"\n", indStr, getDiffSimbol(tg.Type), tg.Name) + + for _, field := range tg.Fields { + printFieldsDiff(field, verbosePlan, 2) + } + + for _, obj := range tg.Objects { + printObjectDiff(obj, verbosePlan, 2) + } + + for _, task := range tg.Tasks { + // Build indentation level + indStr := strings.Repeat(indentationStyle, 2) + + ann := strings.Join(task.Annotations, ", ") + fmt.Printf("%s%s Task: \"%s\" (%s)\n", indStr, getDiffSimbol(task.Type), task.Name, ann) + + for _, field := range task.Fields { + printFieldsDiff(field, verbosePlan, 3) + } + + for _, obj := range task.Objects { + printObjectDiff(obj, verbosePlan, 3) + } + + } } + fmt.Println() } - // Flushes all the table output after all the plans output. w.Flush() wt.Close() diff --git a/cmd/utils.go b/cmd/utils.go index 01fbff5..583513a 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -118,17 +118,3 @@ func getValuesFromCLIInput(cmd *cobra.Command) pkg.ValuesType { } return values } - -// getDiffSimbol returns the symbol to use for Plan output -func getDiffSimbol(diffType string) string { - switch diffType { - case "Added": - return "+" - case "Deleted": - return "-" - case "Edited": - return "+/-" - default: - return "" - } -} diff --git a/cmd/utils_plan.go b/cmd/utils_plan.go new file mode 100644 index 0000000..15d6b9b --- /dev/null +++ b/cmd/utils_plan.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/hashicorp/nomad/api" +) + +var ( + indentationStyle = " " +) + +// getDiffSimbol returns the symbol to use for Plan output +func getDiffSimbol(diffType string) string { + switch diffType { + case "Added": + return "+" + case "Deleted": + return "-" + case "Edited": + return "~" + default: + return " " + } +} + +func printObjectDiff(object *api.ObjectDiff, verbosePlan bool, indentation int) { + // Build indentation level + indStr := strings.Repeat(indentationStyle, indentation) + // Print only if verbose or edited + if object.Type == "Edited" || verbosePlan { + fmt.Printf("%s%s %s {\n", indStr, getDiffSimbol(object.Type), object.Name) + for _, field := range object.Fields { + printFieldsDiff(field, verbosePlan, indentation+1) + } + + for _, subobj := range object.Objects { + printObjectDiff(subobj, verbosePlan, indentation+1) + } + fmt.Printf("%s}\n", indStr) + } +} + +func printFieldsDiff(field *api.FieldDiff, verbosePlan bool, indentation int) { + // Build indentation level + if field.Type == "Edited" || verbosePlan { + indStr := strings.Repeat(" ", indentation) + marker := getDiffSimbol(field.Type) + + if field.Type == "Edited" { + fmt.Printf("%s%s %s: \"%s\" -> \"%s\" ", indStr, marker, field.Name, field.Old, field.New) + } else { + fmt.Printf("%s%s %s: \"%s\" ", indStr, marker, field.Name, field.New) + } + + if len(field.Annotations) > 0 { + ann := strings.Join(field.Annotations, ", ") + fmt.Printf("(%s)", ann) + } + fmt.Print("\n") + } +} diff --git a/test_files/backpack/example.nomad b/test_files/backpack/example.nomad index 9278db1..b9a0f8a 100644 --- a/test_files/backpack/example.nomad +++ b/test_files/backpack/example.nomad @@ -283,10 +283,10 @@ job "example" { # # https://www.nomadproject.io/docs/job-specification/logs.html # - # logs { - # max_files = 10 - # max_file_size = 15 - # } + logs { + max_files = 10 + max_file_size = 15 + } # The "resources" stanza describes the requirements a task needs to # execute. Resource requirements include memory, network, cpu, and more.