转换结构体
(containers []types.Container, workingDir string, dockerComposeServices []string)
| 94 | |
| 95 | // 转换结构体 |
| 96 | func convertOriginContainer(containers []types.Container, |
| 97 | workingDir string, dockerComposeServices []string) (dockerComposeContainers []DockerComposeContainer) { |
| 98 | for _, container := range containers { |
| 99 | currentServiceName := container.Labels["com.docker.compose.service"] |
| 100 | currentWorkingDir := container.Labels["com.docker.compose.project.working_dir"] |
| 101 | if workingDir != "" && currentWorkingDir != workingDir { // 工作目录不匹配 |
| 102 | continue |
| 103 | } |
| 104 | if common.Contains(dockerComposeServices, currentServiceName) { |
| 105 | var ports []string |
| 106 | for _, port := range container.Ports { |
| 107 | if port.PublicPort <= 0 { |
| 108 | continue |
| 109 | } |
| 110 | str := fmt.Sprintf("%v:%v", port.PublicPort, port.PrivatePort) |
| 111 | if !common.Contains(ports, str) { // 限制重复的端口绑定信息 |
| 112 | ports = append(ports, str) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // 去掉/ |
| 117 | containerName := "" |
| 118 | for _, name := range container.Names { |
| 119 | tmp := "" |
| 120 | if strings.Index(name, "/") == 0 { |
| 121 | tmp = name[1:] |
| 122 | } else { |
| 123 | tmp = name |
| 124 | } |
| 125 | if len(containerName) > 0 { |
| 126 | containerName += "," + tmp |
| 127 | } else { |
| 128 | containerName += tmp |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | dockerComposeContainer := DockerComposeContainer{ |
| 133 | ServiceName: currentServiceName, |
| 134 | ContainerName: containerName, |
| 135 | State: container.State, |
| 136 | Image: container.Image, |
| 137 | ImageID: container.ImageID, |
| 138 | Ports: ports, |
| 139 | } |
| 140 | dockerComposeContainers = append(dockerComposeContainers, dockerComposeContainer) |
| 141 | break |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | return dockerComposeContainers |
| 147 | } |
| 148 | |
| 149 | // 打印 service 列表 |
| 150 | func PrintDockerComposeContainers(dockerComposeContainers []DockerComposeContainer) { |
no outgoing calls
no test coverage detected