@@ -12,17 +12,22 @@ import (
1212 "github.com/spf13/cobra"
1313
1414 "github.com/natesales/pathvector/pkg/bird"
15+ "github.com/natesales/pathvector/pkg/templating"
1516 "github.com/natesales/pathvector/pkg/util"
1617)
1718
1819var (
19- userProtocolNames bool
20+ realProtocolNames bool
2021 onlyBGP bool
22+ showTags bool
23+ tagFilter []string
2124)
2225
2326func init () {
24- statusCmd .Flags ().BoolVarP (& userProtocolNames , "user -protocol-names" , "u " , false , "use user-defined protocol names" )
27+ statusCmd .Flags ().BoolVarP (& realProtocolNames , "real -protocol-names" , "r " , false , "use real protocol names" )
2528 statusCmd .Flags ().BoolVarP (& onlyBGP , "bgp" , "b" , false , "only show BGP protocols" )
29+ statusCmd .Flags ().BoolVar (& showTags , "tags" , false , "show tags column" )
30+ statusCmd .Flags ().StringArrayVarP (& tagFilter , "filter" , "f" , []string {}, "tags to filter by" )
2631 rootCmd .AddCommand (statusCmd )
2732}
2833
@@ -42,13 +47,13 @@ var statusCmd = &cobra.Command{
4247 }
4348
4449 // Read protocol names map
45- var protocolNames map [string ]string
46- if userProtocolNames {
50+ var protocols map [string ]* templating. Protocol
51+ if ! realProtocolNames {
4752 contents , err := os .ReadFile (path .Join ("/etc/bird/" , "protocol_names.json" ))
4853 if err != nil {
4954 log .Fatalf ("Reading protocol names: %v" , err )
5055 }
51- if err := json .Unmarshal (contents , & protocolNames ); err != nil {
56+ if err := json .Unmarshal (contents , & protocols ); err != nil {
5257 log .Fatalf ("Unmarshalling protocol names: %v" , err )
5358 }
5459 }
@@ -58,32 +63,43 @@ var statusCmd = &cobra.Command{
5863 log .Fatal (err )
5964 }
6065
61- util .PrintTable ([]string {"Peer" , "AS" , "Neighbor" , "State" , "In" , "Out" , "Since" , "Info" }, func () [][]string {
66+ header := []string {"Peer" , "AS" , "Neighbor" , "State" , "In" , "Out" , "Since" , "Info" }
67+ if showTags {
68+ header = append (header , "Tags" )
69+ }
70+ util .PrintTable (header , func () [][]string {
6271 var table [][]string
6372 for _ , protocolState := range protocolStates {
6473 if ! onlyBGP || protocolState .BGP != nil {
65- if protocolState .BGP == nil {
66- table = append (table , []string {
67- protocolName (protocolState .Name , protocolNames ),
68- "-" ,
69- "-" ,
70- colorStatus (protocolState .State ),
71- parseTableInt (protocolState .Routes .Imported ),
72- parseTableInt (protocolState .Routes .Exported ),
73- protocolState .Since ,
74- colorStatus (protocolState .Info ),
75- })
76- } else { // BGP
77- table = append (table , []string {
78- protocolName (protocolState .Name , protocolNames ),
79- parseTableInt (protocolState .BGP .NeighborAS ),
80- protocolState .BGP .NeighborAddress ,
74+ neighborAddr , neighborAS := "-" , "-"
75+ if protocolState .BGP != nil {
76+ neighborAS = parseTableInt (protocolState .BGP .NeighborAS )
77+ neighborAddr = protocolState .BGP .NeighborAddress
78+ }
79+
80+ // Lookup peer in protocol JSON
81+ protocolName := protocolState .Name
82+ var tags []string
83+ if p , found := protocols [protocolState .Name ]; found {
84+ protocolName = p .Name
85+ tags = p .Tags
86+ }
87+
88+ if len (tagFilter ) == 0 || containsAny (tagFilter , tags ) {
89+ row := []string {
90+ protocolName ,
91+ neighborAS ,
92+ neighborAddr ,
8193 colorStatus (protocolState .State ),
8294 parseTableInt (protocolState .Routes .Imported ),
8395 parseTableInt (protocolState .Routes .Exported ),
8496 protocolState .Since ,
8597 colorStatus (protocolState .Info ),
86- })
98+ }
99+ if showTags {
100+ row = append (row , strings .Join (tags , ", " ))
101+ }
102+ table = append (table , row )
87103 }
88104 }
89105 }
@@ -92,12 +108,16 @@ var statusCmd = &cobra.Command{
92108 },
93109}
94110
95- func protocolName (n string , names map [string ]string ) string {
96- if userSuppliedName , found := names [n ]; found {
97- return userSuppliedName
98- } else {
99- return n
111+ // containsAny checks if two string slices contain any of the same elements
112+ func containsAny (a []string , b []string ) bool {
113+ for _ , i := range a {
114+ for _ , j := range b {
115+ if i == j {
116+ return true
117+ }
118+ }
100119 }
120+ return false
101121}
102122
103123func parseTableInt (i int ) string {
0 commit comments