Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspf13 <steve.francia@gmail.com>2014-05-15 23:07:46 +0400
committerspf13 <steve.francia@gmail.com>2014-05-16 01:41:03 +0400
commit296d218e6704e73b3261bca6f5f72b3569cf899b (patch)
tree7cab3198b5ef5f07209db6f3683f91092ce2d35c /helpers
parentb520f8852d566f778ad13e2201adcf71c7d42d34 (diff)
Better handling of when the specified port is already in use
Diffstat (limited to 'helpers')
-rw-r--r--helpers/general.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/helpers/general.go b/helpers/general.go
index 208b0d2d3..a2ebac9e1 100644
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -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
+}