list.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package node
  2. import (
  3. "os"
  4. "github.com/gravitl/netmaker/cli/functions"
  5. "github.com/gravitl/netmaker/models"
  6. "github.com/guumaster/tablewriter"
  7. "github.com/spf13/cobra"
  8. )
  9. // nodeListCmd lists all nodes
  10. var nodeListCmd = &cobra.Command{
  11. Use: "list",
  12. Args: cobra.NoArgs,
  13. Short: "List all nodes",
  14. Long: `List all nodes`,
  15. Run: func(cmd *cobra.Command, args []string) {
  16. var data []models.Node
  17. if networkName != "" {
  18. data = *functions.GetNodes(networkName)
  19. } else {
  20. data = *functions.GetNodes()
  21. }
  22. table := tablewriter.NewWriter(os.Stdout)
  23. table.SetHeader([]string{"Name", "Addresses", "Version", "Network", "Egress", "Ingress", "Relay", "ID"})
  24. for _, d := range data {
  25. addresses := ""
  26. if d.Address != "" {
  27. addresses += d.Address
  28. }
  29. if d.Address6 != "" {
  30. if d.Address != "" {
  31. addresses += ", "
  32. }
  33. addresses += d.Address6
  34. }
  35. table.Append([]string{d.Name, addresses, d.Version, d.Network, d.IsEgressGateway, d.IsIngressGateway, d.IsRelay, d.ID})
  36. }
  37. table.Render()
  38. },
  39. }
  40. func init() {
  41. nodeListCmd.Flags().StringVar(&networkName, "network", "", "Network name specifier")
  42. rootCmd.AddCommand(nodeListCmd)
  43. }