-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
236 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters