list.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package node
  2. import (
  3. "os"
  4. "strconv"
  5. "github.com/gravitl/netmaker/cli/cmd/commons"
  6. "github.com/gravitl/netmaker/cli/functions"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/guumaster/tablewriter"
  9. "github.com/spf13/cobra"
  10. )
  11. // nodeListCmd lists all nodes
  12. var nodeListCmd = &cobra.Command{
  13. Use: "list",
  14. Args: cobra.NoArgs,
  15. Short: "List all nodes",
  16. Long: `List all nodes`,
  17. Run: func(cmd *cobra.Command, args []string) {
  18. var data []models.ApiNode
  19. if networkName != "" {
  20. data = *functions.GetNodes(networkName)
  21. } else {
  22. data = *functions.GetNodes()
  23. }
  24. switch commons.OutputFormat {
  25. case commons.JsonOutput:
  26. functions.PrettyPrint(data)
  27. default:
  28. table := tablewriter.NewWriter(os.Stdout)
  29. table.SetHeader([]string{"ID", "Addresses", "Network", "Egress", "Remote Access Gateway", "Relay", "Type"})
  30. for _, d := range data {
  31. addresses := ""
  32. if d.Address != "" {
  33. addresses += d.Address
  34. }
  35. if d.Address6 != "" {
  36. if d.Address != "" {
  37. addresses += ", "
  38. }
  39. addresses += d.Address6
  40. }
  41. network := d.Network
  42. id := d.ID
  43. nodeType := "Device"
  44. if d.IsStatic {
  45. id = d.StaticNode.ClientID
  46. nodeType = "Static"
  47. }
  48. if d.IsUserNode {
  49. id = d.StaticNode.OwnerID
  50. nodeType = "User"
  51. }
  52. if d.IsStatic || d.IsUserNode {
  53. addresses = d.StaticNode.Address
  54. if d.StaticNode.Address6 != "" {
  55. if addresses != "" {
  56. addresses += ", "
  57. }
  58. addresses += d.StaticNode.Address6
  59. }
  60. network = d.StaticNode.Network
  61. }
  62. table.Append([]string{id, addresses, network,
  63. strconv.FormatBool(d.IsEgressGateway), strconv.FormatBool(d.IsIngressGateway), strconv.FormatBool(d.IsRelay), nodeType})
  64. }
  65. table.Render()
  66. }
  67. },
  68. }
  69. func init() {
  70. nodeListCmd.Flags().StringVar(&networkName, "network", "", "Network name specifier")
  71. rootCmd.AddCommand(nodeListCmd)
  72. }