-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
105 lines (95 loc) · 3.21 KB
/
Copy pathtypes.go
File metadata and controls
105 lines (95 loc) · 3.21 KB
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
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
inventoryv1alpha2 "go.miloapis.com/inventory/api/v1alpha2"
)
func newTypesCmd() *cobra.Command {
return &cobra.Command{
Use: "types",
Short: "List node and edge types (the schema registry)",
Long: `List the NodeType and EdgeType definitions that make up the inventory schema
registry. Each type declares the attribute keys its nodes or edges may carry;
'datumctl inventory get <TYPE>' uses these to choose columns.`,
Example: ` datumctl inventory types
datumctl inventory types -o yaml`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
c, err := newClient()
if err != nil {
return err
}
ctx := cmd.Context()
var nodeTypes inventoryv1alpha2.NodeTypeList
if err := c.List(ctx, &nodeTypes); err != nil {
return listErr("nodetypes", err)
}
var edgeTypes inventoryv1alpha2.EdgeTypeList
if err := c.List(ctx, &edgeTypes); err != nil {
return listErr("edgetypes", err)
}
sort.Slice(nodeTypes.Items, func(i, j int) bool { return nodeTypes.Items[i].Name < nodeTypes.Items[j].Name })
sort.Slice(edgeTypes.Items, func(i, j int) bool { return edgeTypes.Items[i].Name < edgeTypes.Items[j].Name })
format, _ := cmd.Flags().GetString("output")
switch format {
case "json":
return writeMarshaled(cmd, json.MarshalIndent, nodeTypes, edgeTypes)
case "yaml":
return writeMarshaled(cmd, func(v any, _, _ string) ([]byte, error) { return yaml.Marshal(v) }, nodeTypes, edgeTypes)
case "", "table":
rows := make([][]string, 0, len(nodeTypes.Items)+len(edgeTypes.Items))
for _, nt := range nodeTypes.Items {
rows = append(rows, []string{"NodeType", nt.Name, orNone(nt.Spec.DisplayName), attrKeys(nodeAttrKeys(nt))})
}
for _, et := range edgeTypes.Items {
rows = append(rows, []string{"EdgeType", et.Name, orNone(et.Spec.DisplayName), attrKeys(edgeAttrKeys(et))})
}
return printTable(cmd.OutOrStdout(), []string{"KIND", "NAME", "DISPLAY", "ATTRIBUTES"}, rows)
default:
return fmt.Errorf("invalid value %q for --output; allowed: table, json, yaml", format)
}
},
}
}
func nodeAttrKeys(nt inventoryv1alpha2.NodeType) []string {
keys := make([]string, 0, len(nt.Spec.Attributes))
for _, a := range nt.Spec.Attributes {
k := a.Key
if a.Required {
k += "*"
}
keys = append(keys, k)
}
return keys
}
func edgeAttrKeys(et inventoryv1alpha2.EdgeType) []string {
keys := make([]string, 0, len(et.Spec.Attributes))
for _, a := range et.Spec.Attributes {
k := a.Key
if a.Required {
k += "*"
}
keys = append(keys, k)
}
return keys
}
func attrKeys(keys []string) string {
if len(keys) == 0 {
return none
}
return strings.Join(keys, ", ")
}
func writeMarshaled(cmd *cobra.Command, marshal func(any, string, string) ([]byte, error), nodeTypes inventoryv1alpha2.NodeTypeList, edgeTypes inventoryv1alpha2.EdgeTypeList) error {
payload := map[string]any{"nodeTypes": nodeTypes.Items, "edgeTypes": edgeTypes.Items}
b, err := marshal(payload, "", " ")
if err != nil {
return err
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), strings.TrimRight(string(b), "\n"))
return err
}