From 0a68b2d8e75e9a26bb0f6aa9ff585cac774cb685 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Thu, 20 Feb 2025 12:33:57 +0100 Subject: [PATCH] fix(dockerCompose): add port.ExistProtocol --- pkg/domain/dockerCompose/config.go | 21 ++++++++ .../dockerCompose/config_intern_test.go | 50 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 pkg/domain/dockerCompose/config_intern_test.go diff --git a/pkg/domain/dockerCompose/config.go b/pkg/domain/dockerCompose/config.go index 287d50c..64f9fa0 100644 --- a/pkg/domain/dockerCompose/config.go +++ b/pkg/domain/dockerCompose/config.go @@ -2,6 +2,7 @@ package dockerCompose import ( "fmt" + "regexp" "strings" ) @@ -1973,3 +1974,23 @@ func splitStringInVolume(s string) (string, string, string) { } return src, dest, "" } + +var protocolRegExp = regexp.MustCompile(`/(?[a-z]*)$`) + +type port string + +func (p port) existsProtocol() bool { + return protocolRegExp.MatchString(string(p)) +} + +func (p port) getProtocol() string { + result := make(map[string]string, 0) + matches := protocolRegExp.FindStringSubmatch(string(p)) + for i, name := range protocolRegExp.SubexpNames() { + if i != 0 && len(name) > 0 { + result[name] = matches[i] + } + } + + return result["protocol"] +} diff --git a/pkg/domain/dockerCompose/config_intern_test.go b/pkg/domain/dockerCompose/config_intern_test.go new file mode 100644 index 0000000..6973f71 --- /dev/null +++ b/pkg/domain/dockerCompose/config_intern_test.go @@ -0,0 +1,50 @@ +package dockerCompose + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPort_ExistsProtocol(t *testing.T) { + require := require.New(t) + + testCases := []struct { + s string + expectedResult bool + }{ + { + s: "0", + expectedResult: false, + }, + { + s: "53", + expectedResult: false, + }, + { + s: "53/tcp", + expectedResult: true, + }, + { + s: "53/udp", + expectedResult: true, + }, + { + s: "53:53", + expectedResult: false, + }, + { + s: "53:53/udp", + expectedResult: true, + }, + { + s: "53:53/tcp", + expectedResult: true, + }, + } + + for _, testCase := range testCases { + p := port(testCase.s) + require.Equal(testCase.expectedResult, p.existsProtocol()) + } +}