list.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package acl
  2. import (
  3. "os"
  4. "github.com/gravitl/netmaker/cli/functions"
  5. "github.com/gravitl/netmaker/logic/acls"
  6. "github.com/guumaster/tablewriter"
  7. "github.com/spf13/cobra"
  8. )
  9. var aclListCmd = &cobra.Command{
  10. Use: "list [NETWORK NAME]",
  11. Args: cobra.ExactArgs(1),
  12. Short: "List all ACLs associated with a network",
  13. Long: `List all ACLs associated with a network`,
  14. Run: func(cmd *cobra.Command, args []string) {
  15. aclSource := (map[acls.AclID]acls.ACL)(*functions.GetACL(args[0]))
  16. nodes := functions.GetNodes(args[0])
  17. idNameMap := make(map[string]string)
  18. for _, node := range *nodes {
  19. idNameMap[node.ID] = node.Name
  20. }
  21. table := tablewriter.NewWriter(os.Stdout)
  22. table.SetHeader([]string{"From", "To", "Status"})
  23. for id, acl := range aclSource {
  24. for k, v := range (map[acls.AclID]byte)(acl) {
  25. row := []string{idNameMap[string(id)], idNameMap[string(k)]}
  26. switch v {
  27. case acls.NotAllowed:
  28. row = append(row, "Not Allowed")
  29. case acls.NotPresent:
  30. row = append(row, "Not Present")
  31. case acls.Allowed:
  32. row = append(row, "Allowed")
  33. }
  34. table.Append(row)
  35. }
  36. }
  37. table.Render()
  38. },
  39. }
  40. func init() {
  41. rootCmd.AddCommand(aclListCmd)
  42. }