list.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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", "Ingress", "Relay"})
  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. table.Append([]string{d.ID, addresses, d.Network,
  42. strconv.FormatBool(d.IsEgressGateway), strconv.FormatBool(d.IsIngressGateway), strconv.FormatBool(d.IsRelay)})
  43. }
  44. table.Render()
  45. }
  46. },
  47. }
  48. func init() {
  49. nodeListCmd.Flags().StringVar(&networkName, "network", "", "Network name specifier")
  50. rootCmd.AddCommand(nodeListCmd)
  51. }