From 1dcc2f3485d1bc6ede0fb5eb37bcb882d71c7720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Gr=C3=B8ndahl?= Date: Mon, 10 Aug 2026 09:25:08 +0200 Subject: [PATCH] fix(docs): correct invalid regex in snapshot ecs help examples The --*-regex flags compile their values as Go RE2 regexes, but two examples used "*-prod-*", which fails with "missing argument to repetition operator" - and only after the AWS API calls have run. Also anchor the four remaining examples that read as globs: "backend-*" compiles, but -* means "zero or more hyphens" and matching is unanchored, so it really means "contains backend". Add a test that every regex pattern in the examples compiles. --- cmd/kosli/snapshotECS.go | 12 ++++++------ cmd/kosli/snapshotECS_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/kosli/snapshotECS.go b/cmd/kosli/snapshotECS.go index 3d05372f4..aaeec4d5d 100644 --- a/cmd/kosli/snapshotECS.go +++ b/cmd/kosli/snapshotECS.go @@ -50,7 +50,7 @@ kosli snapshot ecs my-env \ kosli snapshot ecs my-env --clusters my-cluster ... # include clusters matching a pattern in the AWS account -kosli snapshot ecs my-env --clusters-regex "my-cluster-*" ... +kosli snapshot ecs my-env --clusters-regex "^my-cluster-.*" ... # include clusters matching a list of names in the AWS account kosli snapshot ecs my-env --clusters my-cluster1,my-cluster2 ... @@ -59,7 +59,7 @@ kosli snapshot ecs my-env --clusters my-cluster1,my-cluster2 ... kosli snapshot ecs my-env --exclude my-cluster ... # exclude clusters matching a pattern in the AWS account -kosli snapshot ecs my-env --exclude-regex "my-cluster-*" ... +kosli snapshot ecs my-env --exclude-regex "^my-cluster-.*" ... # exclude clusters matching a list of names in the AWS account kosli snapshot ecs my-env --exclude my-cluster1,my-cluster2 ... @@ -68,10 +68,10 @@ kosli snapshot ecs my-env --exclude my-cluster1,my-cluster2 ... kosli snapshot ecs my-env --clusters my-cluster --services backend-app ... # include Services matching a pattern in one cluster -kosli snapshot ecs my-env --clusters my-cluster --services-regex "backend-*" ... +kosli snapshot ecs my-env --clusters my-cluster --services-regex "^backend-.*" ... # include production Services only (by naming convention) in all clusters in the AWS account -kosli snapshot ecs my-env --services-regex "*-prod-*" ... +kosli snapshot ecs my-env --services-regex ".*-prod-.*" ... # include Services matching a name in all clusters in the AWS account kosli snapshot ecs my-env --services backend-app ... @@ -80,10 +80,10 @@ kosli snapshot ecs my-env --services backend-app ... kosli snapshot ecs my-env --services backend-app,frontend-app ... # exclude Services matching a pattern in one cluster -kosli snapshot ecs my-env --clusters my-cluster --exclude-services-regex "backend-*" ... +kosli snapshot ecs my-env --clusters my-cluster --exclude-services-regex "^backend-.*" ... # exclude Production services only (by naming convention) in all clusters in the AWS account -kosli snapshot ecs my-env --exclude-services-regex "*-prod-*" ... +kosli snapshot ecs my-env --exclude-services-regex ".*-prod-.*" ... # exclude Services matching a name in one cluster kosli snapshot ecs my-env --clusters my-cluster --exclude-services backend-app ... diff --git a/cmd/kosli/snapshotECS_test.go b/cmd/kosli/snapshotECS_test.go index 415f335b0..9fcfdb540 100644 --- a/cmd/kosli/snapshotECS_test.go +++ b/cmd/kosli/snapshotECS_test.go @@ -2,9 +2,11 @@ package main import ( "fmt" + "regexp" "testing" "github.com/kosli-dev/cli/internal/testHelpers" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -129,6 +131,18 @@ func (suite *SnapshotECSTestSuite) TestSnapshotECSCmd() { } } +// the --*-regex flags compile their values as Go regexes, so every regex +// pattern shown in the help examples must actually compile +func TestSnapshotECSExampleRegexesAreValid(t *testing.T) { + patterns := regexp.MustCompile(`--[a-z-]*regex "([^"]*)"`).FindAllStringSubmatch(snapshotECSExample, -1) + require.NotEmpty(t, patterns, "expected the examples to contain regex patterns") + + for _, p := range patterns { + _, err := regexp.Compile(p[1]) + require.NoError(t, err, "example regex %q does not compile", p[1]) + } +} + // In order for 'go test' to run this suite, we need to create // a normal test function and pass our suite to suite.Run func TestSnapshotECSTestSuite(t *testing.T) {