-
Notifications
You must be signed in to change notification settings - Fork 64
/
list.go
152 lines (126 loc) · 3.47 KB
/
list.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
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file expect in compliance with the License.
package list
import (
"context"
"fmt"
"net"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
networkapi "kraftkit.sh/api/network/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/machine/network"
)
type ListOptions struct {
Driver string `noattribute:"true"`
Long bool `long:"long" short:"l" usage:"Show more information"`
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list" default:"table"`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&ListOptions{}, cobra.Command{
Short: "List machine networks",
Use: "list [FLAGS]",
Aliases: []string{"ls"},
Args: cobra.NoArgs,
Long: heredoc.Doc(`
List machine networks.
`),
Example: heredoc.Doc(`
# List all machine networks
$ kraft network list
# List all machine networks in JSON format
$ kraft network list -o json
# List all machine networks with all information
$ kraft network list -l
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "net",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.Driver = cmd.Flag("driver").Value.String()
if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}
return nil
}
func (opts *ListOptions) Run(ctx context.Context, _ []string) error {
var err error
strategy, ok := network.Strategies()[opts.Driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %s", opts.Driver)
}
controller, err := strategy.NewNetworkV1alpha1(ctx)
if err != nil {
return err
}
networks, err := controller.List(ctx, &networkapi.NetworkList{})
if err != nil {
return err
}
type netTable struct {
id string
name string
network string
driver string
status networkapi.NetworkState
}
var items []netTable
for _, network := range networks.Items {
addr := &net.IPNet{
IP: net.ParseIP(network.Spec.Gateway),
Mask: net.IPMask(net.ParseIP(network.Spec.Netmask)),
}
items = append(items, netTable{
id: string(network.UID),
name: network.Name,
network: addr.String(),
driver: opts.Driver,
status: network.Status.State,
})
}
err = iostreams.G(ctx).StartPager()
if err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
cs := iostreams.G(ctx).ColorScheme()
table, err := tableprinter.NewTablePrinter(ctx,
tableprinter.WithMaxWidth(iostreams.G(ctx).TerminalWidth()),
tableprinter.WithOutputFormatFromString(opts.Output),
)
if err != nil {
return err
}
// Header row
if opts.Long {
table.AddField("MACHINE ID", cs.Bold)
}
table.AddField("NAME", cs.Bold)
table.AddField("NETWORK", cs.Bold)
table.AddField("DRIVER", cs.Bold)
table.AddField("STATUS", cs.Bold)
table.EndRow()
for _, item := range items {
if opts.Long {
table.AddField(item.id, nil)
}
table.AddField(item.name, nil)
table.AddField(item.network, nil)
table.AddField(item.driver, nil)
table.AddField(item.status.String(), nil)
table.EndRow()
}
return table.Render(iostreams.G(ctx).Out)
}