/
print.go
138 lines (124 loc) · 5.25 KB
/
print.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cli
import (
"fmt"
"os"
"path/filepath"
"github.com/christosgalano/bruh/internal/types"
"github.com/olekukonko/tablewriter"
)
// printFileNormal prints the file's information in normal format.
func printFileNormal(bicepFile *types.BicepFile, filename string, outdated bool, mode types.Mode) {
fmt.Printf("%s:\n", filename)
for _, resource := range bicepFile.Resources {
latestAPIVersion := resource.AvailableAPIVersions[0]
if mode == types.ModeScan {
if resource.CurrentAPIVersion != latestAPIVersion {
fmt.Printf(" - %s is using %s while the latest version is %s\n", resource.ID, resource.CurrentAPIVersion, latestAPIVersion)
} else if !outdated {
fmt.Printf(" - %s is using the latest version %s\n", resource.ID, resource.CurrentAPIVersion)
}
} else {
fmt.Printf(" + Updated %s to version %s\n", resource.ID, resource.CurrentAPIVersion)
}
}
fmt.Println()
}
// printFileTable prints the file's information in tabular format.
func printFileTable(bicepFile *types.BicepFile, outdated bool) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Resource", "Current API Version", "Latest API Version"})
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER})
fmt.Printf("%s:\n", bicepFile.Path)
for _, resource := range bicepFile.Resources {
if outdated && (resource.CurrentAPIVersion == resource.AvailableAPIVersions[0]) {
continue
}
table.Append([]string{resource.ID, resource.CurrentAPIVersion, resource.AvailableAPIVersions[0]})
}
table.Render()
fmt.Println()
}
// printFileTable prints the file's information in tabular Markdown format.
func printFileMarkdown(bicepFile *types.BicepFile, outdated bool) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Resource", "Current API Version", "Latest API Version"})
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER})
// Markdown specific
table.SetCenterSeparator("|")
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
fmt.Printf("%s:\n", bicepFile.Path)
for _, resource := range bicepFile.Resources {
if outdated && (resource.CurrentAPIVersion == resource.AvailableAPIVersions[0]) {
continue
}
table.Append([]string{resource.ID, resource.CurrentAPIVersion, resource.AvailableAPIVersions[0]})
}
table.Render()
fmt.Println()
}
// printDirectoryNormal prints the directory's information in normal format.
func printDirectoryNormal(bicepDirectory *types.BicepDirectory, outdated bool, mode types.Mode) {
absolutePath, err := filepath.Abs(bicepDirectory.Path)
if err != nil {
absolutePath = bicepDirectory.Path
}
fmt.Printf("%s:\n\n", absolutePath)
for i := range bicepDirectory.Files {
filename, err := filepath.Rel(bicepDirectory.Path, bicepDirectory.Files[i].Path)
if err != nil {
filename = bicepDirectory.Files[i].Path
}
printFileNormal(&bicepDirectory.Files[i], filename, outdated, mode)
}
}
// printDirectoryTable prints the directory's information in tabular format.
func printDirectoryTable(bicepDirectory *types.BicepDirectory, outdated bool) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"File", "Resource", "Current API Version", "Latest API Version"})
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER})
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.SetRowLine(true)
fmt.Printf("%s:\n\n", bicepDirectory.Path)
for _, file := range bicepDirectory.Files {
for _, resource := range file.Resources {
filename, err := filepath.Rel(bicepDirectory.Path, file.Path)
if err != nil {
filename = file.Path
}
if outdated && (resource.CurrentAPIVersion == resource.AvailableAPIVersions[0]) {
continue
}
table.Append([]string{filename, resource.ID, resource.CurrentAPIVersion, resource.AvailableAPIVersions[0]})
}
}
table.Render()
fmt.Println()
}
// printDirectoryMarkdown prints the directory's information in tabular Markdown format.
func printDirectoryMarkdown(bicepDirectory *types.BicepDirectory, outdated bool) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"File", "Resource", "Current API Version", "Latest API Version"})
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER})
table.SetAutoMergeCellsByColumnIndex([]int{0})
// Markdown specific
table.SetRowLine(false)
table.SetCenterSeparator("|")
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
fmt.Printf("### %s\n\n", bicepDirectory.Path)
for _, file := range bicepDirectory.Files {
for _, resource := range file.Resources {
filename, err := filepath.Rel(bicepDirectory.Path, file.Path)
if err != nil {
filename = file.Path
}
if outdated && (resource.CurrentAPIVersion == resource.AvailableAPIVersions[0]) {
continue
}
table.Append([]string{filename, resource.ID, resource.CurrentAPIVersion, resource.AvailableAPIVersions[0]})
}
}
table.Render()
fmt.Println()
}