Files
EvoBGP/internal/birdfmt/scenarios.go
T

65 lines
1.6 KiB
Go

package birdfmt
import (
"embed"
"io/fs"
"path"
"sort"
)
//go:embed testdata/scenarios
var scenariosFS embed.FS
// BirdScenarioPaths returns POSIX-style directory paths (under testdata/scenarios)
// that contain a bird.conf file, sorted for stable iteration.
func BirdScenarioPaths() ([]string, error) {
var dirs []string
err := fs.WalkDir(scenariosFS, "testdata/scenarios", func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || d.Name() != "bird.conf" {
return nil
}
dir := path.Dir(p)
if dir == "testdata/scenarios" {
return nil
}
dirs = append(dirs, dir)
return nil
})
if err != nil {
return nil, err
}
sort.Strings(dirs)
return dirs, nil
}
// ScenarioBirdConfRel returns the path relative to module root internal/birdfmt/ for bird.conf.
func ScenarioBirdConfRel(scenarioDir string) string {
return path.Join(scenarioDir, "bird.conf")
}
// ReadScenarioBirdConf returns the contents of bird.conf for a scenario directory from embed.
func ReadScenarioBirdConf(scenarioDir string) ([]byte, error) {
return scenariosFS.ReadFile(path.Join(scenarioDir, "bird.conf"))
}
// RepoRelativeBirdConf is the path from the repository root (for CI / docker bind mounts).
func RepoRelativeBirdConf(scenarioDir string) string {
return path.Join("internal/birdfmt", scenarioDir, "bird.conf")
}
// AllScenarioNames returns leaf scenario names (last path segment) for tables / docs.
func AllScenarioNames() ([]string, error) {
paths, err := BirdScenarioPaths()
if err != nil {
return nil, err
}
names := make([]string, len(paths))
for i, p := range paths {
names[i] = path.Base(p)
}
return names, nil
}