talos/cmd/osctl/cmd/interfaces.go
Andrey Smirnov fc52025490 fix: provide peer remote address for 'NODE': as default in osctl
This change is pretty mechanical, just wrap every API so that remote
peer address is used as default for `resp.Metadata.Hostname`.

This makes `NODE:` non-empty in all the API calls.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2019-12-05 00:11:55 +03:00

72 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// nolint: dupl,golint
package cmd
import (
"fmt"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
networkapi "github.com/talos-systems/talos/api/network"
"github.com/talos-systems/talos/cmd/osctl/pkg/client"
"github.com/talos-systems/talos/cmd/osctl/pkg/helpers"
)
// interfacesCmd represents the net interfaces command
var interfacesCmd = &cobra.Command{
Use: "interfaces",
Short: "List network interfaces",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
helpers.Should(cmd.Usage())
os.Exit(1)
}
setupClient(func(c *client.Client) {
var remotePeer peer.Peer
reply, err := c.Interfaces(globalCtx, grpc.Peer(&remotePeer))
if err != nil {
helpers.Fatalf("error getting interfaces: %s", err)
}
intersRender(&remotePeer, reply)
})
},
}
func intersRender(remotePeer *peer.Peer, reply *networkapi.InterfacesReply) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "NODE\tINDEX\tNAME\tMAC\tMTU\tADDRESS")
defaultNode := addrFromPeer(remotePeer)
for _, resp := range reply.Response {
node := defaultNode
if resp.Metadata != nil {
node = resp.Metadata.Hostname
}
for _, netif := range resp.Interfaces {
for _, addr := range netif.Ipaddress {
fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%d\t%s\n", node, netif.Index, netif.Name, netif.Hardwareaddr, netif.Mtu, addr)
}
}
}
helpers.Should(w.Flush())
}
func init() {
rootCmd.AddCommand(interfacesCmd)
}