use tables for displaying list command output

This commit is contained in:
Anish Mukherjee 2022-11-28 15:05:55 +05:30
parent 9d37f068b2
commit 9061c2f26c
6 changed files with 65 additions and 9 deletions

View file

@ -2,8 +2,11 @@ package dns
import (
"fmt"
"os"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
"github.com/spf13/cobra"
)
@ -13,20 +16,27 @@ var dnsListCmd = &cobra.Command{
Short: "List DNS entries",
Long: `List DNS entries`,
Run: func(cmd *cobra.Command, args []string) {
var data []models.DNSEntry
if networkName != "" {
switch dnsType {
case "node":
functions.PrettyPrint(functions.GetNodeDNS(networkName))
data = *functions.GetNodeDNS(networkName)
case "custom":
functions.PrettyPrint(functions.GetCustomDNS(networkName))
data = *functions.GetCustomDNS(networkName)
case "network", "":
functions.PrettyPrint(functions.GetNetworkDNS(networkName))
data = *functions.GetNetworkDNS(networkName)
default:
fmt.Println("Invalid DNS type provided ", dnsType)
}
} else {
functions.PrettyPrint(functions.GetDNS())
data = *functions.GetDNS()
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Network", "IPv4 Address", "IPv6 Address"})
for _, d := range data {
table.Append([]string{d.Name, d.Network, d.Address, d.Address6})
}
table.Render()
},
}

View file

@ -1,7 +1,13 @@
package ext_client
import (
"os"
"strconv"
"time"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
"github.com/spf13/cobra"
)
@ -13,11 +19,18 @@ var extClientListCmd = &cobra.Command{
Short: "List External Clients",
Long: `List External Clients`,
Run: func(cmd *cobra.Command, args []string) {
var data []models.ExtClient
if networkName != "" {
functions.PrettyPrint(functions.GetNetworkExtClients(networkName))
data = *functions.GetNetworkExtClients(networkName)
} else {
functions.PrettyPrint(functions.GetAllExtClients())
data = *functions.GetAllExtClients()
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ClientID", "Network", "IPv4 Address", "IPv6 Address", "Enabled", "Last Modified"})
for _, d := range data {
table.Append([]string{d.ClientID, d.Network, d.Address, d.Address6, strconv.FormatBool(d.Enabled), time.Unix(d.LastModified, 0).String()})
}
table.Render()
},
}

View file

@ -1,7 +1,11 @@
package node
import (
"os"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
"github.com/spf13/cobra"
)
@ -12,11 +16,28 @@ var nodeListCmd = &cobra.Command{
Short: "List all nodes",
Long: `List all nodes`,
Run: func(cmd *cobra.Command, args []string) {
var data []models.Node
if networkName != "" {
functions.PrettyPrint(functions.GetNodes(networkName))
data = *functions.GetNodes(networkName)
} else {
functions.PrettyPrint(functions.GetNodes())
data = *functions.GetNodes()
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Node Name", "Addresses", "Version", "Network", "Egress Status", "Ingress Status", "Relay Status"})
for _, d := range data {
addresses := ""
if d.Address != "" {
addresses += d.Address
}
if d.Address6 != "" {
if d.Address != "" {
addresses += ", "
}
addresses += d.Address6
}
table.Append([]string{d.Name, addresses, d.Version, d.Network, d.IsEgressGateway, d.IsIngressGateway, d.IsRelay})
}
table.Render()
},
}

View file

@ -1,7 +1,12 @@
package user
import (
"os"
"strconv"
"strings"
"github.com/gravitl/netmaker/cli/functions"
"github.com/guumaster/tablewriter"
"github.com/spf13/cobra"
)
@ -11,7 +16,12 @@ var userListCmd = &cobra.Command{
Short: "List all users",
Long: `List all users`,
Run: func(cmd *cobra.Command, args []string) {
functions.PrettyPrint(functions.ListUsers())
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Admin", "Networks", "Groups"})
for _, d := range *functions.ListUsers() {
table.Append([]string{d.UserName, strconv.FormatBool(d.IsAdmin), strings.Join(d.Networks, ", "), strings.Join(d.Groups, ", ")})
}
table.Render()
},
}

1
go.mod
View file

@ -43,6 +43,7 @@ require (
github.com/agnivade/levenshtein v1.1.1
github.com/coreos/go-oidc/v3 v3.4.0
github.com/gorilla/websocket v1.5.0
github.com/guumaster/tablewriter v0.0.10
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.5.0
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e

1
go.sum
View file

@ -289,6 +289,7 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb
github.com/guumaster/hostctl v1.1.3 h1:b/yR3svkYsbr5VBdvfdyLXUl2xaKopSzgE/Xi7+1WRo=
github.com/guumaster/hostctl v1.1.3/go.mod h1:h5rDx5Z8Hj2bYZfDt/eX4BNS2RSq7iRcGVQqfROJyH8=
github.com/guumaster/tablewriter v0.0.10 h1:A0HD94yMdt4usgxBjoEceNeE0XMJ027euoHAzsPqBQs=
github.com/guumaster/tablewriter v0.0.10/go.mod h1:p4FRFhyfo0UD9ZLmMRbbJooTUsxo6b80qZTERVDWrH8=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=