fix(dockerCompose): add port.ExistProtocol
This commit is contained in:
parent
54af4469a4
commit
0a68b2d8e7
@ -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(`/(?<protocol>[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"]
|
||||
}
|
||||
|
50
pkg/domain/dockerCompose/config_intern_test.go
Normal file
50
pkg/domain/dockerCompose/config_intern_test.go
Normal file
@ -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())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user