Skip to content

Commit

Permalink
feat: bird-fmt command
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Jan 18, 2024
1 parent c4aa3f4 commit 7287a5a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
80 changes: 80 additions & 0 deletions cmd/bird_fmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cmd

import (
"os"
"path/filepath"
"strings"

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

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

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

func isDir(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}

return fi.IsDir(), nil
}

func formatFile(path string) error {
if !strings.HasSuffix(path, ".conf") {
log.Debugf("Skipping %s", path)
return nil
}

log.Infof("Formatting %s", path)

unformatted, err := os.ReadFile(path)
if err != nil {
return err
}

formatted := bird.Reformat(string(unformatted))

if err := os.WriteFile(path, []byte(formatted), 0644); err != nil {
return err
}

return nil
}

var birdFmtCmd = &cobra.Command{
Use: "bird-fmt [file/directory]",
Short: "Format BIRD config",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("No file/directory specified")
}

target := args[0]

// Check if directory
dir, err := isDir(target)
if err != nil {
log.Fatal(err)
}
if dir {
// For file in walk directory
if err := filepath.Walk(target, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
return formatFile(path)
}); err != nil {
log.Fatal(err)
}
} else {
if err := formatFile(target); err != nil {
log.Fatal(err)
}
}
},
}
3 changes: 3 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ Usage:
pathvector [command]
Available Commands:
bird-fmt Format BIRD config
birdsh Lightweight BIRD shell
completion Generate the autocompletion script for the specified shell
config Export configuration, optionally sanitized with logknife
dump Dump configuration
generate Generate router configuration
help Help about any command
match Find common IXPs for a given ASN
reload Reload a session
restart Restart a session
status Show protocol status
version Show version information
Expand Down
10 changes: 5 additions & 5 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Additional command line arguments to pass to bgpq4
|------|---------|------------|
| string | | |

### `bgpq-bin`
### `bgpq-binary`

Path to bgpq4 binary

Expand Down Expand Up @@ -671,15 +671,15 @@ BGP local preference

### `local-pref4`

IPv4 BGP local preference
IPv4 BGP local preference (overrides local-pref, not included in optimizer)

| Type | Default | Validation |
|------|---------|------------|
| int | | |

### `local-pref6`

IPv6 BGP local preference
IPv6 BGP local preference (overrides local-pref, not included in optimizer)

| Type | Default | Validation |
|------|---------|------------|
Expand Down Expand Up @@ -967,15 +967,15 @@ Remove all standard and large communities beginning with this value

### `as-prefs`

Map of ASN to import local pref
Map of ASN to import local pref (not included in optimizer)

| Type | Default | Validation |
|------|---------|------------|
| map[uint32]uint32 | | |

### `community-prefs`

Map of community to import local pref
Map of community to import local pref (not included in optimizer)

| Type | Default | Validation |
|------|---------|------------|
Expand Down

0 comments on commit 7287a5a

Please sign in to comment.