From 73fdf3374f4f7d39bcb8e6f75468b25415c80330 Mon Sep 17 00:00:00 2001 From: Christopher Dziomba Date: Fri, 20 Sep 2024 09:23:19 +0200 Subject: [PATCH] allow regex replace --- pkg/config/config.go | 5 +++-- pkg/frr/configure.go | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index aae329d..c705488 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -28,8 +28,9 @@ type VRFConfig struct { } type Replacement struct { - Old string `yaml:"old"` - New string `yaml:"new"` + Old string `yaml:"old"` + New string `yaml:"new"` + Regex bool `yaml:"regex"` } func LoadConfig() (*Config, error) { diff --git a/pkg/frr/configure.go b/pkg/frr/configure.go index 734d1b2..fa83c58 100644 --- a/pkg/frr/configure.go +++ b/pkg/frr/configure.go @@ -178,7 +178,12 @@ func fixRouteTargetReload(frrConfig []byte) []byte { // fixCfgReplacements replaces placeholders in the configuration with the actual values. func fixCfgReplacements(frrConfig []byte, replacements []config.Replacement) []byte { for _, replacement := range replacements { - frrConfig = bytes.ReplaceAll(frrConfig, []byte(replacement.Old), []byte(replacement.New)) + if !replacement.Regex { + frrConfig = bytes.ReplaceAll(frrConfig, []byte(replacement.Old), []byte(replacement.New)) + } else { + re := regexp.MustCompile(replacement.Old) + frrConfig = re.ReplaceAll(frrConfig, []byte(replacement.New)) + } } return frrConfig }