This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_plan.go
63 lines (54 loc) · 1.46 KB
/
utils_plan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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")
}
}