From 9061c2f26c1d47e2f16508a4a881a60c493f1ce5 Mon Sep 17 00:00:00 2001 From: Anish Mukherjee Date: Mon, 28 Nov 2022 15:05:55 +0530 Subject: [PATCH] use tables for displaying list command output --- cli/cmd/dns/list.go | 18 ++++++++++++++---- cli/cmd/ext_client/list.go | 17 +++++++++++++++-- cli/cmd/node/list.go | 25 +++++++++++++++++++++++-- cli/cmd/user/list.go | 12 +++++++++++- go.mod | 1 + go.sum | 1 + 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/cli/cmd/dns/list.go b/cli/cmd/dns/list.go index afabaa96..4f0a91d0 100644 --- a/cli/cmd/dns/list.go +++ b/cli/cmd/dns/list.go @@ -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() }, } diff --git a/cli/cmd/ext_client/list.go b/cli/cmd/ext_client/list.go index 9822d805..cd95bcef 100644 --- a/cli/cmd/ext_client/list.go +++ b/cli/cmd/ext_client/list.go @@ -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() }, } diff --git a/cli/cmd/node/list.go b/cli/cmd/node/list.go index 084b090f..b793efd6 100644 --- a/cli/cmd/node/list.go +++ b/cli/cmd/node/list.go @@ -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() }, } diff --git a/cli/cmd/user/list.go b/cli/cmd/user/list.go index da46bcea..12bb563a 100644 --- a/cli/cmd/user/list.go +++ b/cli/cmd/user/list.go @@ -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() }, } diff --git a/go.mod b/go.mod index cb0b163f..197e26be 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index bff73e40..717785a2 100644 --- a/go.sum +++ b/go.sum @@ -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=