-
Notifications
You must be signed in to change notification settings - Fork 3
/
tabletemplate.go
72 lines (62 loc) · 2.54 KB
/
tabletemplate.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
/**
* Copyright 2020 Napptive
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package printer
import (
"reflect"
grpc_catalog_common_go "github.com/napptive/grpc-catalog-common-go"
grpc_catalog_go "github.com/napptive/grpc-catalog-go"
"github.com/napptive/nerrors/pkg/nerrors"
)
// OpResponseTemplate with the table representation of an OpResponse.
const OpResponseTemplate = `STATUS INFO
{{.StatusName}} {{.UserInfo}}
`
// InfoAppResponseTemplate with the table representation of an InfoAppResponse.
const InfoAppResponseTemplate = `APP_ID NAME
{{.Namespace}}/{{.ApplicationName}}:{{.Tag}} {{.Metadata.Name}}
DESCRIPTION
{{.Metadata.Description}}
{{if .Metadata.Requires.Traits}}TRAITS
{{range $name := .Metadata.Requires.Traits}}{{$name}}
{{end}}{{end}}{{if .Metadata.Requires.Scopes}}SCOPES
{{range $name := .Metadata.Requires.Scopes}}{{$name}}
{{end}}{{end}}{{if .Metadata.Requires.K8S}}K8S_ENTITIES
{{range .Metadata.Requires.K8S}}{{.ApiVersion}}/{{.Kind}}
{{end}}{{end}}
README
{{toString .ReadmeFile}}
`
const ApplicationListTemplate = `APPLICATION NAME
{{range $other, $app := .Applications}}{{fromApplicationSummary $app}}{{end}}`
const SummaryResponseTemplate = `NAMESPACES APPLICATIONS TAGS
{{.NumNamespaces}} {{.NumApplications}} {{.NumTags}}
`
// structTemplates map associating type and template to print it.
var structTemplates = map[reflect.Type]string{
reflect.TypeOf(&grpc_catalog_common_go.OpResponse{}): OpResponseTemplate,
reflect.TypeOf(&grpc_catalog_go.InfoApplicationResponse{}): InfoAppResponseTemplate,
reflect.TypeOf(&grpc_catalog_go.ApplicationList{}): ApplicationListTemplate,
reflect.TypeOf(&grpc_catalog_go.SummaryResponse{}): SummaryResponseTemplate,
//
}
// GetTemplate returns a template to print an arbitrary structure in table format.
func GetTemplate(result interface{}) (*string, error) {
template, exists := structTemplates[reflect.TypeOf(result)]
if !exists {
return nil, nerrors.NewUnimplementedError("no template is available to print %s", reflect.TypeOf(result).String())
}
return &template, nil
}