Only log min and max port when connecting via TCP; move port determination inside of the TCP function

This commit is contained in:
Megan Marsh 2019-12-09 12:21:45 -08:00
parent a02a95e992
commit 8b50f03056
1 changed files with 17 additions and 17 deletions

View File

@ -51,20 +51,7 @@ func Server() (*packrpc.Server, error) {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
} }
minPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MIN_PORT"), 10, 32) listener, err := serverListener()
if err != nil {
return nil, err
}
maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
if err != nil {
return nil, err
}
log.Printf("Plugin minimum port: %d\n", minPort)
log.Printf("Plugin maximum port: %d\n", maxPort)
listener, err := serverListener(minPort, maxPort)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,15 +91,28 @@ func Server() (*packrpc.Server, error) {
return packrpc.NewServer(conn) return packrpc.NewServer(conn)
} }
func serverListener(minPort, maxPort int64) (net.Listener, error) { func serverListener() (net.Listener, error) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return serverListener_tcp(minPort, maxPort) return serverListener_tcp()
} }
return serverListener_unix() return serverListener_unix()
} }
func serverListener_tcp(minPort, maxPort int64) (net.Listener, error) { func serverListener_tcp() (net.Listener, error) {
minPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MIN_PORT"), 10, 32)
if err != nil {
return nil, err
}
maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
if err != nil {
return nil, err
}
log.Printf("Plugin minimum port: %d/%d\n", minPort)
log.Printf("Plugin maximum port: %d/%d\n", maxPort)
for port := minPort; port <= maxPort; port++ { for port := minPort; port <= maxPort; port++ {
address := fmt.Sprintf("127.0.0.1:%d", port) address := fmt.Sprintf("127.0.0.1:%d", port)
listener, err := net.Listen("tcp", address) listener, err := net.Listen("tcp", address)