Browse Source

Fix: change append to use slice index in ps.go

Signed-off-by: Blane Tschida <[email protected]>
IDOMATH 1 year ago
parent
commit
250c3112b9
2 changed files with 6 additions and 6 deletions
  1. 4 4
      pkg/compose/ps.go
  2. 2 2
      pkg/compose/ps_test.go

+ 4 - 4
pkg/compose/ps.go

@@ -45,17 +45,17 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options api
 	for i, container := range containers {
 		i, container := i, container
 		eg.Go(func() error {
-			var publishers []api.PortPublisher
+			publishers := make([]api.PortPublisher, len(container.Ports))
 			sort.Slice(container.Ports, func(i, j int) bool {
 				return container.Ports[i].PrivatePort < container.Ports[j].PrivatePort
 			})
-			for _, p := range container.Ports {
-				publishers = append(publishers, api.PortPublisher{
+			for i, p := range container.Ports {
+				publishers[i] = api.PortPublisher{
 					URL:           p.IP,
 					TargetPort:    int(p.PrivatePort),
 					PublishedPort: int(p.PublicPort),
 					Protocol:      p.Type,
-				})
+				}
 			}
 
 			inspect, err := s.apiClient().ContainerInspect(ctx, container.ID)

+ 2 - 2
pkg/compose/ps_test.go

@@ -56,7 +56,7 @@ func TestPs(t *testing.T) {
 
 	expected := []compose.ContainerSummary{
 		{ID: "123", Name: "123", Names: []string{"/123"}, Image: "foo", Project: strings.ToLower(testProject), Service: "service1",
-			State: "running", Health: "healthy", Publishers: nil,
+			State: "running", Health: "healthy", Publishers: []compose.PortPublisher{},
 			Labels: map[string]string{
 				compose.ProjectLabel:     strings.ToLower(testProject),
 				compose.ConfigFilesLabel: "/src/pkg/compose/testdata/compose.yaml",
@@ -75,7 +75,7 @@ func TestPs(t *testing.T) {
 			},
 		},
 		{ID: "789", Name: "789", Names: []string{"/789"}, Image: "foo", Project: strings.ToLower(testProject), Service: "service2",
-			State: "exited", Health: "", ExitCode: 130, Publishers: nil,
+			State: "exited", Health: "", ExitCode: 130, Publishers: []compose.PortPublisher{},
 			Labels: map[string]string{
 				compose.ProjectLabel:     strings.ToLower(testProject),
 				compose.ConfigFilesLabel: "/src/pkg/compose/testdata/compose.yaml",