forked from altuslabsxyz/devnet-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.go
More file actions
274 lines (241 loc) · 7.37 KB
/
Copy pathget.go
File metadata and controls
274 lines (241 loc) · 7.37 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// cmd/dvb/get.go
package main
import (
"encoding/json"
"fmt"
"os"
"text/tabwriter"
v1 "github.com/altuslabsxyz/devnet-builder/api/proto/gen/v1"
"github.com/fatih/color"
"github.com/spf13/cobra"
k8syaml "sigs.k8s.io/yaml"
)
func newGetCmd() *cobra.Command {
var (
namespace string
output string
showNodes bool
)
cmd := &cobra.Command{
Use: "get [devnet]",
Short: "Display devnet information",
Long: `Display devnet information.
Uses the current context if no devnet is specified.
Examples:
# Get the current context devnet
dvb get
# Get a specific devnet
dvb get my-devnet
# Get devnet with node details
dvb get --show-nodes
# Output in yaml format
dvb get -o yaml
# Get a devnet in a specific namespace
dvb get staging/my-devnet`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var explicitDevnet string
if len(args) > 0 {
explicitDevnet = args[0]
}
ns, name, err := resolveWithSuggestions(explicitDevnet, namespace)
if err != nil {
return err
}
printContextHeader(explicitDevnet, currentContext)
return getDevnet(cmd, ns, name, output, showNodes)
},
}
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Namespace (defaults to server default)")
cmd.Flags().StringVarP(&output, "output", "o", "", "Output format: wide, yaml, json")
cmd.Flags().BoolVar(&showNodes, "show-nodes", false, "Show nodes when getting a devnet")
return cmd
}
func getDevnet(cmd *cobra.Command, namespace, name, output string, showNodes bool) error {
devnet, err := daemonClient.GetDevnet(cmd.Context(), namespace, name)
if err != nil {
return err
}
// Handle yaml/json output
switch output {
case "yaml":
out, err := k8syaml.Marshal(protoDevnetToYAML(devnet))
if err != nil {
return fmt.Errorf("failed to marshal yaml: %w", err)
}
fmt.Print(string(out))
return nil
case "json":
out, err := json.MarshalIndent(devnet, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal json: %w", err)
}
fmt.Println(string(out))
return nil
}
// Print devnet info (default table format)
printDevnetDetail(devnet)
// Optionally show nodes
if showNodes {
fmt.Println()
nodes, err := daemonClient.ListNodes(cmd.Context(), namespace, name)
if err != nil {
return fmt.Errorf("failed to list nodes: %w", err)
}
printNodes(nodes)
}
return nil
}
func printDevnetDetail(d *v1.Devnet) {
fmt.Printf("Name: %s\n", d.Metadata.Name)
fmt.Printf("Namespace: %s\n", d.Metadata.Namespace)
fmt.Printf("Phase: %s\n", colorPhase(d.Status.Phase))
fmt.Printf("Plugin: %s\n", d.Spec.Plugin)
fmt.Printf("Mode: %s\n", d.Spec.Mode)
fmt.Printf("Validators: %d\n", d.Spec.Validators)
if d.Spec.FullNodes > 0 {
fmt.Printf("Full Nodes: %d\n", d.Spec.FullNodes)
}
fmt.Printf("Nodes Ready: %d/%d\n", d.Status.ReadyNodes, d.Status.Nodes)
if d.Status.CurrentHeight > 0 {
fmt.Printf("Block Height: %d\n", d.Status.CurrentHeight)
}
if d.Status.SdkVersion != "" {
fmt.Printf("SDK Version: %s\n", d.Status.SdkVersion)
}
if d.Status.Message != "" {
fmt.Printf("Message: %s\n", d.Status.Message)
}
if len(d.Metadata.Labels) > 0 {
fmt.Printf("Labels: ")
first := true
for k, v := range d.Metadata.Labels {
if !first {
fmt.Printf(", ")
}
fmt.Printf("%s=%s", k, v)
first = false
}
fmt.Println()
}
}
func printNodes(nodes []*v1.Node) {
if len(nodes) == 0 {
fmt.Println("No nodes found")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "INDEX\tROLE\tPHASE\tHEALTH\tHEIGHT\tPEERS")
for _, n := range nodes {
if n == nil {
continue
}
// Safely extract fields with nil checks
var index int32
if n.Metadata != nil {
index = n.Metadata.Index
}
var role string
if n.Spec != nil {
role = n.Spec.Role
}
var phase, health string
var blockHeight int64
var peerCount int32
if n.Status != nil {
phase = n.Status.Phase
blockHeight = n.Status.BlockHeight
peerCount = n.Status.PeerCount
if n.Status.Health != nil {
health = n.Status.Health.Status
}
}
if health == "" {
health = "Unknown"
}
fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%d\t%d\n",
index,
role,
colorPhase(phase),
health,
blockHeight,
peerCount)
}
w.Flush()
}
func colorPhase(phase string) string {
switch phase {
case "Running":
return color.GreenString(phase)
case "Pending", "Provisioning", "Starting":
return color.YellowString(phase)
case "Stopped":
return color.WhiteString(phase)
case "Degraded", "Unhealthy", "Failed":
return color.RedString(phase)
default:
return phase
}
}
// YAMLDevnetOutput represents a devnet in kubectl-style YAML format
type YAMLDevnetOutput struct {
APIVersion string `json:"apiVersion" yaml:"apiVersion"`
Kind string `json:"kind" yaml:"kind"`
Metadata YAMLDevnetMetadataOutput `json:"metadata" yaml:"metadata"`
Spec YAMLDevnetSpecOutput `json:"spec" yaml:"spec"`
Status YAMLDevnetStatusOutput `json:"status" yaml:"status"`
}
// YAMLDevnetMetadataOutput is the metadata section
type YAMLDevnetMetadataOutput struct {
Name string `json:"name" yaml:"name"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
}
// YAMLDevnetSpecOutput is the spec section
type YAMLDevnetSpecOutput struct {
Network string `json:"network" yaml:"network"`
NetworkType string `json:"networkType,omitempty" yaml:"networkType,omitempty"`
NetworkVersion string `json:"networkVersion,omitempty" yaml:"networkVersion,omitempty"`
Validators int32 `json:"validators" yaml:"validators"`
FullNodes int32 `json:"fullNodes,omitempty" yaml:"fullNodes,omitempty"`
Mode string `json:"mode" yaml:"mode"`
}
// YAMLDevnetStatusOutput is the status section
type YAMLDevnetStatusOutput struct {
Phase string `json:"phase" yaml:"phase"`
Nodes int32 `json:"nodes" yaml:"nodes"`
ReadyNodes int32 `json:"readyNodes" yaml:"readyNodes"`
CurrentHeight int64 `json:"currentHeight,omitempty" yaml:"currentHeight,omitempty"`
SdkVersion string `json:"sdkVersion,omitempty" yaml:"sdkVersion,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty"`
}
// protoDevnetToYAML converts a proto Devnet to YAML output format
func protoDevnetToYAML(d *v1.Devnet) *YAMLDevnetOutput {
return &YAMLDevnetOutput{
APIVersion: "devnet.lagos/v1",
Kind: "Devnet",
Metadata: YAMLDevnetMetadataOutput{
Name: d.Metadata.Name,
Namespace: d.Metadata.Namespace,
Labels: d.Metadata.Labels,
Annotations: d.Metadata.Annotations,
},
Spec: YAMLDevnetSpecOutput{
Network: d.Spec.Plugin, // proto uses Plugin, YAML uses network
NetworkType: d.Spec.NetworkType,
NetworkVersion: d.Spec.SdkVersion, // proto uses SdkVersion, YAML uses networkVersion
Validators: d.Spec.Validators,
FullNodes: d.Spec.FullNodes,
Mode: d.Spec.Mode,
},
Status: YAMLDevnetStatusOutput{
Phase: d.Status.Phase,
Nodes: d.Status.Nodes,
ReadyNodes: d.Status.ReadyNodes,
CurrentHeight: d.Status.CurrentHeight,
SdkVersion: d.Status.SdkVersion,
Message: d.Status.Message,
},
}
}