MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / simplifyPort

Function simplifyPort

agent/app/service/container.go:2040–2109  ·  view source on GitHub ↗
(ports []container.Port)

Source from the content-addressed store, hash-verified

2038 return append(list1, list2...)
2039}
2040func simplifyPort(ports []container.Port) []string {
2041 var datas []string
2042 if len(ports) == 0 {
2043 return datas
2044 }
2045 if len(ports) == 1 {
2046 ip := ""
2047 if len(ports[0].IP) != 0 {
2048 ip = ports[0].IP + ":"
2049 }
2050 itemPortStr := fmt.Sprintf("%s%v/%s", ip, ports[0].PrivatePort, ports[0].Type)
2051 if ports[0].PublicPort != 0 {
2052 itemPortStr = fmt.Sprintf("%s%v->%v/%s", ip, ports[0].PublicPort, ports[0].PrivatePort, ports[0].Type)
2053 }
2054 datas = append(datas, itemPortStr)
2055 return datas
2056 }
2057
2058 sort.Slice(ports, func(i, j int) bool {
2059 return ports[i].PrivatePort < ports[j].PrivatePort
2060 })
2061 start := ports[0]
2062
2063 for i := 1; i < len(ports); i++ {
2064 if ports[i].PrivatePort != ports[i-1].PrivatePort+1 || ports[i].IP != ports[i-1].IP || ports[i].PublicPort != ports[i-1].PublicPort+1 || ports[i].Type != ports[i-1].Type {
2065 if ports[i-1].PrivatePort == start.PrivatePort {
2066 itemPortStr := fmt.Sprintf("%s:%v/%s", start.IP, start.PrivatePort, start.Type)
2067 if start.PublicPort != 0 {
2068 itemPortStr = fmt.Sprintf("%s:%v->%v/%s", start.IP, start.PublicPort, start.PrivatePort, start.Type)
2069 }
2070 if len(start.IP) == 0 {
2071 itemPortStr = strings.TrimPrefix(itemPortStr, ":")
2072 }
2073 datas = append(datas, itemPortStr)
2074 } else {
2075 itemPortStr := fmt.Sprintf("%s:%v-%v/%s", start.IP, start.PrivatePort, ports[i-1].PrivatePort, start.Type)
2076 if start.PublicPort != 0 {
2077 itemPortStr = fmt.Sprintf("%s:%v-%v->%v-%v/%s", start.IP, start.PublicPort, ports[i-1].PublicPort, start.PrivatePort, ports[i-1].PrivatePort, start.Type)
2078 }
2079 if len(start.IP) == 0 {
2080 itemPortStr = strings.TrimPrefix(itemPortStr, ":")
2081 }
2082 datas = append(datas, itemPortStr)
2083 }
2084 start = ports[i]
2085 }
2086 if i == len(ports)-1 {
2087 if ports[i].PrivatePort == start.PrivatePort {
2088 itemPortStr := fmt.Sprintf("%s:%v/%s", start.IP, start.PrivatePort, start.Type)
2089 if start.PublicPort != 0 {
2090 itemPortStr = fmt.Sprintf("%s:%v->%v/%s", start.IP, start.PublicPort, start.PrivatePort, start.Type)
2091 }
2092 if len(start.IP) == 0 {
2093 itemPortStr = strings.TrimPrefix(itemPortStr, ":")
2094 }
2095 datas = append(datas, itemPortStr)
2096 } else {
2097 itemPortStr := fmt.Sprintf("%s:%v-%v/%s", start.IP, start.PrivatePort, ports[i].PrivatePort, start.Type)

Callers 1

transPortToStrFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected