diff options
author | spf13 <steve.francia@gmail.com> | 2014-05-15 15:07:46 -0400 |
---|---|---|
committer | spf13 <steve.francia@gmail.com> | 2014-05-15 17:41:03 -0400 |
commit | 296d218e6704e73b3261bca6f5f72b3569cf899b (patch) | |
tree | 7cab3198b5ef5f07209db6f3683f91092ce2d35c /commands | |
parent | b520f8852d566f778ad13e2201adcf71c7d42d34 (diff) |
Better handling of when the specified port is already in use
Diffstat (limited to 'commands')
-rw-r--r-- | commands/server.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/commands/server.go b/commands/server.go index ed04f0410..2b2794fe8 100644 --- a/commands/server.go +++ b/commands/server.go @@ -15,6 +15,7 @@ package commands import ( "fmt" + "net" "net/http" "os" "strconv" @@ -56,6 +57,19 @@ func server(cmd *cobra.Command, args []string) { BaseUrl = "http://" + BaseUrl } + l, err := net.Listen("tcp", ":"+strconv.Itoa(serverPort)) + if err == nil { + l.Close() + } else { + jww.ERROR.Println("port", serverPort, "already in use, attempting to use an available port") + sp, err := helpers.FindAvailablePort() + if err != nil { + jww.ERROR.Println("Unable to find alternative port to use") + jww.ERROR.Fatalln(err) + } + serverPort = sp.Port + } + if serverAppend { viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")+":"+strconv.Itoa(serverPort)) } else { |