From a3b438883302ca8807f1372ac2bd64e7fbd8982f Mon Sep 17 00:00:00 2001 From: Ivan Ilves Date: Tue, 16 Apr 2024 12:25:21 +0200 Subject: [PATCH] refactor: add WithNameEx() func to wrap arbitrary regex rule --- .gitignore | 3 +++ Makefile | 2 ++ cmd/travelgrunt/main.go | 5 +---- pkg/config/with.go | 12 ++++++++++++ pkg/config/with_test.go | 17 +++++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 pkg/config/with.go create mode 100644 pkg/config/with_test.go diff --git a/.gitignore b/.gitignore index c2ccd1f..7aaac10 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ vendor/ /cmd/travelgrunt/* !/cmd/travelgrunt/*.go /dist/* + +# Local Makefile +local.mk diff --git a/Makefile b/Makefile index a3074a7..5f8a8b9 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ NEXT_VERSION = v${API_VERSION}.${NEXT_PATCH} SHELL := /bin/bash +-include local.mk + default: dep build all: dep build test install diff --git a/cmd/travelgrunt/main.go b/cmd/travelgrunt/main.go index 81eea90..d53b4e0 100644 --- a/cmd/travelgrunt/main.go +++ b/cmd/travelgrunt/main.go @@ -6,7 +6,6 @@ import ( "os" "github.com/ivanilves/travelgrunt/pkg/config" - "github.com/ivanilves/travelgrunt/pkg/config/rule" "github.com/ivanilves/travelgrunt/pkg/directory" "github.com/ivanilves/travelgrunt/pkg/directory/tree" "github.com/ivanilves/travelgrunt/pkg/file" @@ -117,9 +116,7 @@ func main() { } if expression != "" { - rules := []rule.Rule{{NameEx: expression}} - - cfg.Rules = rules + cfg = cfg.WithNameEx(expression) } entries, paths, err := directory.Collect(rootPath, cfg) diff --git a/pkg/config/with.go b/pkg/config/with.go new file mode 100644 index 0000000..e8e88e8 --- /dev/null +++ b/pkg/config/with.go @@ -0,0 +1,12 @@ +package config + +import ( + "github.com/ivanilves/travelgrunt/pkg/config/rule" +) + +// WithNameEx returns a copy of config with regex-based rule added +func (cfg Config) WithNameEx(nameEx string) Config { + cfg.Rules = []rule.Rule{rule.Rule{NameEx: nameEx}} + + return cfg +} diff --git a/pkg/config/with_test.go b/pkg/config/with_test.go new file mode 100644 index 0000000..a1e90c5 --- /dev/null +++ b/pkg/config/with_test.go @@ -0,0 +1,17 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestWithNameEx(t *testing.T) { + assert := assert.New(t) + + cfg := DefaultConfig() + + cfg = cfg.WithNameEx("*.tf") + + assert.Len(cfg.Rules, 1) +}