forked from redhat-developer/odo
/
util.go
84 lines (74 loc) · 2.68 KB
/
util.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
package util
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/openshift/odo/pkg/catalog"
"github.com/openshift/odo/pkg/log"
olm "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
)
// DisplayServices displays the specified services
func DisplayServices(services catalog.ServiceTypeList) {
w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent)
log.Info("Services available through Service Catalog")
fmt.Fprintln(w, "NAME", "\t", "PLANS")
for _, service := range services.Items {
fmt.Fprintln(w, service.ObjectMeta.Name, "\t", strings.Join(service.Spec.PlanList, ","))
}
w.Flush()
}
// DisplayComponents displays the specified components
func DisplayComponents(components []string) {
w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, "NAME")
for _, component := range components {
fmt.Fprintln(w, component)
}
w.Flush()
}
// FilterHiddenServices filters out services that should be hidden from the specified list
func FilterHiddenServices(input catalog.ServiceTypeList) catalog.ServiceTypeList {
inputLength := len(input.Items)
filteredServices := make([]catalog.ServiceType, 0, inputLength)
for _, service := range input.Items {
if !service.Spec.Hidden {
filteredServices = append(filteredServices, service)
}
}
return catalog.ServiceTypeList{
TypeMeta: input.TypeMeta,
ListMeta: input.ListMeta,
Items: filteredServices,
}
}
// FilterHiddenComponents filters out components that should be hidden from the specified list
func FilterHiddenComponents(input []catalog.ComponentType) []catalog.ComponentType {
inputLength := len(input)
filteredComponents := make([]catalog.ComponentType, 0, inputLength)
for _, component := range input {
// we keep the image if it has tags that are no hidden
if len(component.Spec.NonHiddenTags) > 0 {
filteredComponents = append(filteredComponents, component)
}
}
return filteredComponents
}
// DisplayClusterServiceVersions displays installed Operators in a human friendly manner
func DisplayClusterServiceVersions(csvs *olm.ClusterServiceVersionList) {
w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent)
log.Info("Operators available in the cluster")
fmt.Fprintln(w, "NAME", "\t", "CRDs")
for _, csv := range csvs.Items {
fmt.Fprintln(w, csv.ObjectMeta.Name, "\t", CsvOperators(csv.Spec.CustomResourceDefinitions))
}
w.Flush()
}
// CsvOperators returns a string contains all the Kind from the input crds
func CsvOperators(crds olm.CustomResourceDefinitions) string {
var crdsSlice []string
for _, crd := range crds.Owned {
crdsSlice = append(crdsSlice, crd.Kind)
}
return strings.Join(crdsSlice, ", ")
}