/
resources.go
52 lines (43 loc) · 1.2 KB
/
resources.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
package interactive
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/similarweb/kubectl-interactive/printers"
"k8s.io/cli-runtime/pkg/resource"
)
// tableData describe the present table
type tableData struct {
Name string
Namespace string
}
// tableHeader set a table headers
var tableHeader = []printers.PrintTableConfig{
{Header: "Name", Key: "Name"},
{Header: "Namespace", Key: "Namespace"},
}
// PrintResources will add all the resource data the given buffer
func PrintResources(resourceInfo []*resource.Info, like string, buf *bytes.Buffer) ([]*resource.Info, error) {
resourcesResults := []*resource.Info{}
data := []tableData{}
for _, info := range resourceInfo {
if like != "" && !strings.Contains(strings.ToLower(info.Name), strings.ToLower(like)) {
continue
}
resourcesResults = append(resourcesResults, info)
data = append(data, tableData{
Name: info.Name,
Namespace: info.Namespace,
})
}
resourceBuffer, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("could not marshal contexts. error: %s", err.Error())
}
err = printers.Table(tableHeader, resourceBuffer, buf)
if err != nil {
return nil, err
}
return resourcesResults, nil
}