mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-11 20:03:58 +02:00
Better handling of when the specified port is already in use
This commit is contained in:
@@ -15,6 +15,8 @@ package helpers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -49,3 +51,16 @@ func StripHTML(s string) string {
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func FindAvailablePort() (*net.TCPAddr, error) {
|
||||
l, err := net.Listen("tcp", ":0")
|
||||
if err == nil {
|
||||
defer l.Close()
|
||||
addr := l.Addr()
|
||||
if a, ok := addr.(*net.TCPAddr); ok {
|
||||
return a, nil
|
||||
}
|
||||
return nil, fmt.Errorf("Unable to obtain a valid tcp port. %v", addr)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user