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 ( import (
"os" "os"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions" "github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/logic/acls" "github.com/gravitl/netmaker/logic/acls"
"github.com/guumaster/tablewriter" "github.com/guumaster/tablewriter"
@ -16,6 +17,10 @@ var aclListCmd = &cobra.Command{
Long: `List all ACLs associated with a network`, Long: `List all ACLs associated with a network`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
aclSource := (map[acls.AclID]acls.ACL)(*functions.GetACL(args[0])) 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 := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"From", "To", "Status"}) table.SetHeader([]string{"From", "To", "Status"})
for id, acl := range aclSource { for id, acl := range aclSource {
@ -33,6 +38,7 @@ var aclListCmd = &cobra.Command{
} }
} }
table.Render() 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" "fmt"
"os" "os"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions" "github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter" "github.com/guumaster/tablewriter"
@ -31,12 +32,17 @@ var dnsListCmd = &cobra.Command{
} else { } else {
data = *functions.GetDNS() data = *functions.GetDNS()
} }
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
default:
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Network", "IPv4 Address", "IPv6 Address"}) table.SetHeader([]string{"Name", "Network", "IPv4 Address", "IPv6 Address"})
for _, d := range data { for _, d := range data {
table.Append([]string{d.Name, d.Network, d.Address, d.Address6}) table.Append([]string{d.Name, d.Network, d.Address, d.Address6})
} }
table.Render() table.Render()
}
}, },
} }

View file

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions" "github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter" "github.com/guumaster/tablewriter"
@ -25,12 +26,17 @@ var extClientListCmd = &cobra.Command{
} else { } else {
data = *functions.GetAllExtClients() data = *functions.GetAllExtClients()
} }
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
default:
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Client ID", "Network", "IPv4 Address", "IPv6 Address", "Enabled", "Last Modified"}) table.SetHeader([]string{"Client ID", "Network", "IPv4 Address", "IPv6 Address", "Enabled", "Last Modified"})
for _, d := range data { 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.Append([]string{d.ClientID, d.Network, d.Address, d.Address6, strconv.FormatBool(d.Enabled), time.Unix(d.LastModified, 0).String()})
} }
table.Render() table.Render()
}
}, },
} }

View file

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

View file

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

View file

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

View file

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