Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
format the plan diff
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Setale <[email protected]>
  • Loading branch information
koalalorenzo committed Nov 15, 2020
1 parent 8c7083f commit aa6059e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 28 deletions.
52 changes: 42 additions & 10 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"text/tabwriter"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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! 🚀
Expand All @@ -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()
Expand Down
14 changes: 0 additions & 14 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
}
63 changes: 63 additions & 0 deletions cmd/utils_plan.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
8 changes: 4 additions & 4 deletions test_files/backpack/example.nomad
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit aa6059e

Please sign in to comment.