-
Notifications
You must be signed in to change notification settings - Fork 6
/
printer.go
104 lines (87 loc) · 2.41 KB
/
printer.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
package apps
import (
"fmt"
"github.com/giantswarm/microerror"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/printers"
"github.com/giantswarm/kubectl-gs/pkg/data/domain/app"
"github.com/giantswarm/kubectl-gs/pkg/output"
)
func (r *runner) printOutput(appResource app.Resource) error {
var (
err error
printer printers.ResourcePrinter
resource runtime.Object
)
switch {
case output.IsOutputDefault(r.flag.print.OutputFormat):
resource = getTable(appResource)
printOptions := printers.PrintOptions{
WithNamespace: r.flag.AllNamespaces,
}
printer = printers.NewTablePrinter(printOptions)
case output.IsOutputName(r.flag.print.OutputFormat):
resource = appResource.Object()
err = output.PrintResourceNames(r.stdout, resource)
if err != nil {
return microerror.Mask(err)
}
return nil
default:
resource = appResource.Object()
printer, err = r.flag.print.ToPrinter()
if err != nil {
return microerror.Mask(err)
}
}
err = printer.PrintObj(resource, r.stdout)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *runner) printNoMatchOutput() {
fmt.Fprintf(r.stdout, "No App CRD found.\n")
fmt.Fprintf(r.stdout, "Please check you are accessing a management cluster\n\n")
}
func (r *runner) printNoResourcesOutput() {
fmt.Fprintf(r.stdout, "No apps found.\n")
fmt.Fprintf(r.stdout, "To create an app, please check\n\n")
fmt.Fprintf(r.stdout, " kubectl gs template app --help\n")
}
func getTable(appResource app.Resource) *metav1.Table {
// Creating a custom table resource.
table := &metav1.Table{}
table.ColumnDefinitions = []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Version", Type: "string"},
{Name: "Last Deployed", Type: "string", Format: "date-time"},
{Name: "Status", Type: "string"},
}
switch c := appResource.(type) {
case *app.App:
table.Rows = append(table.Rows, getAppRow(*c))
case *app.Collection:
for _, appItem := range c.Items {
table.Rows = append(table.Rows, getAppRow(appItem))
}
}
return table
}
func getAppRow(a app.App) metav1.TableRow {
if a.CR == nil {
return metav1.TableRow{}
}
return metav1.TableRow{
Cells: []interface{}{
a.CR.Name,
a.CR.Status.Version,
output.TranslateTimestampSince(a.CR.Status.Release.LastDeployed),
a.CR.Status.Release.Status,
},
Object: runtime.RawExtension{
Object: a.CR,
},
}
}