add json format output alternative to tables

This commit is contained in:
Anish Mukherjee 2023-03-06 12:08:46 +05:30
parent 38f13edb7e
commit 8e09684def
8 changed files with 97 additions and 49 deletions

View file

@ -3,6 +3,7 @@ package acl
import (
"os"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/logic/acls"
"github.com/guumaster/tablewriter"
@ -16,6 +17,10 @@ var aclListCmd = &cobra.Command{
Long: `List all ACLs associated with a network`,
Run: func(cmd *cobra.Command, args []string) {
aclSource := (map[acls.AclID]acls.ACL)(*functions.GetACL(args[0]))
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(aclSource)
default:
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"From", "To", "Status"})
for id, acl := range aclSource {
@ -33,6 +38,7 @@ var aclListCmd = &cobra.Command{
}
}
table.Render()
}
},
}

View file

@ -0,0 +1,9 @@
package commons
// OutputFormat flag defines the output format to stdout (Enum:- json)
var OutputFormat string
const (
// JsonOutput refers to json format output to stdout
JsonOutput = "json"
)

View file

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
@ -31,12 +32,17 @@ var dnsListCmd = &cobra.Command{
} else {
data = *functions.GetDNS()
}
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
default:
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

@ -5,6 +5,7 @@ import (
"strconv"
"time"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
@ -25,12 +26,17 @@ var extClientListCmd = &cobra.Command{
} else {
data = *functions.GetAllExtClients()
}
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
default:
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Client ID", "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

@ -4,6 +4,7 @@ import (
"os"
"time"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
@ -16,6 +17,10 @@ var networkListCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
networks := functions.GetNetworks()
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(networks)
default:
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"NetId", "Address Range (IPv4)", "Address Range (IPv6)", "Network Last Modified", "Nodes Last Modified"})
for _, n := range *networks {
@ -24,6 +29,7 @@ var networkListCmd = &cobra.Command{
table.Append([]string{n.NetID, n.AddressRange, n.AddressRange6, networkLastModified, nodesLastModified})
}
table.Render()
}
},
}

View file

@ -4,6 +4,7 @@ import (
"os"
"strconv"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
@ -23,6 +24,10 @@ var nodeListCmd = &cobra.Command{
} else {
data = *functions.GetNodes()
}
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
default:
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Addresses", "Network", "Egress", "Ingress", "Relay"})
for _, d := range data {
@ -40,6 +45,7 @@ var nodeListCmd = &cobra.Command{
strconv.FormatBool(d.IsEgressGateway), strconv.FormatBool(d.IsIngressGateway), strconv.FormatBool(d.IsRelay)})
}
table.Render()
}
},
}

View file

@ -4,6 +4,7 @@ import (
"os"
"github.com/gravitl/netmaker/cli/cmd/acl"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/cmd/context"
"github.com/gravitl/netmaker/cli/cmd/dns"
"github.com/gravitl/netmaker/cli/cmd/enrollment_key"
@ -42,6 +43,7 @@ func Execute() {
}
func init() {
rootCmd.PersistentFlags().StringVarP(&commons.OutputFormat, "output", "o", "", "List output in specific format (Enum:- json)")
// Bind subcommands here
rootCmd.AddCommand(network.GetRoot())
rootCmd.AddCommand(context.GetRoot())

View file

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