Skip to content

Commit f4f420e

Browse files
move checkPortAvailable to its own package
1 parent 0227242 commit f4f420e

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

internal/container/start.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package container
33
import (
44
"context"
55
"fmt"
6-
"net"
76
"net/http"
87
"os"
98
stdruntime "runtime"
@@ -14,6 +13,7 @@ import (
1413
"github.com/localstack/lstk/internal/auth"
1514
"github.com/localstack/lstk/internal/config"
1615
"github.com/localstack/lstk/internal/output"
16+
"github.com/localstack/lstk/internal/ports"
1717
"github.com/localstack/lstk/internal/runtime"
1818
)
1919

@@ -142,7 +142,7 @@ func selectContainersToStart(ctx context.Context, rt runtime.Runtime, sink outpu
142142
output.EmitLog(sink, fmt.Sprintf("%s is already running", c.Name))
143143
continue
144144
}
145-
if err := checkPortAvailable(c.Port); err != nil {
145+
if err := ports.CheckAvailable(c.Port); err != nil {
146146
configPath, pathErr := config.ConfigFilePath()
147147
if pathErr != nil {
148148
return nil, err
@@ -154,18 +154,6 @@ func selectContainersToStart(ctx context.Context, rt runtime.Runtime, sink outpu
154154
return filtered, nil
155155
}
156156

157-
func checkPortAvailable(port string) error {
158-
conn, err := net.DialTimeout("tcp", "localhost:"+port, time.Second)
159-
if err != nil {
160-
return nil
161-
}
162-
err = conn.Close()
163-
if err != nil {
164-
return nil
165-
}
166-
return fmt.Errorf("port %s already in use", port)
167-
}
168-
169157
func validateLicense(ctx context.Context, rt runtime.Runtime, sink output.Sink, platformClient api.PlatformAPI, containerConfig runtime.ContainerConfig, token string) error {
170158
version := containerConfig.Tag
171159
if version == "" || version == "latest" {

internal/ports/ports.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ports
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"time"
7+
)
8+
9+
func CheckAvailable(port string) error {
10+
conn, err := net.DialTimeout("tcp", "localhost:"+port, time.Second)
11+
if err != nil {
12+
return nil
13+
}
14+
_ = conn.Close()
15+
return fmt.Errorf("port %s already in use", port)
16+
}

0 commit comments

Comments
 (0)