Skip to content

Commit

Permalink
feat: reload command
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Jan 17, 2024
1 parent 9adfdbb commit 6dca566
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 27 deletions.
66 changes: 66 additions & 0 deletions cmd/ctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"path"
"strings"

"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/natesales/pathvector/pkg/templating"
)

func protocols(birdDirectory string) (map[string]*templating.Protocol, error) {
// Read protocol names map
protos := map[string]*templating.Protocol{}
if !realProtocolNames {
contents, err := os.ReadFile(path.Join(birdDirectory, "protocols.json"))
if err != nil {
return nil, fmt.Errorf("reading protocol names: %v", err)
}
if err := json.Unmarshal(contents, &protos); err != nil {
return nil, fmt.Errorf("unmarshalling protocol names: %v", err)
}
}
return protos, nil
}

// normalize makes a string all lowercase and removes spaces, dashes, and underscores
func normalize(s string) string {
return strings.ReplaceAll(
strings.ReplaceAll(
strings.ReplaceAll(strings.ToLower(s), " ", ""),
"-", ""),
"_", "")
}

// protocolByQuery returns a BIRD BGP protocol string by a given name
func protocolByQuery(query string, protocols map[string]*templating.Protocol) (string, string) {
if query == "all" {
return "all", "all"
}

// Expand AFI suffix
if strings.HasSuffix(query, " 4") {
query = strings.TrimSuffix(query, " 4") + " v4"
} else if strings.HasSuffix(query, " 6") {
query = strings.TrimSuffix(query, " 6") + " v6"
}

query = normalize(query)
for birdProto, meta := range protocols {
if fuzzy.Match(query, normalize(birdProto)) || fuzzy.Match(query, normalize(meta.Name)) {
return birdProto, meta.Name
}
}
return "", ""
}

// confirmYesNo asks a [y/N] question and returns true if the user selects yes
func confirmYesNo(question string) bool {
fmt.Printf("%s [y/N] ", question)
var response string
_, _ = fmt.Scanln(&response)
return response == "y" || response == "Y"
}
79 changes: 79 additions & 0 deletions cmd/ctl_reload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"fmt"
"slices"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/natesales/pathvector/pkg/bird"
)

func init() {
rootCmd.AddCommand(reloadCmd)
}

func usage() {
log.Fatal("Usage: pathvector reload [direction] [session]")
}

func parseArgs(args []string) (string, string) {
if len(args) == 0 {
usage()
}

direction := args[0]
query := strings.Join(args[1:], " ")
if !slices.Contains([]string{"in", "out"}, direction) {
direction = "both"
query = strings.Join(args, " ")
}

return query, direction
}

var reloadCmd = &cobra.Command{
Use: "reload [in|out] [session]",
Short: "Show version information",
Run: func(cmd *cobra.Command, args []string) {
// Load config file
c, err := loadConfig()
if err != nil {
log.Fatal(err)
}

query, direction := parseArgs(args)

// Load protocol names map
protos, err := protocols(c.BIRDDirectory)
if err != nil {
log.Fatal(err)
}

log.Debugf("Looking for protocol for %s", query)
birdProtoName, richName := protocolByQuery(query, protos)
if birdProtoName == "" {
log.Fatalf("no protocol found for query: %s", query)
}

if !confirmYesNo(fmt.Sprintf("Are you sure you want to reload %s (%s)?", richName, birdProtoName)) {
log.Fatal("Cancelled")
}

// Reload protocol
reloadCmd := "reload"
if direction != "both" {
reloadCmd += " " + direction
}
reloadCmd += " " + birdProtoName

log.Debugf("Running command: %s", reloadCmd)
out, _, err := bird.RunCommand(reloadCmd, c.BIRDSocket)
if err != nil {
log.Fatal(err)
}
log.Info(out)
},
}
29 changes: 29 additions & 0 deletions cmd/ctl_reload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCtlReloadParseArgs(t *testing.T) {
for _, tc := range []struct {
args []string
expQuery string
expDirection string
}{
{[]string{"in", "all"}, "all", "in"},
{[]string{"out", "all"}, "all", "out"},
{[]string{"all"}, "all", "both"},
{[]string{"in", "he"}, "he", "in"},
{[]string{"out", "he"}, "he", "out"},
{[]string{"he"}, "he", "both"},
} {
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
query, direction := parseArgs(tc.args)
assert.Equal(t, tc.expQuery, query)
assert.Equal(t, tc.expDirection, direction)
})
}
}
30 changes: 30 additions & 0 deletions cmd/ctl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"

"github.com/natesales/pathvector/pkg/templating"
)

func TestCtlProtocolByQuery(t *testing.T) {
protocolsJSON := `{"HURRICANE_ELECTRIC_AS6939_v4":{"Name":"Hurricane Electric","Tags":null},"HURRICANE_ELECTRIC_AS6939_v6":{"Name":"Hurricane Electric","Tags":null}}`
var protocols map[string]*templating.Protocol
assert.Nil(t, json.Unmarshal([]byte(protocolsJSON), &protocols))

for _, tc := range []struct {
expected string
query string
}{
{"HURRICANE_ELECTRIC_AS6939_v4", "Hurricane Electric v4"},
{"HURRICANE_ELECTRIC_AS6939_v4", "hurricane v4"},
{"HURRICANE_ELECTRIC_AS6939_v6", "he v6"},
} {
t.Run(tc.query, func(t *testing.T) {
birdProto, _ := protocolByQuery(tc.query, protocols)
assert.Equal(t, tc.expected, birdProto)
})
}
}
19 changes: 4 additions & 15 deletions cmd/status.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"path"
"strings"

"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/natesales/pathvector/pkg/bird"
"github.com/natesales/pathvector/pkg/templating"
"github.com/natesales/pathvector/pkg/util"
)

Expand Down Expand Up @@ -47,16 +43,9 @@ var statusCmd = &cobra.Command{
log.Fatal(err)
}

// Read protocol names map
var protocols map[string]*templating.Protocol
if !realProtocolNames {
contents, err := os.ReadFile(path.Join(c.BIRDDirectory, "protocols.json"))
if err != nil {
log.Fatalf("Reading protocol names: %v", err)
}
if err := json.Unmarshal(contents, &protocols); err != nil {
log.Fatalf("Unmarshalling protocol names: %v", err)
}
protos, err := protocols(c.BIRDDirectory)
if err != nil {
log.Fatal(err)
}

protocolStates, err := bird.ParseProtocols(commandOutput)
Expand All @@ -81,7 +70,7 @@ var statusCmd = &cobra.Command{
// Lookup peer in protocol JSON
protocolName := protocolState.Name
var tags []string
if p, found := protocols[protocolState.Name]; found {
if p, found := protos[protocolState.Name]; found {
protocolName = p.Name
tags = p.Tags
}
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.18
require (
github.com/creasty/defaults v1.7.0
github.com/fatih/color v1.15.0
github.com/go-ping/ping v1.1.0
github.com/go-playground/validator/v10 v10.12.0
github.com/lithammer/fuzzysearch v1.1.8
github.com/natesales/logknife v0.0.4-0.20230403055117-5e928ad4153b
github.com/olekukonko/tablewriter v0.0.5
github.com/sirupsen/logrus v1.9.0
Expand All @@ -30,8 +30,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
36 changes: 27 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.12.0 h1:E4gtWgxWxp8YSxExrQFv5BpCahla0PVF2oTTEYaWQGI=
github.com/go-playground/validator/v10 v10.12.0/go.mod h1:hCAPuzYvKdP33pxWa+2+6AIKXEKqjIUyqsNCtbsSJrA=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/leodido/go-urn v1.2.3 h1:6BE2vPT0lqoz3fmOesHZiaiFh7889ssCo2GMvLCfiuA=
github.com/leodido/go-urn v1.2.3/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down Expand Up @@ -58,29 +57,48 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down

0 comments on commit 6dca566

Please sign in to comment.