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

Commit

Permalink
adds more verbose plan output 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 08cbe0a commit 8c7083f
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/spf13/cobra"
"gitlab.com/qm64/backpack/conn"
"gitlab.com/qm64/backpack/pkg"
"gitlab.com/qm64/backpack/templating"
)

Expand All @@ -24,7 +23,8 @@ var planCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(planCmd)
planCmd.Flags().BoolP("diff", "d", true, "calculate and show the differences and changes")
planCmd.Flags().BoolP("diff", "d", false, "show the differences of changes applied")
planCmd.Flags().Bool("verbose", false, "show the full changes applied not just top level")
planCmd.Flags().StringP("values", "v", "", "specifies the file to use for values and ensure to populate the Go Templates")
planCmd.Flags().BoolP("unpacked", "u", false, "instead of reading from a file, read from a directory")
planCmd.Flags().Bool("debug", false, "prints the jobs on stdout instead of sending them to nomad")
Expand All @@ -40,13 +40,16 @@ func planRun(cmd *cobra.Command, args []string) {
log.Fatalf("Error creating new Nomad Client: %s", err)
}

vfPath, _ := cmd.Flags().GetString("values")
values := pkg.ValuesType{}
if vfPath != "" {
values, err = pkg.ValuesFromFile(vfPath)
if err != nil {
log.Fatalf("Error reading the value file: %s", err)
}
values := getValuesFromCLIInput(cmd)

verbosePlan, err := cmd.Flags().GetBool("verbose")
if err != nil {
log.Fatalf("Error parsing CLI flags (verbose): %s", err)
}

debugFlag, err := cmd.Flags().GetBool("debug")
if err != nil {
log.Fatalf("Error parsing CLI flags (debug): %s", err)
}

// Populate the template into job files 💪
Expand All @@ -55,7 +58,6 @@ func planRun(cmd *cobra.Command, args []string) {
log.Fatalf("Error building the HCL files: %s", err)
}

debugFlag, _ := cmd.Flags().GetBool("debug")
if debugFlag {
for name, hcl := range bts {
log.Printf("File: %s\n", name)
Expand All @@ -67,21 +69,48 @@ func planRun(cmd *cobra.Command, args []string) {
// Prepare a table for the output in a buffer. This is done so that we can
// have a table after outputting the Plans for each job
rt, wt, err := os.Pipe()
if err != nil {
log.Fatal("Error preparing the output table:", err)
}

defer rt.Close()
w := tabwriter.NewWriter(wt, 3, 0, 2, ' ', 0)
fmt.Fprintln(w, "File Name\tCheck Index\tDiff Type\tPlan warnings")
showPlanDiff, _ := cmd.Flags().GetBool("diff")

// For each job file perform the plan! 🚀
for name, hcl := range bts {
p, err := client.Plan(string(hcl), showPlanDiff)
job, err := client.GetJob(string(hcl))
if err != nil {
log.Fatalf("Error obtaining job %s: %s", name, err)
}

// always show the diff for plan
p, err := client.Plan(job, true)
if err != nil {
log.Fatalf("Error running %s: %s", name, err)
}
// Write in the table in the buffer output
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)
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)
}
}
for _, object := range p.Diff.Objects {
if object.Type == "Edited" || verbosePlan {
fmt.Printf(" %s %s\n", getDiffSimbol(object.Type), object.Name)
}
}
for _, tg := range p.Diff.TaskGroups {
fmt.Printf(" %s Task Group: %s\n", getDiffSimbol(tg.Type), tg.Name)
}
}

// Flushes all the table output
// Flushes all the table output after all the plans output.
w.Flush()
wt.Close()
output, err := ioutil.ReadAll(rt)
Expand Down

0 comments on commit 8c7083f

Please sign in to comment.